1
|
|
|
import * as sinon from 'sinon'; |
2
|
|
|
import ColorGenerator from '../../../src/utils/services/ColorGenerator'; |
3
|
|
|
|
4
|
|
|
describe('ColorGenerator', () => { |
5
|
|
|
let colorGenerator; |
6
|
|
|
const storageMock = { |
7
|
|
|
set: sinon.fake(), |
8
|
|
|
get: sinon.fake.returns(undefined), |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
beforeEach(() => { |
12
|
|
|
storageMock.set.resetHistory(); |
13
|
|
|
storageMock.get.resetHistory(); |
14
|
|
|
|
15
|
|
|
colorGenerator = new ColorGenerator(storageMock); |
16
|
|
|
}); |
17
|
|
|
|
18
|
|
|
it('sets a color in the storage and makes it available after that', () => { |
19
|
|
|
const color = '#ff0000'; |
20
|
|
|
|
21
|
|
|
colorGenerator.setColorForKey('foo', color); |
22
|
|
|
|
23
|
|
|
expect(colorGenerator.getColorForKey('foo')).toEqual(color); |
24
|
|
|
expect(storageMock.set.callCount).toEqual(1); |
25
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
26
|
|
|
}); |
27
|
|
|
|
28
|
|
|
it('generates a random color when none is available for requested key', () => { |
29
|
|
|
expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/)); |
|
|
|
|
30
|
|
|
expect(storageMock.set.callCount).toEqual(1); |
31
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
it('trims and lower cases keys before trying to match', () => { |
35
|
|
|
const color = '#ff0000'; |
36
|
|
|
|
37
|
|
|
colorGenerator.setColorForKey('foo', color); |
38
|
|
|
|
39
|
|
|
expect(colorGenerator.getColorForKey(' foo')).toEqual(color); |
40
|
|
|
expect(colorGenerator.getColorForKey('foO')).toEqual(color); |
41
|
|
|
expect(colorGenerator.getColorForKey('FoO')).toEqual(color); |
42
|
|
|
expect(colorGenerator.getColorForKey('FOO')).toEqual(color); |
43
|
|
|
expect(colorGenerator.getColorForKey('FOO ')).toEqual(color); |
44
|
|
|
expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color); |
45
|
|
|
expect(storageMock.set.callCount).toEqual(1); |
46
|
|
|
expect(storageMock.get.callCount).toEqual(1); |
47
|
|
|
}); |
48
|
|
|
}); |
49
|
|
|
|
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.