shlink-web-client/test/utils/services/Storage.test.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-12-19 22:43:55 +03:00
import Storage from '../../../src/utils/services/Storage';
describe('Storage', () => {
const localStorageMock = {
2019-04-19 13:41:59 +03:00
getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
setItem: jest.fn(),
2018-12-19 22:43:55 +03:00
};
let storage;
beforeEach(() => {
2019-04-19 13:41:59 +03:00
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockReset();
2018-12-19 22:43:55 +03:00
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);
2019-04-19 13:41:59 +03:00
expect(localStorageMock.setItem).toHaveBeenCalledTimes(1);
expect(localStorageMock.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');
2019-04-19 13:41:59 +03:00
expect(localStorageMock.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();
});
});
});