-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery-params.test.js
84 lines (75 loc) · 2.78 KB
/
query-params.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const test = require('tape');
const queryParams = require('../lib/query-params/query-params');
const dir = __dirname.split('/')[__dirname.split('/').length - 1];
const file = dir + __filename.replace(__dirname, '') + ' > ';
test(file + 'Should build a query param object from a html request', (t) => {
t.plan(4);
const query = {
query: {
q: 'ada',
'fields[type]': 'people',
'filter[occupation]': 'mathematician',
'filter[birth[place]]': 'london',
'filter[date[from]]': 1800,
'filter[date[to]]': 1900
},
params: {}
};
const result = queryParams('html', query);
t.equal(result.filter.people.birthPlace[0], 'london', 'filter with place equal "london"');
t.equal(result.filter.people.birthDate, 1800, 'filter by birthDate with 1800');
t.equal(result.filter.people.deathDate, 1900, 'filter by deathDate with 1900');
t.equal(result.filter.people.occupation[0], 'mathematician', 'filter by occupation mathematician');
t.end();
});
test(file + 'Should build a query param object from a json api request', (t) => {
t.plan(6);
const query = {
query: {
q: 'ada',
'fields[type]': 'people',
'filter[occupation]': 'mathematician,developer',
'filter[birth[place]]': 'london,Paris',
'filter[date[from]]': 1800,
'filter[date[to]]': 1900
},
params: {}
};
const result = queryParams('json', query);
t.equal(result.filter.people.birthPlace[0], 'london', 'filter with place equal "london"');
t.equal(result.filter.people.birthPlace[1], 'Paris', 'filter with place equal "Paris"');
t.equal(result.filter.people.birthDate, 1800, 'filter by birthDate with 1800');
t.equal(result.filter.people.deathDate, 1900, 'filter by deathDate with 1900');
t.equal(result.filter.people.occupation[0], 'mathematician', 'filter by occupation mathematician');
t.equal(result.filter.people.occupation[1], 'developer', 'filter by occupation developer');
t.end();
});
test(file + 'Should build a query param object with the value if the format is not html or json', (t) => {
t.plan(1);
const query = {
query: {
q: 'ada',
'fields[type]': 'people',
'filter[occupation]': 'mathematician,developer'
},
params: {}
};
const result = queryParams('wrongFormat', query);
t.equal(result.filter.people.occupation, 'mathematician,developer', 'filter by occupation is null');
t.end();
});
test(file + 'Should convert pageNumber and pageSize to a type number', (t) => {
t.plan(2);
const query = {
query: {
q: 'ada',
'page[number]': '2',
'page[size]': '100'
},
params: {}
};
const result = queryParams('html', query);
t.equal(typeof result.pageNumber, 'number', 'pageNumber is a number');
t.equal(typeof result.pageSize, 'number', 'pageSize is a number');
t.end();
});