Skip to content

Commit

Permalink
fix: merge sessionId instead of removing it (#119)
Browse files Browse the repository at this point in the history
It is unexpected for "uppdateContext" to remove
the automatically generated sessionId. This fix keeps the
sessionId as long as it is not specified in the new
context passed in to the "updateContext" method.

fixes: #118
  • Loading branch information
ivarconr committed Oct 6, 2022
1 parent bba7406 commit 2262f74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,36 @@ test('Should update context fields on request', async () => {
expect(url.searchParams.get('environment')).toEqual('prod');
});

test('Should not replace sessionId when updating context', 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.start();
const context = client.getContext();
await client.updateContext({
userId: '123',
remoteAddress: 'address',
properties: {
property1: 'property1',
property2: 'property2',
},
});

jest.advanceTimersByTime(1001);

const url = new URL(getTypeSafeRequestUrl(fetchMock));

expect(url.searchParams.get('sessionId')).toEqual(context.sessionId?.toString());
});

test('Should not add property fields when properties is an empty object', async () => {
fetchMock.mockResponses(
[JSON.stringify(data), { status: 200 }],
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export class UnleashClient extends TinyEmitter {
const staticContext = {
environment: this.context.environment,
appName: this.context.appName,
sessionId: this.context.sessionId,
};
this.context = { ...staticContext, ...context };
if (this.timerRef) {
Expand Down

0 comments on commit 2262f74

Please sign in to comment.