1
|
|
|
import SettingsService from '../../../src/settings/services/SettingsService'; |
2
|
|
|
|
3
|
|
|
describe('SettingsService', () => { |
4
|
|
|
const settings = { foo: 'bar' }; |
5
|
|
|
const createService = (withSettings = true) => { |
6
|
|
|
const storageMock = { |
7
|
|
|
set: jest.fn(), |
|
|
|
|
8
|
|
|
get: jest.fn(() => withSettings ? settings : undefined), |
9
|
|
|
}; |
10
|
|
|
const service = new SettingsService(storageMock); |
11
|
|
|
|
12
|
|
|
return [ service, storageMock ]; |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
afterEach(jest.resetAllMocks); |
|
|
|
|
16
|
|
|
|
17
|
|
|
describe('loadSettings', () => { |
18
|
|
|
it.each([ |
|
|
|
|
19
|
|
|
[ false, {}], |
20
|
|
|
[ true, settings ], |
21
|
|
|
])('returns result if found in storage', (withSettings, expectedResult) => { |
22
|
|
|
const [ service, storageMock ] = createService(withSettings); |
23
|
|
|
|
24
|
|
|
const result = service.loadSettings(); |
25
|
|
|
|
26
|
|
|
expect(result).toEqual(expectedResult); |
27
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1); |
28
|
|
|
expect(storageMock.set).not.toHaveBeenCalled(); |
29
|
|
|
}); |
30
|
|
|
}); |
31
|
|
|
|
32
|
|
|
describe('updateSettings', () => { |
33
|
|
|
it.each([ |
|
|
|
|
34
|
|
|
[ false, { hi: 'goodbye' }, { hi: 'goodbye' }], |
35
|
|
|
[ true, { hi: 'goodbye' }, { foo: 'bar', hi: 'goodbye' }], |
36
|
|
|
[ true, { foo: 'goodbye' }, { foo: 'goodbye' }], |
37
|
|
|
])('appends provided data to existing settings', (withSettings, settingsToUpdate, expectedResult) => { |
38
|
|
|
const [ service, storageMock ] = createService(withSettings); |
39
|
|
|
|
40
|
|
|
service.updateSettings(settingsToUpdate); |
41
|
|
|
|
42
|
|
|
expect(storageMock.get).toHaveBeenCalledTimes(1); |
43
|
|
|
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), expectedResult); |
|
|
|
|
44
|
|
|
}); |
45
|
|
|
}); |
46
|
|
|
}); |
47
|
|
|
|
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.