Skip to content

Commit

Permalink
Add an EAP init hook and bind EAP methods to the plug scope
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Mar 30, 2021
1 parent 702c289 commit a266f23
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/eap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {SlotId, FetchResponse} from './fetch';
import {NullableJsonObject} from './sdk/json';
import {Plug} from './plug';

export interface EapFeatures {
fetch<P extends NullableJsonObject, I extends SlotId = SlotId>(slotId: I): Promise<FetchResponse<I, P>>;
fetch<P extends NullableJsonObject, I extends SlotId = SlotId>(this: Plug, slotId: I): Promise<FetchResponse<I, P>>;
}

interface EapHooks extends EapFeatures{
initialize(this: Plug): void;
}

declare global {
interface Window {
croctEap?: Partial<EapFeatures>;
croctEap?: Partial<EapHooks>;
}
}
12 changes: 9 additions & 3 deletions src/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ export class GlobalPlug implements Plug {
);
}

const initializeEap = window.croctEap?.initialize;

if (typeof initializeEap === 'function') {
initializeEap.call(this);
}

Promise.all(pending).then(() => {
this.initialize();

Expand Down Expand Up @@ -314,7 +320,7 @@ export class GlobalPlug implements Plug {
* This API is unstable and subject to change in future releases.
*/
public fetch<P extends NullableJsonObject, I extends SlotId = SlotId>(slotId: I): Promise<FetchResponse<I, P>> {
return this.eap('fetch')(slotId);
return this.eap('fetch').call(this, slotId);
}

public async unplug(): Promise<void> {
Expand Down Expand Up @@ -366,7 +372,7 @@ export class GlobalPlug implements Plug {
private eap<T extends keyof EapFeatures>(feature: T): EapFeatures[T] {
const logger = this.sdk.getLogger();
const eap = window.croctEap;
const method = typeof eap === 'object' ? eap[feature] : undefined;
const method: EapFeatures[T] | undefined = typeof eap === 'object' ? eap[feature] : undefined;

if (typeof method !== 'function') {
throw new Error(
Expand All @@ -378,6 +384,6 @@ export class GlobalPlug implements Plug {

logger.warn(`The ${feature} API is still unstable and subject to change in future releases.`);

return method.bind(eap);
return method;
}
}
19 changes: 18 additions & 1 deletion test/plug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Plugin, PluginFactory} from '../src/plugin';
import {GlobalPlug} from '../src/plug';
import {CDN_URL} from '../src/constants';
import {Token} from '../src/sdk/token';
import {Plug} from '../build';

jest.mock('../src/constants', () => {
return {
Expand Down Expand Up @@ -119,6 +120,18 @@ describe('The Croct plug', () => {
expect(initialize).toBeCalledWith(config);
});

test('should call the EAP initialization hook', () => {
window.croctEap = {
initialize: jest.fn().mockImplementation(function initialize(this: Plug) {
expect(this).toBe(croct);
}),
};

croct.plug({appId: APP_ID});

expect(window.croctEap.initialize).toBeCalled();
});

test('should log failures initializing plugins', () => {
croct.extend('foo', () => {
throw new Error('Failure');
Expand Down Expand Up @@ -856,7 +869,11 @@ describe('The Croct plug', () => {
const response = Promise.resolve({payload: {title: 'Hello'}});

window.croctEap = {
fetch: jest.fn().mockReturnValue(response),
fetch: jest.fn().mockImplementation(function fetch(this: Plug) {
expect(this).toBe(croct);

return response;
}),
};

const actualResponse = croct.fetch('foo');
Expand Down

0 comments on commit a266f23

Please sign in to comment.