Skip to content

Commit

Permalink
support missing window
Browse files Browse the repository at this point in the history
  • Loading branch information
gernsdorfer authored and gernsdorfer committed Dec 18, 2023
1 parent b4fa5f2 commit 7847d68
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
23 changes: 14 additions & 9 deletions libs/store/src/plugins/client-storage.plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getWindow,
localStoragePlugin,
sessionStoragePlugin,
} from './client-storage.plugin';
Expand All @@ -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', () => {
Expand All @@ -25,18 +30,18 @@ describe('sessionStorageHandlerForStores', () => {
spyGetItem.and.returnValue(null);

expect(
sessionStoragePlugin.getDefaultState<MyState>('testStore1')
sessionStoragePlugin.getDefaultState<MyState>('testStore1'),
).toEqual(undefined);
});

it('should return state from storage', () => {
const spyGetItem = getSpyGetItem(storage);
spyGetItem.and.returnValue(
JSON.stringify(<MyState>{ myState: 'testValue' })
JSON.stringify(<MyState>{ myState: 'testValue' }),
);

expect(
sessionStoragePlugin.getDefaultState<MyState>('testStore1')
sessionStoragePlugin.getDefaultState<MyState>('testStore1'),
).toEqual({ myState: 'testValue' });
});
});
Expand All @@ -51,7 +56,7 @@ describe('sessionStorageHandlerForStores', () => {

expect(spySetItem).toHaveBeenCalledWith(
'testStore1',
JSON.stringify(<MyState>{ myState: 'testValue' })
JSON.stringify(<MyState>{ myState: 'testValue' }),
);
});
});
Expand All @@ -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>{ myState: 'testValue' })
JSON.stringify(<MyState>{ myState: 'testValue' }),
);

expect(
localStoragePlugin.getDefaultState<MyState>('testStore1')
localStoragePlugin.getDefaultState<MyState>('testStore1'),
).toEqual({
myState: 'testValue',
});
Expand All @@ -97,7 +102,7 @@ describe('localStorageHandlerForStores', () => {

expect(spySetItem).toHaveBeenCalledWith(
'testStore1',
JSON.stringify({ isLoading: false })
JSON.stringify({ isLoading: false }),
);
});
});
Expand Down
16 changes: 11 additions & 5 deletions libs/store/src/plugins/client-storage.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { ClientStoragePlugin } from '../models';

class ClientStorage implements ClientStoragePlugin {
constructor(private store: Storage) {}
constructor(private store?: Storage) {}

getDefaultState<STATE>(storeName: string): STATE | undefined {
const stateFromStorage = this.store.getItem(storeName);
const stateFromStorage = this.store?.getItem(storeName);
if (!stateFromStorage) return;
return JSON.parse(stateFromStorage);
}

setStateToStorage<STATE>(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,
);
5 changes: 4 additions & 1 deletion libs/store/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './client-storage.plugin';
export {
localStoragePlugin,
sessionStoragePlugin,
} from './client-storage.plugin';

0 comments on commit 7847d68

Please sign in to comment.