-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathget-query-parts.js
118 lines (102 loc) · 2.59 KB
/
get-query-parts.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Internal dependencies
*/
import { getQueryParts } from '../get-query-parts';
describe( 'getQueryParts', () => {
it( 'parses out pagination data', () => {
const parts = getQueryParts( { page: 2, per_page: 2 } );
expect( parts ).toEqual( {
context: 'default',
page: 2,
perPage: 2,
stableKey: '',
fields: null,
include: null,
} );
} );
it( 'parses out `include` ID filtering', () => {
const first = getQueryParts( { include: '1' } );
const second = getQueryParts( { include: 1 } );
const parts = getQueryParts( { include: [ 1 ] } );
expect( first ).toEqual( second );
expect( second ).toEqual( parts );
expect( parts ).toEqual( {
context: 'default',
page: 1,
perPage: 10,
stableKey: 'include=1',
fields: null,
include: [ 1 ],
} );
} );
it( 'encodes stable string key', () => {
const first = getQueryParts( { '?': '&', b: 2 } );
const second = getQueryParts( { b: 2, '?': '&' } );
expect( first ).toEqual( second );
expect( first ).toEqual( {
context: 'default',
page: 1,
perPage: 10,
stableKey: '%3F=%26&b=2',
fields: null,
include: null,
} );
} );
it( 'encodes deep values', () => {
const parts = getQueryParts( { a: [ 1, 2 ] } );
expect( parts ).toEqual( {
context: 'default',
page: 1,
perPage: 10,
stableKey: 'a%5B0%5D=1&a%5B1%5D=2',
fields: null,
include: null,
} );
} );
it( 'encodes stable string key with page data normalized to number', () => {
const first = getQueryParts( { b: 2, page: 1, per_page: 10 } );
const second = getQueryParts( { b: 2, page: '1', per_page: '10' } );
expect( first ).toEqual( second );
expect( first ).toEqual( {
context: 'default',
page: 1,
perPage: 10,
stableKey: 'b=2',
fields: null,
include: null,
} );
} );
it( 'returns -1 for unlimited queries', () => {
const parts = getQueryParts( { b: 2, page: 1, per_page: -1 } );
expect( parts ).toEqual( {
context: 'default',
page: 1,
perPage: -1,
stableKey: 'b=2',
fields: null,
include: null,
} );
} );
it( 'encodes stable string key with fields parameters', () => {
const parts = getQueryParts( { _fields: [ 'id', 'title' ] } );
expect( parts ).toEqual( {
context: 'default',
page: 1,
perPage: 10,
stableKey: '_fields=id%2Ctitle',
fields: [ 'id', 'title' ],
include: null,
} );
} );
it( 'returns the context as a dedicated query part', () => {
const parts = getQueryParts( { context: 'view' } );
expect( parts ).toEqual( {
page: 1,
perPage: 10,
stableKey: '',
include: null,
fields: null,
context: 'view',
} );
} );
} );