Completed
Pull Request — master (#227)
by Alejandro
06:25
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 createService = (withServers = true) => {
9
    const storageMock = {
10
      set: jest.fn(),
11
      get: jest.fn(() => withServers ? servers : undefined),
12
    };
13
    const service = new ServersService(storageMock);
14
15
    return [ service, storageMock ];
16
  };
17
18
  describe('listServers', () => {
19
    it('returns an empty object when servers are not found in storage', () => {
20
      const [ service, storageMock ] = createService(false);
21
22
      const result = service.listServers();
23
24
      expect(result).toEqual({});
25
      expect(storageMock.get).toHaveBeenCalledTimes(1);
26
      expect(storageMock.set).not.toHaveBeenCalled();
27
    });
28
29
    it('returns value from storage when found', () => {
30
      const [ service, storageMock ] = createService();
31
32
      const result = service.listServers();
33
34
      expect(result).toEqual(servers);
35
      expect(storageMock.get).toHaveBeenCalledTimes(1);
36
      expect(storageMock.set).not.toHaveBeenCalled();
37
    });
38
  });
39
40
  describe('findServerById', () => {
41
    it('returns undefined when requested server is not found', () => {
42
      const [ service, storageMock ] = createService();
43
44
      const result = service.findServerById('ghi789');
45
46
      expect(result).toBeUndefined();
47
      expect(storageMock.get).toHaveBeenCalledTimes(1);
48
      expect(storageMock.set).not.toHaveBeenCalled();
49
    });
50
51
    it('returns server from list when found', () => {
52
      const [ service, storageMock ] = createService();
53
54
      const result = service.findServerById('abc123');
55
56
      expect(result).toEqual({ id: 'abc123' });
57
      expect(storageMock.get).toHaveBeenCalledTimes(1);
58
      expect(storageMock.set).not.toHaveBeenCalled();
59
    });
60
  });
61
62
  describe('createServer', () => {
63
    it('adds one server to the list', () => {
64
      const [ service, storageMock ] = createService();
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(), {
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 [ service, storageMock ] = createService();
81
82
      service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]);
83
84
      expect(storageMock.get).toHaveBeenCalledTimes(1);
85
      expect(storageMock.set).toHaveBeenCalledTimes(1);
86
      expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
87
        abc123: { id: 'abc123' },
88
        def456: { id: 'def456' },
89
        ghi789: { id: 'ghi789' },
90
        jkl123: { id: 'jkl123' },
91
      });
92
    });
93
  });
94
95
  describe('deleteServer', () => {
96
    it('removes one server from the list', () => {
97
      const [ service, storageMock ] = createService();
98
99
      service.deleteServer({ id: 'abc123' });
100
101
      expect(storageMock.get).toHaveBeenCalledTimes(1);
102
      expect(storageMock.set).toHaveBeenCalledTimes(1);
103
      expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
104
        def456: { id: 'def456' },
105
      });
106
    });
107
  });
108
109
  describe('editServer', () => {
110
    it('dos nothing is provided server does not exist', () => {
111
      const [ service, storageMock ] = createService();
112
113
      service.editServer('notFound', {});
114
115
      expect(storageMock.set).not.toHaveBeenCalled();
116
    });
117
118
    it('updates the list with provided server data', () => {
119
      const [ service, storageMock ] = createService();
120
      const serverData = { name: 'foo', apiKey: 'bar' };
121
122
      service.editServer('abc123', serverData);
123
124
      expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
125
        abc123: { id: 'abc123', ...serverData },
126
        def456: { id: 'def456' },
127
      });
128
    });
129
  });
130
});
131