Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

notifyManager.test-d.tsx

205 lines6.1 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 createNotifyManager,
4 defaultScheduler,
5 notifyManager,
6} from '../notifyManager'
7​
8describe('notifyManager', () => {
9 it('should expose the same type as the factory', () => {
10 expectTypeOf(notifyManager).toEqualTypeOf<
11 ReturnType<typeof createNotifyManager>
12 >()
13 })
14​
15 it('should expose every method as readonly', () => {
16 type Manager = ReturnType<typeof createNotifyManager>
17​
18 expectTypeOf<Manager>().toEqualTypeOf<{
19 readonly [K in keyof Manager]: Manager[K]
20 }>()
21 })
22​
23 it('should only expose its documented members', () => {
24 expectTypeOf<keyof ReturnType<typeof createNotifyManager>>().toEqualTypeOf<
25 | 'batch'
26 | 'batchCalls'
27 | 'schedule'
28 | 'setNotifyFunction'
29 | 'setBatchNotifyFunction'
30 | 'setScheduler'
31 >()
32 })
33​
34 describe('batch', () => {
35 it('should pass the return type of the callback through', () => {
36 const notifyManagerTest = createNotifyManager()
37​
38 expectTypeOf(notifyManagerTest.batch(() => 5)).toEqualTypeOf<number>()
39 expectTypeOf(
40 notifyManagerTest.batch(() => 'im happy' as const),
41 ).toEqualTypeOf<'im happy'>()
42 expectTypeOf(notifyManagerTest.batch(() => ({ a: 1 }))).toEqualTypeOf<{
43 a: number
44 }>()
45 })
46​
47 it('should be void when the callback returns nothing', () => {
48 const notifyManagerTest = createNotifyManager()
49​
50 expectTypeOf(notifyManagerTest.batch(() => {})).toEqualTypeOf<void>()
51 })
52​
53 it('should only accept a callback taking no arguments', () => {
54 const notifyManagerTest = createNotifyManager()
55​
56 expectTypeOf(notifyManagerTest.batch).parameters.toEqualTypeOf<
57 [callback: () => unknown]
58 >()
59 })
60 })
61​
62 describe('batchCalls', () => {
63 it('should catch improper signatures of the wrapped function', () => {
64 const notifyManagerTest = createNotifyManager()
65​
66 // we define some fn with its signature:
67 const fn: (a: string, b: number) => string = (a, b) => a + b
68​
69 // now someFn expect to be called with args [a: string, b: number]
70 const someFn = notifyManagerTest.batchCalls(fn)
71​
72 expectTypeOf(someFn).parameters.toEqualTypeOf<Parameters<typeof fn>>()
73 assertType<Parameters<typeof someFn>>(['im happy', 4])
74 assertType<Parameters<typeof someFn>>([
75 'im not happy',
76 // @ts-expect-error
77 false,
78 ])
79 })
80​
81 it('should discard the return type of the wrapped function', () => {
82 const notifyManagerTest = createNotifyManager()
83​
84 const fn: (a: string) => string = (a) => a
85 const someFn = notifyManagerTest.batchCalls(fn)
86​
87 expectTypeOf(someFn).returns.toEqualTypeOf<void>()
88 })
89​
90 it('should accept a callback without parameters', () => {
91 const notifyManagerTest = createNotifyManager()
92​
93 const someFn = notifyManagerTest.batchCalls(() => {})
94​
95 expectTypeOf(someFn).parameters.toEqualTypeOf<[]>()
96 })
97​
98 it('should preserve optional parameters', () => {
99 const notifyManagerTest = createNotifyManager()
100​
101 const someFn = notifyManagerTest.batchCalls(
102 (_a: string, _b?: number) => {},
103 )
104​
105 expectTypeOf(someFn).parameters.toEqualTypeOf<[_a: string, _b?: number]>()
106 })
107​
108 it('should preserve rest parameters', () => {
109 const notifyManagerTest = createNotifyManager()
110​
111 const someFn = notifyManagerTest.batchCalls(
112 (..._args: Array<number>) => {},
113 )
114​
115 expectTypeOf(someFn).parameters.toEqualTypeOf<Array<number>>()
116 })
117​
118 it('should constrain its type argument to an argument tuple', () => {
119 const notifyManagerTest = createNotifyManager()
120​
121 expectTypeOf(
122 notifyManagerTest.batchCalls<[a: string, b: number]>((_a, _b) => {}),
123 ).parameters.toEqualTypeOf<[a: string, b: number]>()
124​
125 // @ts-expect-error a non-array type does not satisfy Array<unknown>
126 notifyManagerTest.batchCalls<string>(() => {})
127 })
128 })
129​
130 describe('schedule', () => {
131 it('should only accept a callback taking no arguments', () => {
132 const notifyManagerTest = createNotifyManager()
133​
134 expectTypeOf(notifyManagerTest.schedule).parameters.toEqualTypeOf<
135 [callback: () => void]
136 >()
137 })
138​
139 it('should return void', () => {
140 const notifyManagerTest = createNotifyManager()
141​
142 expectTypeOf(notifyManagerTest.schedule).returns.toEqualTypeOf<void>()
143 })
144 })
145​
146 describe('setNotifyFunction', () => {
147 it('should only accept a function taking a callback', () => {
148 const notifyManagerTest = createNotifyManager()
149​
150 expectTypeOf(
151 notifyManagerTest.setNotifyFunction,
152 ).parameters.toEqualTypeOf<[fn: (callback: () => void) => void]>()
153 })
154​
155 it('should return void', () => {
156 const notifyManagerTest = createNotifyManager()
157​
158 expectTypeOf(
159 notifyManagerTest.setNotifyFunction,
160 ).returns.toEqualTypeOf<void>()
161 })
162 })
163​
164 describe('setBatchNotifyFunction', () => {
165 it('should only accept a function taking a callback', () => {
166 const notifyManagerTest = createNotifyManager()
167​
168 expectTypeOf(
169 notifyManagerTest.setBatchNotifyFunction,
170 ).parameters.toEqualTypeOf<[fn: (callback: () => void) => void]>()
171 })
172​
173 it('should return void', () => {
174 const notifyManagerTest = createNotifyManager()
175​
176 expectTypeOf(
177 notifyManagerTest.setBatchNotifyFunction,
178 ).returns.toEqualTypeOf<void>()
179 })
180 })
181​
182 describe('setScheduler', () => {
183 it('should only accept a function taking a callback', () => {
184 const notifyManagerTest = createNotifyManager()
185​
186 expectTypeOf(notifyManagerTest.setScheduler).parameters.toEqualTypeOf<
187 [fn: (callback: () => void) => void]
188 >()
189 })
190​
191 it('should accept the exported defaultScheduler', () => {
192 const notifyManagerTest = createNotifyManager()
193​
194 expectTypeOf(defaultScheduler).toEqualTypeOf<
195 Parameters<typeof notifyManagerTest.setScheduler>[0]
196 >()
197 })
198​
199 it('should return void', () => {
200 const notifyManagerTest = createNotifyManager()
201​
202 expectTypeOf(notifyManagerTest.setScheduler).returns.toEqualTypeOf<void>()
203 })
204 })
205})