From 63a0f358354dd8b22c1286fff3a2735463995e51 Mon Sep 17 00:00:00 2001 From: ivgo Date: Thu, 29 Sep 2022 13:24:37 +0200 Subject: [PATCH] Add tests for the fallback Signed-off-by: ivgo --- .../StorageApi/UserSettingsStorage.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/plugins/user-settings/src/apis/StorageApi/UserSettingsStorage.test.ts b/plugins/user-settings/src/apis/StorageApi/UserSettingsStorage.test.ts index 0ebd96d19c..a421c413dc 100644 --- a/plugins/user-settings/src/apis/StorageApi/UserSettingsStorage.test.ts +++ b/plugins/user-settings/src/apis/StorageApi/UserSettingsStorage.test.ts @@ -38,6 +38,10 @@ describe('Persistent Storage API', () => { const mockIdentityApi: Partial = { getCredentials: async () => ({ token: 'a-token' }), }; + const mockIdentityApiFallback: Partial = { + // This API recreates the guest mode, where the WebStorage is used as fallback + getCredentials: async () => ({}), + }; const createPersistentStorage = ( args?: Partial<{ @@ -56,6 +60,23 @@ describe('Persistent Storage API', () => { }); }; + const createPersistentStorageFallback = ( + args?: Partial<{ + fetchApi: FetchApi; + discoveryApi: DiscoveryApi; + errorApi: ErrorApi; + namespace?: string; + }>, + ): StorageApi => { + return UserSettingsStorage.create({ + errorApi: mockErrorApi, + fetchApi: new MockFetchApi(), + discoveryApi: mockDiscoveryApi, + identityApi: mockIdentityApiFallback as IdentityApi, + ...args, + }); + }; + afterEach(() => { jest.resetAllMocks(); }); @@ -124,6 +145,33 @@ describe('Persistent Storage API', () => { await storage.set('my-key', dummyValue); }); + it('should fallback set when user not logged in', async () => { + const storage = createPersistentStorageFallback(); + + const selectedKeyNextHandler = jest.fn(); + const dummyValue = 'my-value'; + + await new Promise(resolve => { + storage.observe$('my-key').subscribe({ + next: snapshot => { + selectedKeyNextHandler(snapshot); + if (snapshot.presence === 'present') { + resolve(); + } + }, + }); + + storage.set('my-key', dummyValue); + }); + + expect(selectedKeyNextHandler).toHaveBeenCalledTimes(1); + expect(selectedKeyNextHandler).toHaveBeenCalledWith({ + key: 'my-key', + value: dummyValue, + presence: 'present', + }); + }); + it('should subscribe to key changes when setting a new value', async () => { const storage = createPersistentStorage();