Total Complexity | 8 |
Complexity/F | 1.33 |
Lines of Code | 49 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { assert } from 'chai'; |
||
2 | import { FunctionDecorator } from 'tests/entry'; |
||
3 | |||
4 | suite('Decorators: FunctionDecorator'); |
||
5 | |||
6 | function addOne(x) { |
||
7 | if (x < 0) throw new Error('Bad x'); |
||
|
|||
8 | |||
9 | return x + 1; |
||
10 | } |
||
11 | |||
12 | test('Positive: onSuccess', function () { |
||
13 | class Decorator extends FunctionDecorator { |
||
14 | onSuccess({ result }) { |
||
15 | return super.onSuccess({ result }) + 1; |
||
16 | } |
||
17 | } |
||
18 | |||
19 | const decorator = new Decorator({ config: {} }); |
||
20 | const addTwo = decorator.run(addOne); |
||
21 | |||
22 | assert.equal(addTwo(2), 4); |
||
23 | assert.equal(addTwo(5), 7); |
||
24 | }); |
||
25 | |||
26 | |||
27 | test('Negative: fail with onParams + onError', function () { |
||
28 | class Decorator extends FunctionDecorator { |
||
29 | onParams({ params }) { |
||
30 | return [ params[0] - 2 ]; |
||
31 | } |
||
32 | |||
33 | onError({ error }) { |
||
34 | error._handled = 1; |
||
35 | super.onError(error); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | const decorator = new Decorator({ config: {} }); |
||
40 | const delOne = decorator.run(addOne); |
||
41 | |||
42 | assert.equal(delOne(3), 2); |
||
43 | try { |
||
44 | delOne(1); |
||
45 | assert.fail('Expected to throw error'); |
||
46 | } catch (error) { |
||
47 | assert.equal(error.message, 'Bad x'); |
||
48 | } |
||
49 | }); |
||
50 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.