diff --git a/src/index.test.ts b/src/index.test.ts index 5718f22..bdb1a39 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -815,6 +815,44 @@ test('Should note include context fields with "null" value', async () => { expect(url.searchParams.get('sessionId')).toBe('0'); }); +test('Should update context fields with await', async () => { + fetchMock.mockResponses( + [JSON.stringify(data), { status: 200 }], + [JSON.stringify(data), { status: 304 }] + ); + const config: IConfig = { + url: 'http://localhost/test', + clientKey: '12', + appName: 'web', + environment: 'prod', + }; + const client = new UnleashClient(config); + await client.updateContext({ + userId: '123', + sessionId: '456', + remoteAddress: 'address', + properties: { + property1: 'property1', + property2: 'property2', + }, + }); + + await client.start(); + + jest.advanceTimersByTime(1001); + + const url = new URL(getTypeSafeRequestUrl(fetchMock)); + + expect(url.searchParams.get('userId')).toEqual('123'); + expect(url.searchParams.get('sessionId')).toEqual('456'); + expect(url.searchParams.get('remoteAddress')).toEqual('address'); + expect(url.searchParams.get('properties[property1]')).toEqual('property1'); + expect(url.searchParams.get('properties[property2]')).toEqual('property2'); + expect(url.searchParams.get('appName')).toEqual('web'); + expect(url.searchParams.get('environment')).toEqual('prod'); +}); + + test('Should update context fields on request', async () => { fetchMock.mockResponses( [JSON.stringify(data), { status: 200 }], diff --git a/src/index.ts b/src/index.ts index f31ee01..3f8be32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -108,6 +108,7 @@ export class UnleashClient extends TinyEmitter { private customHeaders: Record; private readyEventEmitted = false; private usePOSTrequests = false; + private started = false; constructor({ storageProvider, @@ -247,7 +248,7 @@ export class UnleashClient extends TinyEmitter { if (this.timerRef) { await this.fetchToggles(); - } else { + } else if(this.started) { await new Promise((resolve) => { const listener = () => { this.fetchToggles().then(() => { @@ -294,6 +295,7 @@ export class UnleashClient extends TinyEmitter { } public async start(): Promise { + this.started = true; if (this.timerRef) { console.error( 'Unleash SDK has already started, if you want to restart the SDK you should call client.stop() before starting again.'