1
- import { describe , expect , test } from 'vitest'
1
+ import { describe , expect , it } from 'vitest'
2
2
import type { Options } from './defs'
3
3
import {
4
4
parseAsArrayOf ,
@@ -16,91 +16,91 @@ const parsers = {
16
16
}
17
17
18
18
describe ( 'serializer' , ( ) => {
19
- test ( ' empty', ( ) => {
19
+ it ( 'handles empty inputs ', ( ) => {
20
20
const serialize = createSerializer ( parsers )
21
21
const result = serialize ( { } )
22
22
expect ( result ) . toBe ( '' )
23
23
} )
24
- test ( 'one item', ( ) => {
24
+ it ( 'handles a single item', ( ) => {
25
25
const serialize = createSerializer ( parsers )
26
26
const result = serialize ( { str : 'foo' } )
27
27
expect ( result ) . toBe ( '?str=foo' )
28
28
} )
29
- test ( ' several items', ( ) => {
29
+ it ( 'handles several items', ( ) => {
30
30
const serialize = createSerializer ( parsers )
31
31
const result = serialize ( { str : 'foo' , int : 1 , bool : true } )
32
32
expect ( result ) . toBe ( '?str=foo&int=1&bool=true' )
33
33
} )
34
- test ( "null items don't show up" , ( ) => {
34
+ it ( 'does not render null items' , ( ) => {
35
35
const serialize = createSerializer ( parsers )
36
36
const result = serialize ( { str : null } )
37
37
expect ( result ) . toBe ( '' )
38
38
} )
39
- test ( 'with string base', ( ) => {
39
+ it ( 'handles a string base', ( ) => {
40
40
const serialize = createSerializer ( parsers )
41
41
const result = serialize ( '/foo' , { str : 'foo' } )
42
42
expect ( result ) . toBe ( '/foo?str=foo' )
43
43
} )
44
- test ( 'with string base with search params', ( ) => {
44
+ it ( 'handles a string base with search params', ( ) => {
45
45
const serialize = createSerializer ( parsers )
46
46
const result = serialize ( '/foo?bar=egg' , { str : 'foo' } )
47
47
expect ( result ) . toBe ( '/foo?bar=egg&str=foo' )
48
48
} )
49
- test ( 'with URLSearchParams base', ( ) => {
49
+ it ( 'handles a URLSearchParams base', ( ) => {
50
50
const serialize = createSerializer ( parsers )
51
51
const search = new URLSearchParams ( '?bar=egg' )
52
52
const result = serialize ( search , { str : 'foo' } )
53
53
expect ( result ) . toBe ( '?bar=egg&str=foo' )
54
54
} )
55
- test ( 'Does not mutate existing params with URLSearchParams base', ( ) => {
55
+ it ( 'does not mutate existing params with URLSearchParams base', ( ) => {
56
56
const serialize = createSerializer ( parsers )
57
57
const searchBefore = new URLSearchParams ( '?str=foo' )
58
58
const result = serialize ( searchBefore , { str : 'bar' } )
59
59
expect ( result ) . toBe ( '?str=bar' )
60
60
expect ( searchBefore . get ( 'str' ) ) . toBe ( 'foo' )
61
61
} )
62
- test ( 'with URL base', ( ) => {
62
+ it ( 'handles a URL base', ( ) => {
63
63
const serialize = createSerializer ( parsers )
64
64
const url = new URL ( 'https://example.com/path' )
65
65
const result = serialize ( url , { str : 'foo' } )
66
66
expect ( result ) . toBe ( 'https://example.com/path?str=foo' )
67
67
} )
68
- test ( 'with URL base and search params', ( ) => {
68
+ it ( 'handles a URL base and merges search params', ( ) => {
69
69
const serialize = createSerializer ( parsers )
70
70
const url = new URL ( 'https://example.com/path?bar=egg' )
71
71
const result = serialize ( url , { str : 'foo' } )
72
72
expect ( result ) . toBe ( 'https://example.com/path?bar=egg&str=foo' )
73
73
} )
74
- test ( ' null value deletes from base', ( ) => {
74
+ it ( 'deletes a null value from base', ( ) => {
75
75
const serialize = createSerializer ( parsers )
76
76
const result = serialize ( '?str=bar&int=-1' , { str : 'foo' , int : null } )
77
77
expect ( result ) . toBe ( '?str=foo' )
78
78
} )
79
- test ( 'null deletes all from base', ( ) => {
79
+ it ( ' deletes all from base with a global null ', ( ) => {
80
80
const serialize = createSerializer ( parsers )
81
81
const result = serialize ( '?str=bar&int=-1' , null )
82
82
expect ( result ) . toBe ( '' )
83
83
} )
84
- test ( 'null keeps search params not managed by the serializer', ( ) => {
84
+ it ( ' keeps search params not managed by the serializer when fed null ', ( ) => {
85
85
const serialize = createSerializer ( parsers )
86
86
const result = serialize ( '?str=foo&external=kept' , null )
87
87
expect ( result ) . toBe ( '?external=kept' )
88
88
} )
89
- test ( 'clears value when setting null for search param that has a default value' , ( ) => {
89
+ it ( 'clears value when setting null for a search param that has a default value' , ( ) => {
90
90
const serialize = createSerializer ( {
91
91
int : parseAsInteger . withDefault ( 0 )
92
92
} )
93
93
const result = serialize ( '?int=1&str=foo' , { int : null } )
94
94
expect ( result ) . toBe ( '?str=foo' )
95
95
} )
96
- test ( 'clears value when setting null for search param that is set to its default value' , ( ) => {
96
+ it ( 'clears value when setting null for æ search param that is set to its default value' , ( ) => {
97
97
const serialize = createSerializer ( {
98
98
int : parseAsInteger . withDefault ( 0 )
99
99
} )
100
100
const result = serialize ( '?int=0&str=foo' , { int : null } )
101
101
expect ( result ) . toBe ( '?str=foo' )
102
102
} )
103
- test ( 'clears value when setting the default value (`clearOnDefault: true` is the default)' , ( ) => {
103
+ it ( 'clears value when setting the default value (`clearOnDefault: true` is the default)' , ( ) => {
104
104
const serialize = createSerializer ( {
105
105
int : parseAsInteger . withDefault ( 0 ) ,
106
106
str : parseAsString . withDefault ( '' ) ,
@@ -117,7 +117,7 @@ describe('serializer', () => {
117
117
} )
118
118
expect ( result ) . toBe ( '' )
119
119
} )
120
- test ( 'keeps value when setting the default value when `clearOnDefault: false`' , ( ) => {
120
+ it ( 'keeps value when setting the default value when `clearOnDefault: false`' , ( ) => {
121
121
const options : Options = { clearOnDefault : false }
122
122
const serialize = createSerializer ( {
123
123
int : parseAsInteger . withOptions ( options ) . withDefault ( 0 ) ,
@@ -139,7 +139,7 @@ describe('serializer', () => {
139
139
'?int=0&str=&bool=false&arr=&json={%22foo%22:%22bar%22}'
140
140
)
141
141
} )
142
- test ( 'support for global clearOnDefault option', ( ) => {
142
+ it ( 'supports a global clearOnDefault option', ( ) => {
143
143
const serialize = createSerializer (
144
144
{
145
145
int : parseAsInteger . withDefault ( 0 ) ,
@@ -161,7 +161,7 @@ describe('serializer', () => {
161
161
'?int=0&str=&bool=false&arr=&json={%22foo%22:%22bar%22}'
162
162
)
163
163
} )
164
- test ( 'parser clearOnDefault takes precedence over global clearOnDefault', ( ) => {
164
+ it ( 'gives precedence to parser clearOnDefault over global clearOnDefault', ( ) => {
165
165
const serialize = createSerializer (
166
166
{
167
167
int : parseAsInteger
@@ -177,7 +177,7 @@ describe('serializer', () => {
177
177
} )
178
178
expect ( result ) . toBe ( '?str=' )
179
179
} )
180
- test ( 'supports urlKeys' , ( ) => {
180
+ it ( 'supports urlKeys' , ( ) => {
181
181
const serialize = createSerializer ( parsers , {
182
182
urlKeys : {
183
183
bool : 'b' ,
@@ -188,4 +188,17 @@ describe('serializer', () => {
188
188
const result = serialize ( { str : 'foo' , int : 1 , bool : true } )
189
189
expect ( result ) . toBe ( '?s=foo&i=1&b=true' )
190
190
} )
191
+ it ( 'supports ? in the values' , ( ) => {
192
+ const serialize = createSerializer ( parsers )
193
+ const result = serialize ( { str : 'foo?bar' , int : 1 , bool : true } )
194
+ expect ( result ) . toBe ( '?str=foo?bar&int=1&bool=true' )
195
+ } )
196
+ it ( 'supports & in the base' , ( ) => {
197
+ // Repro for https://github.com/47ng/nuqs/issues/812
198
+ const serialize = createSerializer ( parsers )
199
+ const result = serialize ( 'https://example.com/path?issue=is?here' , {
200
+ str : 'foo?bar'
201
+ } )
202
+ expect ( result ) . toBe ( 'https://example.com/path?issue=is?here&str=foo?bar' )
203
+ } )
191
204
} )
0 commit comments