Passed
Push — master ( 0b7311...da1821 )
by Dmytro
02:58
created

tests/helpers/decorators/FunctionDecorator.test.js   A

Complexity

Total Complexity 8
Complexity/F 1.33

Size

Lines of Code 49
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 29
mnd 2
bc 2
fnc 6
dl 0
loc 49
rs 10
bpm 0.3333
cpm 1.3333
noi 1
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Decorator.onParams 0 3 2
A Decorator.onSuccess 0 3 2
A FunctionDecorator.test.js ➔ addOne 0 5 2
A Decorator.onError 0 4 1
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');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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