1
|
|
|
import createEnhancer from '../enhancer-creator'; |
2
|
|
|
|
3
|
|
|
describe('createEnhancer can create enhancer', () => { |
4
|
|
|
describe('when input function', () => { |
5
|
|
|
it('output enhanced function with original name', () => { |
6
|
|
|
const orignal = () => {}; |
7
|
|
|
const enhancement = input => () => input(); |
8
|
|
|
const enhancer = createEnhancer(enhancement); |
9
|
|
|
const enhanced = enhancer(orignal); |
10
|
|
|
expect(enhanced.name).toBe('orignal'); |
11
|
|
|
}); |
12
|
|
|
|
13
|
|
|
it('output enhanced function invoking enhancement function and orignal function', () => { |
14
|
|
|
const enhancementSideEffect = jest.fn(); |
|
|
|
|
15
|
|
|
const orignal = jest.fn(); |
16
|
|
|
const enhancer = createEnhancer(input => () => { |
17
|
|
|
enhancementSideEffect(); |
18
|
|
|
return input(); |
19
|
|
|
}); |
20
|
|
|
const enhanced = enhancer(orignal); |
21
|
|
|
enhanced(); |
22
|
|
|
expect(orignal.mock.calls).toHaveLength(1); |
23
|
|
|
expect(enhancementSideEffect.mock.calls).toHaveLength(1); |
24
|
|
|
}); |
25
|
|
|
}); |
26
|
|
|
|
27
|
|
|
describe('when input function bundle', () => { |
28
|
|
|
it('output function bundle of functions name as method names', () => { |
29
|
|
|
const functionBundle = { |
30
|
|
|
methodA: () => {}, |
31
|
|
|
methodB: () => {}, |
32
|
|
|
}; |
33
|
|
|
const enhancer = createEnhancer(input => () => input()); |
34
|
|
|
const enhanced = enhancer(functionBundle); |
35
|
|
|
expect(enhanced.methodA.name).toBe('methodA'); |
36
|
|
|
expect(enhanced.methodB.name).toBe('methodB'); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
it('output function bundle of functions invoking enhancement function and origial functions', () => { |
40
|
|
|
const enhancementSideEffect = jest.fn(); |
|
|
|
|
41
|
|
|
const methodA = jest.fn(); |
42
|
|
|
const methodB = jest.fn(); |
43
|
|
|
const functionBundle = { |
44
|
|
|
methodA, |
45
|
|
|
methodB, |
46
|
|
|
}; |
47
|
|
|
const enhancer = createEnhancer(input => () => { |
48
|
|
|
enhancementSideEffect(); |
49
|
|
|
return input(); |
50
|
|
|
}); |
51
|
|
|
const enhanced = enhancer(functionBundle); |
52
|
|
|
enhanced.methodA(); |
53
|
|
|
expect(methodA.mock.calls).toHaveLength(1); |
54
|
|
|
expect(enhancementSideEffect.mock.calls).toHaveLength(1); |
55
|
|
|
enhanced.methodB(); |
56
|
|
|
expect(methodB.mock.calls).toHaveLength(1); |
57
|
|
|
expect(enhancementSideEffect.mock.calls).toHaveLength(2); |
58
|
|
|
}); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
it('when input is invalid throws error', () => { |
62
|
|
|
const enhancer = createEnhancer(input => () => input()); |
63
|
|
|
const doEnhanceStringInput = () => enhancer('test'); |
64
|
|
|
expect(doEnhanceStringInput).toThrowErrorMatchingSnapshot(); |
65
|
|
|
|
66
|
|
|
const doEnhanceInvalidObject = () => |
67
|
|
|
enhancer({ |
68
|
|
|
foo: 'bar', |
69
|
|
|
}); |
70
|
|
|
expect(doEnhanceInvalidObject).toThrowErrorMatchingSnapshot(); |
71
|
|
|
}); |
72
|
|
|
}); |
73
|
|
|
|
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.