Skip to content

Commit

Permalink
Merge pull request #64 from Unleash/fix/emit-ready-on-bootstrap
Browse files Browse the repository at this point in the history
fix: emit ready event on bootstrap
  • Loading branch information
FredrikOseberg authored Jan 25, 2022
2 parents 8185a89 + ba4e3fd commit 4d84934
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
43 changes: 40 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,41 @@ test('Should not bootstrap data when bootstrap is []', async () => {
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(initialData));
});

test('Should publish ready event when bootstrap is provided, before client is started', async () => {
localStorage.clear();
const storeKey = 'unleash:repository:repo';
const bootstrap = [
{
name: 'toggles',
enabled: true,
variant: {
name: 'disabled',
enabled: false,
},
},
{
name: 'algo',
enabled: true,
variant: {
name: 'disabled',
enabled: false,
},
},
];

const config: IConfig = {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
bootstrap,
};
const client = new UnleashClient(config);
expect(client.getAllToggles()).toStrictEqual(bootstrap);
client.on(EVENTS.READY, () => {
expect(client.isEnabled('algo')).toBe(true);
});
});

test('Should publish ready when initial fetch completed', (done) => {
fetchMock.mockResponseOnce(JSON.stringify(data));
const config: IConfig = {
Expand Down Expand Up @@ -846,17 +881,19 @@ test('Should pass under custom header clientKey', async () => {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
headerName: 'NotAuthorization'
headerName: 'NotAuthorization',
};
const client = new UnleashClient(config);

client.on(EVENTS.UPDATE, () => {
expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][1].headers).toMatchObject({ 'NotAuthorization': '12' });
expect(fetchMock.mock.calls[0][1].headers).toMatchObject({
NotAuthorization: '12',
});
client.stop();
});

await client.start();

jest.advanceTimersByTime(999);
});
});
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface IConfig extends IStaticContext {
fetch?: any;
bootstrap?: IToggle[];
bootstrapOverride?: boolean;
headerName?: string
headerName?: string;
}

interface IVariant {
Expand Down Expand Up @@ -105,7 +105,7 @@ export class UnleashClient extends TinyEmitter {
fetch = resolveFetch(),
bootstrap,
bootstrapOverride = true,
headerName = 'Authorization'
headerName = 'Authorization',
}: IConfig) {
super();
// Validations
Expand Down Expand Up @@ -155,7 +155,7 @@ export class UnleashClient extends TinyEmitter {
url,
clientKey,
fetch,
headerName
headerName,
});
}

Expand Down Expand Up @@ -228,8 +228,8 @@ export class UnleashClient extends TinyEmitter {
) {
await this.storage.save(storeKey, this.bootstrap);
this.toggles = this.bootstrap;
this.emit(EVENTS.READY);
}

this.emit(EVENTS.INIT);
}

Expand All @@ -244,7 +244,11 @@ export class UnleashClient extends TinyEmitter {
this.metrics.start();
const interval = this.refreshInterval;
await this.fetchToggles();
this.emit(EVENTS.READY);

if (!this.bootstrap) {
this.emit(EVENTS.READY);
}

if (interval > 0) {
this.timerRef = setInterval(() => this.fetchToggles(), interval);
}
Expand Down

0 comments on commit 4d84934

Please sign in to comment.