test/utils/services/Storage.test.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 1.11

Size

Lines of Code 46
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 26
mnd 1
bc 1
fnc 9
dl 0
loc 46
rs 10
bpm 0.1111
cpm 1.1111
noi 1
c 0
b 0
f 0
1
import Storage from '../../../src/utils/services/Storage';
2
3
describe('Storage', () => {
4
  const localStorageMock = {
5
    getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
    setItem: jest.fn(),
7
  };
8
  let storage;
9
10
  beforeEach(() => {
11
    localStorageMock.getItem.mockClear();
12
    localStorageMock.setItem.mockReset();
13
14
    storage = new Storage(localStorageMock);
15
  });
16
17
  describe('set', () => {
18
    it('writes an stringified representation of provided value in local storage', () => {
19
      const value = { bar: 'baz' };
20
21
      storage.set('foo', value);
22
23
      expect(localStorageMock.setItem).toHaveBeenCalledTimes(1);
24
      expect(localStorageMock.setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
25
    });
26
  });
27
28
  describe('get', () => {
29
    it('fetches item from local storage', () => {
30
      storage.get('foo');
31
      expect(localStorageMock.getItem).toHaveBeenCalledTimes(1);
32
    });
33
34
    it('returns parsed value when requested value is found in local storage', () => {
35
      const value = storage.get('foo');
36
37
      expect(value).toEqual({ foo: 'bar' });
38
    });
39
40
    it('returns undefined when requested value is not found in local storage', () => {
41
      const value = storage.get('bar');
42
43
      expect(value).toBeUndefined();
44
    });
45
  });
46
});
47