Completed
Push — master ( 158ed8...34f194 )
by Alejandro
04:53 queued 02:19
created

test/servers/services/ServersService.test.js   A

Complexity

Total Complexity 15
Complexity/F 1

Size

Lines of Code 110
Function Count 15

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 70
mnd 0
bc 0
fnc 15
dl 0
loc 110
rs 10
bpm 0
cpm 1
noi 4
c 0
b 0
f 0
1
import ServersService from '../../../src/servers/services/ServersService';
2
3
describe('ServersService', () => {
4
  const servers = {
5
    abc123: { id: 'abc123' },
6
    def456: { id: 'def456' },
7
  };
8
  const createStorageMock = (returnValue) => ({
9
    set: jest.fn(),
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...
10
    get: jest.fn(() => returnValue),
11
  });
12
13
  describe('listServers', () => {
14
    it('returns an empty object when servers are not found in storage', () => {
15
      const storageMock = createStorageMock();
16
      const service = new ServersService(storageMock);
17
18
      const result = service.listServers();
19
20
      expect(result).toEqual({});
21
      expect(storageMock.get).toHaveBeenCalledTimes(1);
22
      expect(storageMock.set).not.toHaveBeenCalled();
23
    });
24
25
    it('returns value from storage when found', () => {
26
      const storageMock = createStorageMock(servers);
27
      const service = new ServersService(storageMock);
28
29
      const result = service.listServers();
30
31
      expect(result).toEqual(servers);
32
      expect(storageMock.get).toHaveBeenCalledTimes(1);
33
      expect(storageMock.set).not.toHaveBeenCalled();
34
    });
35
  });
36
37
  describe('findServerById', () => {
38
    it('returns undefined when requested server is not found', () => {
39
      const storageMock = createStorageMock(servers);
40
      const service = new ServersService(storageMock);
41
42
      const result = service.findServerById('ghi789');
43
44
      expect(result).toBeUndefined();
45
      expect(storageMock.get).toHaveBeenCalledTimes(1);
46
      expect(storageMock.set).not.toHaveBeenCalled();
47
    });
48
49
    it('returns server from list when found', () => {
50
      const storageMock = createStorageMock(servers);
51
      const service = new ServersService(storageMock);
52
53
      const result = service.findServerById('abc123');
54
55
      expect(result).toEqual({ id: 'abc123' });
56
      expect(storageMock.get).toHaveBeenCalledTimes(1);
57
      expect(storageMock.set).not.toHaveBeenCalled();
58
    });
59
  });
60
61
  describe('createServer', () => {
62
    it('adds one server to the list', () => {
63
      const storageMock = createStorageMock(servers);
64
      const service = new ServersService(storageMock);
65
66
      service.createServer({ id: 'ghi789' });
67
68
      expect(storageMock.get).toHaveBeenCalledTimes(1);
69
      expect(storageMock.set).toHaveBeenCalledTimes(1);
70
      expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
0 ignored issues
show
Bug introduced by
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ 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...
71
        abc123: { id: 'abc123' },
72
        def456: { id: 'def456' },
73
        ghi789: { id: 'ghi789' },
74
      });
75
    });
76
  });
77
78
  describe('createServers', () => {
79
    it('adds multiple servers to the list', () => {
80
      const storageMock = createStorageMock(servers);
81
      const service = new ServersService(storageMock);
82
83
      service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]);
84
85
      expect(storageMock.get).toHaveBeenCalledTimes(1);
86
      expect(storageMock.set).toHaveBeenCalledTimes(1);
87
      expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
0 ignored issues
show
Bug introduced by
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ 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...
88
        abc123: { id: 'abc123' },
89
        def456: { id: 'def456' },
90
        ghi789: { id: 'ghi789' },
91
        jkl123: { id: 'jkl123' },
92
      });
93
    });
94
  });
95
96
  describe('deleteServer', () => {
97
    it('removes one server from the list', () => {
98
      const storageMock = createStorageMock(servers);
99
      const service = new ServersService(storageMock);
100
101
      service.deleteServer({ id: 'abc123' });
102
103
      expect(storageMock.get).toHaveBeenCalledTimes(1);
104
      expect(storageMock.set).toHaveBeenCalledTimes(1);
105
      expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
0 ignored issues
show
Bug introduced by
The variable expect seems to be never declared. If this is a global, consider adding a /** global: expect */ 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...
106
        def456: { id: 'def456' },
107
      });
108
    });
109
  });
110
});
111