Completed
Push — master ( 12cdda...e93082 )
by Alejandro
12s
created

Storage.test.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 27
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 47
rs 9.232

1 Function

Rating   Name   Duplication   Size   Complexity  
A Storage.test.js ➔ ... ➔ ??? 0 1 2
1
import * as sinon from 'sinon';
2
import Storage from '../../../src/utils/services/Storage';
3
4
describe('Storage', () => {
5
  const localStorageMock = {
6
    getItem: sinon.fake((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
7
    setItem: sinon.spy(),
8
  };
9
  let storage;
10
11
  beforeEach(() => {
12
    localStorageMock.getItem.resetHistory();
13
    localStorageMock.setItem.resetHistory();
14
15
    storage = new Storage(localStorageMock);
16
  });
17
18
  describe('set', () => {
19
    it('writes an stringified representation of provided value in local storage', () => {
20
      const value = { bar: 'baz' };
21
22
      storage.set('foo', value);
23
24
      expect(localStorageMock.setItem.callCount).toEqual(1);
25
      expect(localStorageMock.setItem.getCall(0).args).toEqual([
26
        'shlink.foo',
27
        JSON.stringify(value),
28
      ]);
29
    });
30
  });
31
32
  describe('get', () => {
33
    it('fetches item from local storage', () => {
34
      storage.get('foo');
35
      expect(localStorageMock.getItem.callCount).toEqual(1);
36
    });
37
38
    it('returns parsed value when requested value is found in local storage', () => {
39
      const value = storage.get('foo');
40
41
      expect(value).toEqual({ foo: 'bar' });
42
    });
43
44
    it('returns undefined when requested value is not found in local storage', () => {
45
      const value = storage.get('bar');
46
47
      expect(value).toBeUndefined();
48
    });
49
  });
50
});
51