diff --git a/src/facade/sdkFacade.ts b/src/facade/sdkFacade.ts index e142a00..d813c2e 100644 --- a/src/facade/sdkFacade.ts +++ b/src/facade/sdkFacade.ts @@ -87,12 +87,13 @@ export class SdkFacade { ); if (userId !== undefined) { - if (userId === null) { - sdk.unsetToken(); - } else { - const currentToken = sdk.context.getToken(); + const currentToken = sdk.context.getToken(); + const currentSubject = currentToken?.getSubject() ?? null; - if (currentToken?.getSubject() !== userId) { + if (currentSubject !== userId) { + if (userId === null) { + sdk.unsetToken(); + } else { sdk.identify(userId); } } diff --git a/test/evaluator.test.ts b/test/evaluator.test.ts index 7c33362..f7f0bd9 100644 --- a/test/evaluator.test.ts +++ b/test/evaluator.test.ts @@ -1,14 +1,14 @@ import * as fetchMock from 'fetch-mock'; import {MockOptions} from 'fetch-mock'; import { - Evaluator, ErrorResponse, EvaluationContext, EvaluationError, EvaluationErrorType, + EvaluationOptions, + Evaluator, QueryError, QueryErrorResponse, - EvaluationOptions, } from '../src/evaluator'; import {Token} from '../src/token'; import {BASE_ENDPOINT_URL, CLIENT_LIBRARY} from '../src/constants'; diff --git a/test/facade/sdkFacade.test.ts b/test/facade/sdkFacade.test.ts index 3e44849..58200e2 100644 --- a/test/facade/sdkFacade.test.ts +++ b/test/facade/sdkFacade.test.ts @@ -494,7 +494,7 @@ describe('A SDK facade', () => { expect(context.setToken).not.toHaveBeenCalled(); }); - it('should allow unidentifying a user', () => { + it('should allow anonymizing a user', () => { const context = createContextMock(); jest.spyOn(context, 'getToken') @@ -523,6 +523,62 @@ describe('A SDK facade', () => { expect(context.setToken).toHaveBeenCalledTimes(1); }); + it('should preserve the current token if the user is already anonymous', () => { + const context = createContextMock(); + + jest.spyOn(context, 'getToken') + .mockImplementation(() => Token.issue(appId, null)); + + jest.spyOn(context, 'setToken') + .mockImplementation(); + + jest.spyOn(Sdk, 'init') + .mockImplementationOnce(config => { + const sdk = Sdk.init(config); + + jest.spyOn(sdk, 'appId', 'get').mockReturnValue(appId); + jest.spyOn(sdk, 'context', 'get').mockReturnValue(context); + + return sdk; + }); + + SdkFacade.init({ + appId: appId, + track: false, + userId: null, + }); + + expect(context.setToken).not.toHaveBeenCalled(); + }); + + it('should preserve the current token if the user ID is the same', () => { + const context = createContextMock(); + + jest.spyOn(context, 'getToken') + .mockImplementation(() => Token.issue(appId, 'c4r0l')); + + jest.spyOn(context, 'setToken') + .mockImplementation(); + + jest.spyOn(Sdk, 'init') + .mockImplementationOnce(config => { + const sdk = Sdk.init(config); + + jest.spyOn(sdk, 'appId', 'get').mockReturnValue(appId); + jest.spyOn(sdk, 'context', 'get').mockReturnValue(context); + + return sdk; + }); + + SdkFacade.init({ + appId: appId, + track: false, + userId: 'c4r0l', + }); + + expect(context.setToken).not.toHaveBeenCalled(); + }); + it('should allow anonymizing a user', () => { const context = createContextMock();