shlink-web-client/test/utils/services/Storage.test.js
2019-04-19 12:41:59 +02:00

46 lines
1.3 KiB
JavaScript

import Storage from '../../../src/utils/services/Storage';
describe('Storage', () => {
const localStorageMock = {
getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
setItem: jest.fn(),
};
let storage;
beforeEach(() => {
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockReset();
storage = new Storage(localStorageMock);
});
describe('set', () => {
it('writes an stringified representation of provided value in local storage', () => {
const value = { bar: 'baz' };
storage.set('foo', value);
expect(localStorageMock.setItem).toHaveBeenCalledTimes(1);
expect(localStorageMock.setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
});
});
describe('get', () => {
it('fetches item from local storage', () => {
storage.get('foo');
expect(localStorageMock.getItem).toHaveBeenCalledTimes(1);
});
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();
});
});
});