src/__tests__/paginator.unit.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1

Size

Lines of Code 262
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 209
mnd 0
bc 0
fnc 5
dl 0
loc 262
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0
1
import { expect, describe, it } from '@jest/globals';
2
import Paginator from '../paginator.js';
3
4
const TestCases = [
5
    {
6
        description: 'It should handle an empty array',
7
        items: [],
8
        page: 0,
9
        size: 2,
10
        url: '/',
11
        count: null,
12
        expectedResult: {
13
            _links: {
14
                self: '/?size=2&page=0',
15
                prev: null,
16
                next: null,
17
            },
18
            count: 0,
19
            size: 2,
20
            page: 0,
21
            pages: 0,
22
            result: [],
23
        },
24
    },
25
    {
26
        description: 'It should return the first page with 2 items',
27
        items: [1, 2, 3, 4, 5],
28
        page: 0,
29
        size: 2,
30
        url: '/',
31
        count: null,
32
        expectedResult: {
33
            _links: {
34
                self: '/?size=2&page=0',
35
                prev: null,
36
                next: '/?size=2&page=1',
37
            },
38
            count: 5,
39
            size: 2,
40
            page: 0,
41
            pages: 3,
42
            result: [1, 2],
43
        },
44
    },
45
    {
46
        description: 'It should return the second page with 2 items',
47
        items: [1, 2, 3, 4, 5],
48
        page: 1,
49
        size: 2,
50
        url: '/',
51
        count: null,
52
        expectedResult: {
53
            _links: {
54
                self: '/?size=2&page=1',
55
                prev: '/?size=2&page=0',
56
                next: '/?size=2&page=2',
57
            },
58
            count: 5,
59
            size: 2,
60
            page: 1,
61
            pages: 3,
62
            result: [3, 4],
63
        },
64
    },
65
    {
66
        description: 'It should return the last page with 1 item',
67
        items: [1, 2, 3, 4, 5],
68
        page: 2,
69
        size: 2,
70
        url: '/',
71
        count: null,
72
        expectedResult: {
73
            _links: {
74
                self: '/?size=2&page=2',
75
                prev: '/?size=2&page=1',
76
                next: null,
77
            },
78
            count: 5,
79
            size: 2,
80
            page: 2,
81
            pages: 3,
82
            result: [5],
83
        },
84
    },
85
    {
86
        description:
87
            'It should return the second page with 2 items and no next',
88
        items: [1, 2, 3, 4],
89
        page: 1,
90
        size: 2,
91
        url: '/',
92
        count: null,
93
        expectedResult: {
94
            _links: {
95
                self: '/?size=2&page=1',
96
                prev: '/?size=2&page=0',
97
                next: null,
98
            },
99
            count: 4,
100
            size: 2,
101
            page: 1,
102
            pages: 2,
103
            result: [3, 4],
104
        },
105
    },
106
    {
107
        description: 'It should return null for prev and next',
108
        items: [1],
109
        page: 0,
110
        size: 2,
111
        url: '/',
112
        count: null,
113
        expectedResult: {
114
            _links: {
115
                self: '/?size=2&page=0',
116
                prev: null,
117
                next: null,
118
            },
119
            count: 1,
120
            size: 2,
121
            page: 0,
122
            pages: 1,
123
            result: [1],
124
        },
125
    },
126
    {
127
        description: 'It should return null for prev and next and self',
128
        items: [1],
129
        page: 0,
130
        size: 2,
131
        url: null,
132
        count: null,
133
        expectedResult: {
134
            _links: {
135
                self: null,
136
                prev: null,
137
                next: null,
138
            },
139
            count: 1,
140
            size: 2,
141
            page: 0,
142
            pages: 1,
143
            result: [1],
144
        },
145
    },
146
    {
147
        description: 'It should use page 0 and size 10 by default',
148
        items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
149
        page: null,
150
        size: null,
151
        url: '/?test=true',
152
        count: null,
153
        expectedResult: {
154
            _links: {
155
                self: '/?test=true&size=10&page=0',
156
                prev: null,
157
                next: '/?test=true&size=10&page=1',
158
            },
159
            count: 11,
160
            size: 10,
161
            page: 0,
162
            pages: 2,
163
            result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
164
        },
165
    },
166
    {
167
        description: 'It should use a custom count',
168
        items: [3, 4],
169
        page: 2,
170
        size: 2,
171
        url: '/?test=true',
172
        count: 20,
173
        expectedResult: {
174
            _links: {
175
                self: '/?test=true&size=2&page=2',
176
                prev: '/?test=true&size=2&page=1',
177
                next: '/?test=true&size=2&page=3',
178
            },
179
            count: 20,
180
            size: 2,
181
            page: 2,
182
            pages: 10,
183
            result: [3, 4],
184
        },
185
    },
186
];
187
188
describe.each(TestCases)(
189
    'Paginator helper',
190
    ({ description, items, page, size, url, count, expectedResult }) => {
191
        it(description, () => {
192
            const results = Paginator({
193
                items,
194
                page,
195
                size,
196
                url,
197
                count,
198
            });
199
200
            expect(results.data).toEqual(expectedResult);
201
        });
202
    }
203
);
204
205
const ErrorTestCases = [
206
    {
207
        description: 'It should throw an error if the items arent an array',
208
        props: {
209
            items: null,
210
        },
211
        expectedResult: 'items is not an array',
212
    },
213
    {
214
        description: 'It should throw an error if the items arent an array',
215
        props: {
216
            items: 1,
217
        },
218
        expectedResult: 'items is not an array',
219
    },
220
    {
221
        description: 'It should throw an error if the page is not a number',
222
        props: {
223
            items: [],
224
            page: 'a',
225
        },
226
        expectedResult: 'page is not a number',
227
    },
228
    {
229
        description:
230
            'It should throw an error if the page number is to high or low',
231
        props: {
232
            items: [],
233
            page: 2,
234
        },
235
        expectedResult: 'page is not a valid number',
236
    },
237
    {
238
        description: 'It should throw an error if the size is not a number',
239
        props: {
240
            items: [],
241
            size: 'a',
242
        },
243
        expectedResult: 'size is not a valid positive number',
244
    },
245
    {
246
        description: 'It should throw an error if the url is not a string',
247
        props: {
248
            items: [],
249
            url: 1,
250
        },
251
        expectedResult: 'url is not a valid string',
252
    },
253
];
254
255
describe.each(ErrorTestCases)(
256
    'test paginator validation',
257
    ({ description, props, expectedResult }) => {
258
        it(description, () => {
259
            expect(() => Paginator(props)).toThrowError(expectedResult);
260
        });
261
    }
262
);
263