1
|
|
|
import sinon from 'sinon'; |
2
|
|
|
import { last } from 'ramda'; |
3
|
|
|
import { ServersService } from '../../../src/servers/services/ServersService'; |
4
|
|
|
|
5
|
|
|
describe('ServersService', () => { |
6
|
|
|
const servers = { |
7
|
|
|
abc123: { id: 'abc123' }, |
8
|
|
|
def456: { id: 'def456' }, |
9
|
|
|
}; |
10
|
|
|
const createStorageMock = (returnValue) => ({ |
11
|
|
|
set: sinon.fake(), |
12
|
|
|
get: sinon.fake.returns(returnValue), |
13
|
|
|
}); |
14
|
|
|
|
15
|
|
|
describe('listServers', () => { |
16
|
|
|
it('returns an empty object when servers are not found in storage', () => { |
17
|
|
|
const storageMock = createStorageMock(); |
18
|
|
|
const service = new ServersService(storageMock); |
19
|
|
|
|
20
|
|
|
const result = service.listServers(); |
21
|
|
|
|
22
|
|
|
expect(result).toEqual({}); |
23
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
24
|
|
|
expect(storageMock.set.callCount).toEqual(0); |
25
|
|
|
}); |
26
|
|
|
|
27
|
|
|
it('returns value from storage when found', () => { |
28
|
|
|
const storageMock = createStorageMock(servers); |
29
|
|
|
const service = new ServersService(storageMock); |
30
|
|
|
|
31
|
|
|
const result = service.listServers(); |
32
|
|
|
|
33
|
|
|
expect(result).toEqual(servers); |
34
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
35
|
|
|
expect(storageMock.set.callCount).toEqual(0); |
36
|
|
|
}); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
describe('findServerById', () => { |
40
|
|
|
it('returns undefined when requested server is not found', () => { |
41
|
|
|
const storageMock = createStorageMock(servers); |
42
|
|
|
const service = new ServersService(storageMock); |
43
|
|
|
|
44
|
|
|
const result = service.findServerById('ghi789'); |
45
|
|
|
|
46
|
|
|
expect(result).toBeUndefined(); |
47
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
48
|
|
|
expect(storageMock.set.callCount).toEqual(0); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
it('returns server from list when found', () => { |
52
|
|
|
const storageMock = createStorageMock(servers); |
53
|
|
|
const service = new ServersService(storageMock); |
54
|
|
|
|
55
|
|
|
const result = service.findServerById('abc123'); |
56
|
|
|
|
57
|
|
|
expect(result).toEqual({ id: 'abc123' }); |
58
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
59
|
|
|
expect(storageMock.set.callCount).toEqual(0); |
60
|
|
|
}); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
describe('createServer', () => { |
64
|
|
|
it('adds one server to the list', () => { |
65
|
|
|
const storageMock = createStorageMock(servers); |
66
|
|
|
const service = new ServersService(storageMock); |
67
|
|
|
|
68
|
|
|
service.createServer({ id: 'ghi789' }); |
69
|
|
|
|
70
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
71
|
|
|
expect(storageMock.set.callCount).toEqual(1); |
72
|
|
|
expect(last(storageMock.set.lastCall.args)).toEqual({ |
73
|
|
|
abc123: { id: 'abc123' }, |
74
|
|
|
def456: { id: 'def456' }, |
75
|
|
|
ghi789: { id: 'ghi789' }, |
76
|
|
|
}); |
77
|
|
|
}); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
describe('createServers', () => { |
81
|
|
|
it('adds multiple servers to the list', () => { |
82
|
|
|
const storageMock = createStorageMock(servers); |
83
|
|
|
const service = new ServersService(storageMock); |
84
|
|
|
|
85
|
|
|
service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]); |
86
|
|
|
|
87
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
88
|
|
|
expect(storageMock.set.callCount).toEqual(1); |
89
|
|
|
expect(last(storageMock.set.lastCall.args)).toEqual({ |
90
|
|
|
abc123: { id: 'abc123' }, |
91
|
|
|
def456: { id: 'def456' }, |
92
|
|
|
ghi789: { id: 'ghi789' }, |
93
|
|
|
jkl123: { id: 'jkl123' }, |
94
|
|
|
}); |
95
|
|
|
}); |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
describe('deleteServer', () => { |
99
|
|
|
it('removes one server from the list', () => { |
100
|
|
|
const storageMock = createStorageMock(servers); |
101
|
|
|
const service = new ServersService(storageMock); |
102
|
|
|
|
103
|
|
|
service.deleteServer({ id: 'abc123' }); |
104
|
|
|
|
105
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
106
|
|
|
expect(storageMock.set.callCount).toEqual(1); |
107
|
|
|
expect(last(storageMock.set.lastCall.args)).toEqual({ |
108
|
|
|
def456: { id: 'def456' }, |
109
|
|
|
}); |
110
|
|
|
}); |
111
|
|
|
}); |
112
|
|
|
}); |
113
|
|
|
|