From fa79c0bc46568033dc624d1bd3a9be57d3a6e3f0 Mon Sep 17 00:00:00 2001 From: Denys Oblohin Date: Thu, 5 May 2022 21:42:19 +0300 Subject: [PATCH] fix: post set_storage event when storage is cleared on logout . --- lib/TokenManager.ts | 5 +++-- test/spec/services/SyncStorageService.ts | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/TokenManager.ts b/lib/TokenManager.ts index aa01fd79d9..4270b10fd7 100644 --- a/lib/TokenManager.ts +++ b/lib/TokenManager.ts @@ -278,8 +278,8 @@ export class TokenManager implements TokenManagerInterface { } // for synchronization of LocalStorage cross tabs for IE11 - private emitSetStorageEvent() { - if (isIE11OrLess()) { + private emitSetStorageEvent(force = false) { + if (isIE11OrLess() || force) { const storage = this.storage.getStorage(); this.emitter.emit(EVENT_SET_STORAGE, storage); } @@ -434,6 +434,7 @@ export class TokenManager implements TokenManagerInterface { clear() { this.clearExpireEventTimeoutAll(); this.storage.clearStorage(); + this.emitSetStorageEvent(true); } clearPendingRemoveTokens() { diff --git a/test/spec/services/SyncStorageService.ts b/test/spec/services/SyncStorageService.ts index 72c4d9ee04..a7ff3254e6 100644 --- a/test/spec/services/SyncStorageService.ts +++ b/test/spec/services/SyncStorageService.ts @@ -40,7 +40,10 @@ describe('SyncStorageService', () => { getStorage: jest.fn().mockImplementation(() => storage), setStorage: jest.fn().mockImplementation((newStorage) => { storage = newStorage; - }) + }), + clearStorage: jest.fn().mockImplementation(() => { + storage = {}; + }), }; sdkMock = { options: {}, @@ -210,6 +213,18 @@ describe('SyncStorageService', () => { tokenManager.add('idToken', tokens.standardIdTokenParsed); expect(serviceChannel.postMessage).toHaveBeenCalledTimes(1); // only "added" }); + + it('should post "set_storage" event when token storage is cleared', () => { + createInstance(); + const serviceChannel = (service as any).channel; + jest.spyOn(serviceChannel, 'postMessage'); + tokenManager.clear(); + expect(serviceChannel.postMessage).toHaveBeenCalledTimes(1); + expect(serviceChannel.postMessage).toHaveBeenNthCalledWith(1, { + type: 'set_storage', + storage: {}, + }); + }); }); describe('IE11', () => {