2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { LocalStorage } from '../../../src/utils/services/LocalStorage';
|
2018-12-19 22:43:55 +03:00
|
|
|
|
2020-08-29 15:16:37 +03:00
|
|
|
describe('LocalStorage', () => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const getItem = jest.fn((key) => (key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null));
|
2020-08-29 19:51:03 +03:00
|
|
|
const setItem = jest.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const localStorageMock = fromPartial<Storage>({ getItem, setItem });
|
2020-08-29 19:51:03 +03:00
|
|
|
let storage: LocalStorage;
|
2018-12-19 22:43:55 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-08-29 19:51:03 +03:00
|
|
|
jest.clearAllMocks();
|
2020-08-29 15:16:37 +03:00
|
|
|
storage = new LocalStorage(localStorageMock);
|
2018-12-19 22:43:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('set', () => {
|
|
|
|
it('writes an stringified representation of provided value in local storage', () => {
|
|
|
|
const value = { bar: 'baz' };
|
|
|
|
|
|
|
|
storage.set('foo', value);
|
|
|
|
|
2020-08-29 19:51:03 +03:00
|
|
|
expect(setItem).toHaveBeenCalledTimes(1);
|
|
|
|
expect(setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
|
2018-12-19 22:43:55 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get', () => {
|
|
|
|
it('fetches item from local storage', () => {
|
|
|
|
storage.get('foo');
|
2020-08-29 19:51:03 +03:00
|
|
|
expect(getItem).toHaveBeenCalledTimes(1);
|
2018-12-19 22:43:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns parsed value when requested value is found in local storage', () => {
|
|
|
|
const value = storage.get('foo');
|
|
|
|
|
|
|
|
expect(value).toEqual({ foo: 'bar' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined when requested value is not found in local storage', () => {
|
|
|
|
const value = storage.get('bar');
|
|
|
|
|
|
|
|
expect(value).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|