Passed
Push — master ( 6a7f4c...05e4eb )
by Zhenyu
07:15 queued 05:37
created

enhancer-creator.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 1
dl 0
loc 87
rs 8.6296
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A enhancer-creator.js ➔ ... ➔ ??? 0 22 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
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('update function names in the original bundle methods, so that they can be consistent in enhancement function', () => {
40
			const enhancementSideEffect = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
41
			const functionBundle = {
42
				methodA: jest.fn(),
43
				methodB: jest.fn(),
44
			};
45
			const enhancer = createEnhancer(inputFunction => () => {
46
				enhancementSideEffect(inputFunction.name);
47
				return inputFunction();
48
			});
49
			const enhanced = enhancer(functionBundle);
50
			enhanced.methodA();
51
			expect(enhancementSideEffect.mock.calls).toMatchSnapshot();
52
			enhanced.methodB();
53
			expect(enhancementSideEffect.mock.calls).toMatchSnapshot();
54
		});
55
56
		it('output function bundle of functions invoking enhancement function and origial functions', () => {
57
			const enhancementSideEffect = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

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.

Loading history...
58
			const methodA = jest.fn();
59
			const methodB = jest.fn();
60
			const functionBundle = {
61
				methodA,
62
				methodB,
63
			};
64
			const enhancer = createEnhancer(input => () => {
65
				enhancementSideEffect();
66
				return input();
67
			});
68
			const enhanced = enhancer(functionBundle);
69
			enhanced.methodA();
70
			expect(methodA.mock.calls).toHaveLength(1);
71
			expect(enhancementSideEffect.mock.calls).toHaveLength(1);
72
			enhanced.methodB();
73
			expect(methodB.mock.calls).toHaveLength(1);
74
			expect(enhancementSideEffect.mock.calls).toHaveLength(2);
75
		});
76
	});
77
78
	it('when input is invalid throws error', () => {
79
		const enhancer = createEnhancer(input => () => input());
80
		const doEnhanceStringInput = () => enhancer('test');
81
		expect(doEnhanceStringInput).toThrowErrorMatchingSnapshot();
82
83
		const doEnhanceInvalidObject = () =>
84
			enhancer({
85
				foo: 'bar',
86
			});
87
		expect(doEnhanceInvalidObject).toThrowErrorMatchingSnapshot();
88
	});
89
});
90