Skip to content

Commit

Permalink
Add property to check whether the plug has been initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed May 28, 2021
1 parent a138cef commit 9ad60b7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface Plug extends EapFeatures {
readonly tracker: TrackerFacade;
readonly user: UserFacade;
readonly session: SessionFacade;
readonly initialized: boolean;
readonly flushed: Promise<this>;
readonly plugged: Promise<this>;

Expand Down Expand Up @@ -78,10 +79,10 @@ export class GlobalPlug implements Plug {

private initialize: {(): void};

private initialized: Promise<void>;
private ready: Promise<void>;

public constructor() {
this.initialized = new Promise(resolve => {
this.ready = new Promise(resolve => {
this.initialize = resolve;
});
}
Expand Down Expand Up @@ -243,8 +244,12 @@ export class GlobalPlug implements Plug {
});
}

public get initialized(): boolean {
return this.instance !== undefined;
}

public get plugged(): Promise<this> {
return this.initialized.then(() => this);
return this.ready.then(() => this);
}

public get flushed(): Promise<this> {
Expand Down Expand Up @@ -361,7 +366,7 @@ export class GlobalPlug implements Plug {
delete this.instance;

this.plugins = {};
this.initialized = new Promise(resolve => {
this.ready = new Promise(resolve => {
this.initialize = resolve;
});

Expand Down
17 changes: 15 additions & 2 deletions test/plug.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {SdkFacade, Configuration as SdkFacadeConfiguration} from '@croct/sdk/facade/sdkFacade';
import {Logger} from '../src/sdk';
import {Plugin, PluginFactory} from '../src/plugin';
import {GlobalPlug} from '../src/plug';
import {GlobalPlug, Plug} 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 @@ -335,6 +334,20 @@ describe('The Croct plug', () => {
expect(initialize).toBeCalledTimes(1);
});

test('should determine whether it is initialized', async () => {
const config: SdkFacadeConfiguration = {appId: APP_ID};

expect(croct.initialized).toBe(false);

croct.plug(config);

expect(croct.initialized).toBe(true);

await croct.unplug();

expect(croct.initialized).toBe(false);
});

test('should provide a callback that is called when the plug is plugged in', async () => {
const config: SdkFacadeConfiguration = {appId: APP_ID};

Expand Down

0 comments on commit 9ad60b7

Please sign in to comment.