Skip to content

Commit

Permalink
Merge pull request #55 from Unleash/feat/ssr
Browse files Browse the repository at this point in the history
Feat/ssr
  • Loading branch information
FredrikOseberg authored Dec 10, 2021
2 parents 347f6dc + 680ef96 commit 23aa2ae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
73 changes: 58 additions & 15 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,64 @@ test('Should bootstrap data when bootstrap is provided', async () => {
bootstrap,
};
const client = new UnleashClient(config);
await client.start();

await new Promise((resolve) => {
client.on('initialized', resolve);
});
expect(client.getAllToggles()).toStrictEqual(bootstrap);
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(bootstrap));
});

test('Should set internal toggle state when bootstrap is set, 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 initialData = [
{
name: 'initialData',
enabled: true,
variant: {
name: 'disabled',
enabled: false,
},
},
{
name: 'test initial',
enabled: true,
variant: {
name: 'disabled',
enabled: false,
},
},
];

localStorage.setItem(storeKey, JSON.stringify(initialData));
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(initialData));

const config: IConfig = {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
bootstrap,
};
const client = new UnleashClient(config);
expect(client.getAllToggles()).toStrictEqual(bootstrap);
await client.start();
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(bootstrap));
});

Expand Down Expand Up @@ -258,10 +310,7 @@ test('Should not bootstrap data when bootstrapOverride is false and localStorage
bootstrapOverride: false,
};
const client = new UnleashClient(config);

await new Promise((resolve) => {
client.on('initialized', resolve);
});
await client.start();

expect(client.getAllToggles()).toStrictEqual(initialData);
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(initialData));
Expand Down Expand Up @@ -300,10 +349,7 @@ test('Should bootstrap when bootstrapOverride is false and local storage is empt
bootstrapOverride: false,
};
const client = new UnleashClient(config);

await new Promise((resolve) => {
client.on('initialized', resolve);
});
await client.start();

expect(client.getAllToggles()).toStrictEqual(bootstrap);
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(bootstrap));
Expand Down Expand Up @@ -342,10 +388,7 @@ test('Should not bootstrap data when bootstrap is []', async () => {
bootstrapOverride: true,
};
const client = new UnleashClient(config);

await new Promise((resolve) => {
client.on('initialized', resolve);
});
await client.start();

expect(client.getAllToggles()).toStrictEqual(initialData);
expect(localStorage.getItem(storeKey)).toBe(JSON.stringify(initialData));
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class UnleashClient extends TinyEmitter {
throw new Error('appName is required.');
}

this.toggles = bootstrap && bootstrap.length > 0 ? bootstrap : [];
this.url = new URL(`${url}`);
this.clientKey = clientKey;
this.storage = storageProvider || new LocalStorageProvider();
Expand Down

0 comments on commit 23aa2ae

Please sign in to comment.