1 | const test = require('ava')
|
2 | const validate = require('../src/validate')
|
3 |
|
4 | const paramNames = {
|
5 | perPage: 'per_page',
|
6 | page: 'page',
|
7 | total: 'total'
|
8 | }
|
9 |
|
10 | test('options are valid', (t) => {
|
11 | const specs = [
|
12 | {
|
13 | key: 'foo',
|
14 | perPage: 42
|
15 | },
|
16 |
|
17 | {
|
18 | key: 'foo',
|
19 | perPage: '42'
|
20 | },
|
21 |
|
22 | {
|
23 | key: 'foo',
|
24 | perPage: 42,
|
25 | total: 5
|
26 | }
|
27 | ]
|
28 |
|
29 | t.plan(specs.length)
|
30 |
|
31 | specs.forEach(spec => {
|
32 | t.truthy(validate.options(spec))
|
33 | })
|
34 | })
|
35 |
|
36 | test('options are not valid', (t) => {
|
37 | const specs = [
|
38 |
|
39 | {
|
40 | key: false,
|
41 | perPage: 42
|
42 | },
|
43 |
|
44 | {
|
45 | key: 0,
|
46 | perPage: 42
|
47 | },
|
48 |
|
49 | {
|
50 | key: 'foo',
|
51 | perPage: false
|
52 | },
|
53 |
|
54 | {
|
55 | key: 'foo',
|
56 | perPage: 'foo'
|
57 | },
|
58 |
|
59 | {
|
60 | key: 'foo',
|
61 | perPage: 42,
|
62 | total: 'foo'
|
63 | },
|
64 |
|
65 | {
|
66 | key: 'foo',
|
67 | perPage: 42,
|
68 | total: -1
|
69 | },
|
70 |
|
71 | {
|
72 | key: 'foo',
|
73 | perPage: 42,
|
74 | total: '5'
|
75 | }
|
76 | ]
|
77 |
|
78 | t.plan(specs.length)
|
79 |
|
80 | specs.forEach(spec => {
|
81 | t.falsy(validate.options(spec))
|
82 | })
|
83 | })
|
84 |
|
85 | test('query is valid | default values', (t) => {
|
86 | const options = {
|
87 | perPage: 42
|
88 | }
|
89 |
|
90 | const query = {}
|
91 |
|
92 | t.truthy(validate.query(query, options, paramNames))
|
93 | t.is(query.page, 1)
|
94 | t.is(query.per_page, 42)
|
95 | })
|
96 |
|
97 | test('query is valid | default values with custom param names', (t) => {
|
98 | const options = {
|
99 | perPage: 42
|
100 | }
|
101 |
|
102 | const customParamNames = {
|
103 | perPage: 'pageSize',
|
104 | page: 'currentPage',
|
105 | total: 'totalCount'
|
106 | }
|
107 |
|
108 | const query = {}
|
109 |
|
110 | t.truthy(validate.query(query, options, customParamNames))
|
111 | t.is(query.currentPage, 1)
|
112 | t.is(query.pageSize, 42)
|
113 | })
|
114 |
|
115 | test('query is valid | custom values', (t) => {
|
116 | const options = {
|
117 | perPage: 42
|
118 | }
|
119 |
|
120 | const query = {
|
121 | page: 2,
|
122 | per_page: 100
|
123 | }
|
124 |
|
125 | t.truthy(validate.query(query, options, paramNames), true)
|
126 | t.is(query.page, 2)
|
127 | t.is(query.per_page, 100)
|
128 | })
|
129 |
|
130 | test('query is valid | custom values with custom param names', (t) => {
|
131 | const options = {
|
132 | perPage: 42
|
133 | }
|
134 |
|
135 | const query = {
|
136 | currentPage: 2,
|
137 | pageSize: 100
|
138 | }
|
139 |
|
140 | const customParamNames = {
|
141 | perPage: 'pageSize',
|
142 | page: 'currentPage',
|
143 | total: 'totalCount'
|
144 | }
|
145 |
|
146 | t.truthy(validate.query(query, options, customParamNames))
|
147 | t.is(query.currentPage, 2)
|
148 | t.is(query.pageSize, 100)
|
149 | })
|
150 |
|
151 | test('query is valid | edge minimum cases', (t) => {
|
152 | const options = {
|
153 | perPage: 42
|
154 | }
|
155 |
|
156 | const query = {
|
157 | page: 1,
|
158 | per_page: 1
|
159 | }
|
160 |
|
161 | t.truthy(validate.query(query, options, paramNames))
|
162 | t.is(query.page, 1)
|
163 | t.is(query.per_page, 1)
|
164 | })
|
165 |
|
166 | test('query is valid | edge maximum cases', (t) => {
|
167 | const options = {
|
168 | perPage: 42
|
169 | }
|
170 |
|
171 | const query = {
|
172 | page: 1,
|
173 | per_page: 500
|
174 | }
|
175 |
|
176 | t.truthy(validate.query(query, options, paramNames))
|
177 | t.is(query.page, 1)
|
178 | t.is(query.per_page, 500)
|
179 | })
|
180 |
|
181 | test('query is valid | number-like strings', (t) => {
|
182 | const options = {
|
183 | perPage: 42
|
184 | }
|
185 |
|
186 | const query = {
|
187 | page: '1',
|
188 | per_page: '1'
|
189 | }
|
190 |
|
191 | t.truthy(validate.query(query, options, paramNames))
|
192 | t.is(query.page, 1)
|
193 | t.is(query.per_page, 1)
|
194 | })
|
195 |
|
196 | test('query is valid | page converted to default', (t) => {
|
197 | const options = {
|
198 | perPage: 42
|
199 | }
|
200 |
|
201 | const query = {
|
202 | page: 0,
|
203 | per_page: 1
|
204 | }
|
205 |
|
206 | t.truthy(validate.query(query, options, paramNames))
|
207 | t.is(query.page, 1)
|
208 | t.is(query.per_page, 1)
|
209 | })
|
210 |
|
211 | test('query is valid | per_page converted to default', (t) => {
|
212 | const options = {
|
213 | perPage: 42
|
214 | }
|
215 |
|
216 | const query = {
|
217 | page: 1,
|
218 | per_page: 0
|
219 | }
|
220 |
|
221 | t.truthy(validate.query(query, options, paramNames))
|
222 | t.is(query.page, 1)
|
223 | t.is(query.per_page, 42)
|
224 | })
|
225 |
|
226 | test('query is not valid | page is negative', (t) => {
|
227 | const options = {
|
228 | perPage: 42
|
229 | }
|
230 |
|
231 | const query = {
|
232 | page: -1,
|
233 | per_page: 1
|
234 | }
|
235 |
|
236 | t.falsy(validate.query(query, options, paramNames))
|
237 | t.is(query.page, -1)
|
238 | t.is(query.per_page, 1)
|
239 | })
|
240 |
|
241 | test('query is valid | page is boolean', (t) => {
|
242 | const options = {
|
243 | perPage: 42
|
244 | }
|
245 |
|
246 | const query = {
|
247 | page: false,
|
248 | per_page: 1
|
249 | }
|
250 |
|
251 | t.truthy(validate.query(query, options, paramNames))
|
252 | t.is(query.page, 1)
|
253 | t.is(query.per_page, 1)
|
254 | })
|
255 |
|
256 | test('query is valid | per_page is boolean', (t) => {
|
257 | const options = {
|
258 | perPage: 42
|
259 | }
|
260 |
|
261 | const query = {
|
262 | page: 1,
|
263 | per_page: false
|
264 | }
|
265 |
|
266 | t.truthy(validate.query(query, options, paramNames))
|
267 | t.is(query.page, 1)
|
268 | t.is(query.per_page, 42)
|
269 | })
|
270 |
|
271 | test('query is not valid | page is string', (t) => {
|
272 | const options = {
|
273 | perPage: 42
|
274 | }
|
275 |
|
276 | const query = {
|
277 | page: 'foo',
|
278 | per_page: 1
|
279 | }
|
280 |
|
281 | t.falsy(validate.query(query, options, paramNames))
|
282 | t.is(query.page, 0)
|
283 | t.is(query.per_page, 1)
|
284 | })
|
285 |
|
286 | test('query is not valid | per_page is string', (t) => {
|
287 | const options = {
|
288 | perPage: 42
|
289 | }
|
290 |
|
291 | const query = {
|
292 | page: 1,
|
293 | per_page: 'foo'
|
294 | }
|
295 |
|
296 | t.falsy(validate.query(query, options, paramNames))
|
297 | t.is(query.page, 1)
|
298 | t.is(query.per_page, 0)
|
299 | })
|
300 |
|
301 | test('query is not valid | per_page out of range', (t) => {
|
302 | const options = {
|
303 | perPage: 42
|
304 | }
|
305 |
|
306 | const query = {
|
307 | page: 1,
|
308 | per_page: 1000
|
309 | }
|
310 |
|
311 | t.falsy(validate.query(query, options, paramNames))
|
312 | t.is(query.page, 1)
|
313 | t.is(query.per_page, 1000)
|
314 | })
|