Passed
Branch main (d73254)
by Pieter Epeüs
24:57 queued 21:23
created

src/__tests__/response.unit.js   A

Complexity

Total Complexity 6
Complexity/F 1

Size

Lines of Code 129
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 90
mnd 0
bc 0
fnc 6
dl 0
loc 129
rs 10
bpm 0
cpm 1
noi 1
c 0
b 0
f 0
1
import { buildJsonResponse } from '../response';
2
3
const TestCases = [
4
    {
5
        description: 'Test a simple request',
6
        params: { body: {} },
7
        specification: {},
8
        expectedResult: {
9
            headers: {
10
                'Content-Type': 'application/json',
11
                'Cache-Control': 'no-store, max-age=0',
12
            },
13
            statusCode: 200,
14
            body: {},
15
        },
16
    },
17
    {
18
        description: 'It should overwrite the status code',
19
        params: {
20
            statusCode: 204,
21
            body: {},
22
        },
23
        specification: {
24
            info: {
25
                version: '1.2.3',
26
            },
27
        },
28
        expectedResult: {
29
            headers: {
30
                'Content-Type': 'application/json',
31
                'Cache-Control': 'no-store, max-age=0',
32
            },
33
            statusCode: 204,
34
            body: {},
35
        },
36
    },
37
    {
38
        description: 'It should add the extra header',
39
        params: {
40
            statusCode: 201,
41
            headers: {
42
                test: 'ok',
43
            },
44
            body: {},
45
        },
46
        specification: {},
47
        expectedResult: {
48
            headers: {
49
                'Content-Type': 'application/json',
50
                'Cache-Control': 'no-store, max-age=0',
51
                test: 'ok',
52
            },
53
            statusCode: 201,
54
            body: {},
55
        },
56
    },
57
    {
58
        description: 'It should overwrite the default headers',
59
        params: {
60
            statusCode: 201,
61
            headers: {
62
                'Content-Type': 'text/html',
63
                'Cache-Control': 'private',
64
                test: 'ok',
65
            },
66
            body: {
67
                example: 42,
68
            },
69
        },
70
        specification: {},
71
        expectedResult: {
72
            headers: {
73
                'Content-Type': 'text/html',
74
                'Cache-Control': 'private',
75
                test: 'ok',
76
            },
77
            statusCode: 201,
78
            body: {
79
                example: 42,
80
            },
81
        },
82
    },
83
];
84
85
describe.each(TestCases)(
0 ignored issues
show
Bug introduced by
The variable describe seems to be never declared. If this is a global, consider adding a /** global: describe */ 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...
86
    'Response entity',
87
    ({ description, params, specification, expectedResult }) => {
88
        it(description, () => {
89
            expect(buildJsonResponse(params, specification)).toEqual(
90
                expectedResult
91
            );
92
        });
93
    }
94
);
95
96
describe('Response without a specification', () => {
97
    it('It should generate a response body if no body is send', () => {
98
        const response = buildJsonResponse({});
99
        expect(response.headers).toEqual({
100
            'Content-Type': 'application/json',
101
            'Cache-Control': 'no-store, max-age=0',
102
        });
103
        expect(response.statusCode).toEqual(200);
104
        expect(response.body.status).toEqual(true);
105
        expect(response.body.version).toEqual('unknown');
106
        expect(response.body.message).toEqual('ok');
107
    });
108
});
109
110
describe('Response with a specification', () => {
111
    it('It should generate a response body if no body is send', () => {
112
        const response = buildJsonResponse(
113
            {},
114
            {
115
                info: {
116
                    version: '1.2.3',
117
                },
118
            }
119
        );
120
        expect(response.headers).toEqual({
121
            'Content-Type': 'application/json',
122
            'Cache-Control': 'no-store, max-age=0',
123
        });
124
        expect(response.statusCode).toEqual(200);
125
        expect(response.body.status).toEqual(true);
126
        expect(response.body.version).toEqual('1.2.3');
127
        expect(response.body.message).toEqual('ok');
128
    });
129
});
130