Add tests for the fallback

Signed-off-by: ivgo <ivgo@spreadgroup.com>
This commit is contained in:
ivgo
2022-09-29 13:24:37 +02:00
parent 44c9a95dcf
commit 63a0f35835
@@ -38,6 +38,10 @@ describe('Persistent Storage API', () => {
const mockIdentityApi: Partial<IdentityApi> = {
getCredentials: async () => ({ token: 'a-token' }),
};
const mockIdentityApiFallback: Partial<IdentityApi> = {
// 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<void>(resolve => {
storage.observe$<typeof dummyValue>('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();