From 7847d68948991a0d8220e209d15577dd787e030e Mon Sep 17 00:00:00 2001 From: gernsdorfer Date: Mon, 18 Dec 2023 11:06:38 +0100 Subject: [PATCH] support missing window --- .../src/plugins/client-storage.plugin.spec.ts | 23 +++++++++++-------- .../src/plugins/client-storage.plugin.ts | 16 +++++++++---- libs/store/src/plugins/index.ts | 5 +++- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/libs/store/src/plugins/client-storage.plugin.spec.ts b/libs/store/src/plugins/client-storage.plugin.spec.ts index dd2fd6f1..f81b2c1e 100644 --- a/libs/store/src/plugins/client-storage.plugin.spec.ts +++ b/libs/store/src/plugins/client-storage.plugin.spec.ts @@ -1,4 +1,5 @@ import { + getWindow, localStoragePlugin, sessionStoragePlugin, } from './client-storage.plugin'; @@ -16,7 +17,11 @@ describe('sessionStorageHandlerForStores', () => { beforeEach(() => { storage = window.sessionStorage; }); - + describe('getWindow', () => { + it('should return undefined for missing window', () => { + expect(getWindow()).toBeUndefined(); + }); + }); describe('getDefaultState', () => { describe('load data from storage', () => { it('should return default state', () => { @@ -25,18 +30,18 @@ describe('sessionStorageHandlerForStores', () => { spyGetItem.and.returnValue(null); expect( - sessionStoragePlugin.getDefaultState('testStore1') + sessionStoragePlugin.getDefaultState('testStore1'), ).toEqual(undefined); }); it('should return state from storage', () => { const spyGetItem = getSpyGetItem(storage); spyGetItem.and.returnValue( - JSON.stringify({ myState: 'testValue' }) + JSON.stringify({ myState: 'testValue' }), ); expect( - sessionStoragePlugin.getDefaultState('testStore1') + sessionStoragePlugin.getDefaultState('testStore1'), ).toEqual({ myState: 'testValue' }); }); }); @@ -51,7 +56,7 @@ describe('sessionStorageHandlerForStores', () => { expect(spySetItem).toHaveBeenCalledWith( 'testStore1', - JSON.stringify({ myState: 'testValue' }) + JSON.stringify({ myState: 'testValue' }), ); }); }); @@ -71,18 +76,18 @@ describe('localStorageHandlerForStores', () => { spyGetItem.and.returnValue(null); expect(localStoragePlugin.getDefaultState('testStore1')).toEqual( - undefined + undefined, ); }); it('should return state from storage', () => { const spyGetItem = getSpyGetItem(storage); spyGetItem.and.returnValue( - JSON.stringify({ myState: 'testValue' }) + JSON.stringify({ myState: 'testValue' }), ); expect( - localStoragePlugin.getDefaultState('testStore1') + localStoragePlugin.getDefaultState('testStore1'), ).toEqual({ myState: 'testValue', }); @@ -97,7 +102,7 @@ describe('localStorageHandlerForStores', () => { expect(spySetItem).toHaveBeenCalledWith( 'testStore1', - JSON.stringify({ isLoading: false }) + JSON.stringify({ isLoading: false }), ); }); }); diff --git a/libs/store/src/plugins/client-storage.plugin.ts b/libs/store/src/plugins/client-storage.plugin.ts index 4d0b5d43..5a680912 100644 --- a/libs/store/src/plugins/client-storage.plugin.ts +++ b/libs/store/src/plugins/client-storage.plugin.ts @@ -1,19 +1,25 @@ import { ClientStoragePlugin } from '../models'; class ClientStorage implements ClientStoragePlugin { - constructor(private store: Storage) {} + constructor(private store?: Storage) {} getDefaultState(storeName: string): STATE | undefined { - const stateFromStorage = this.store.getItem(storeName); + const stateFromStorage = this.store?.getItem(storeName); if (!stateFromStorage) return; return JSON.parse(stateFromStorage); } setStateToStorage(storeName: string, state: STATE) { - this.store.setItem(storeName, JSON.stringify(state)); + this.store?.setItem(storeName, JSON.stringify(state)); } } +export const getWindow = (currentWindow?: Window): Window | undefined => + typeof currentWindow === 'object' ? currentWindow : undefined; -export const sessionStoragePlugin = new ClientStorage(window.sessionStorage); +export const sessionStoragePlugin = new ClientStorage( + getWindow(window)?.sessionStorage, +); -export const localStoragePlugin = new ClientStorage(window.localStorage); +export const localStoragePlugin = new ClientStorage( + getWindow(window)?.localStorage, +); diff --git a/libs/store/src/plugins/index.ts b/libs/store/src/plugins/index.ts index 6d35117d..630fe931 100644 --- a/libs/store/src/plugins/index.ts +++ b/libs/store/src/plugins/index.ts @@ -1 +1,4 @@ -export * from './client-storage.plugin'; +export { + localStoragePlugin, + sessionStoragePlugin, +} from './client-storage.plugin';