Skip to content

Commit b25d593

Browse files
committed
test: Add type tests
1 parent 846e029 commit b25d593

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

packages/nuqs/tests/useQueryState.test-d.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { describe, expectTypeOf, it } from 'vitest'
2-
import { parseAsString, useQueryState } from '../dist'
2+
import {
3+
debounce,
4+
defaultRateLimit,
5+
parseAsString,
6+
throttle,
7+
useQueryState
8+
} from '../dist'
39

410
describe('types/useQueryState', () => {
511
it('has a nullable string state by default', () => {
@@ -117,4 +123,54 @@ describe('types/useQueryState', () => {
117123
// @ts-expect-error
118124
setBar(() => undefined)
119125
})
126+
it('accepts a limitUrlUpdates option', () => {
127+
useQueryState('foo', {
128+
limitUrlUpdates: {
129+
method: 'throttle',
130+
timeMs: 100
131+
}
132+
})
133+
useQueryState('foo', {
134+
limitUrlUpdates: {
135+
method: 'debounce',
136+
timeMs: 100
137+
}
138+
})
139+
useQueryState('foo', { limitUrlUpdates: throttle(100) })
140+
useQueryState('foo', { limitUrlUpdates: debounce(100) })
141+
useQueryState('foo', { limitUrlUpdates: defaultRateLimit })
142+
// Using parsers options (builder pattern)
143+
parseAsString.withOptions({
144+
limitUrlUpdates: {
145+
method: 'throttle',
146+
timeMs: 100
147+
}
148+
})
149+
parseAsString.withOptions({
150+
limitUrlUpdates: {
151+
method: 'debounce',
152+
timeMs: 100
153+
}
154+
})
155+
parseAsString.withOptions({ limitUrlUpdates: throttle(100) })
156+
parseAsString.withOptions({ limitUrlUpdates: debounce(100) })
157+
parseAsString.withOptions({ limitUrlUpdates: defaultRateLimit })
158+
// State updater function
159+
const [, setState] = useQueryState('foo')
160+
setState(null, {
161+
limitUrlUpdates: {
162+
method: 'throttle',
163+
timeMs: 100
164+
}
165+
})
166+
setState(null, {
167+
limitUrlUpdates: {
168+
method: 'debounce',
169+
timeMs: 100
170+
}
171+
})
172+
setState(null, { limitUrlUpdates: throttle(100) })
173+
setState(null, { limitUrlUpdates: debounce(100) })
174+
setState(null, { limitUrlUpdates: defaultRateLimit })
175+
})
120176
})

packages/nuqs/tests/useQueryStates.test-d.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { describe, expectTypeOf, it } from 'vitest'
2-
import { parseAsInteger, parseAsString, useQueryStates } from '../dist'
2+
import {
3+
debounce,
4+
defaultRateLimit,
5+
parseAsInteger,
6+
parseAsString,
7+
throttle,
8+
useQueryStates
9+
} from '../dist'
310

411
describe('types/useQueryStates', () => {
512
const parsers = {
@@ -93,4 +100,40 @@ describe('types/useQueryStates', () => {
93100
}
94101
})
95102
})
103+
it('accepts a limitUrlUpdates option', () => {
104+
// Hook-level definition
105+
const parsers = { foo: parseAsString }
106+
useQueryStates(parsers, {
107+
limitUrlUpdates: {
108+
method: 'throttle',
109+
timeMs: 100
110+
}
111+
})
112+
useQueryStates(parsers, {
113+
limitUrlUpdates: {
114+
method: 'debounce',
115+
timeMs: 100
116+
}
117+
})
118+
useQueryStates(parsers, { limitUrlUpdates: throttle(100) })
119+
useQueryStates(parsers, { limitUrlUpdates: debounce(100) })
120+
useQueryStates(parsers, { limitUrlUpdates: defaultRateLimit })
121+
// State updater function
122+
const [, setState] = useQueryStates(parsers)
123+
setState(null, {
124+
limitUrlUpdates: {
125+
method: 'throttle',
126+
timeMs: 100
127+
}
128+
})
129+
setState(null, {
130+
limitUrlUpdates: {
131+
method: 'debounce',
132+
timeMs: 100
133+
}
134+
})
135+
setState(null, { limitUrlUpdates: throttle(100) })
136+
setState(null, { limitUrlUpdates: debounce(100) })
137+
setState(null, { limitUrlUpdates: defaultRateLimit })
138+
})
96139
})

0 commit comments

Comments
 (0)