Open History to see the last commit
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 localNotifyManager = createNotifyManager()
37β
38 expectTypeOf(localNotifyManager.batch(() => 5)).toEqualTypeOf<number>()
39 expectTypeOf(
40 localNotifyManager.batch(() => 'im happy' as const),
41 ).toEqualTypeOf<'im happy'>()
42 expectTypeOf(localNotifyManager.batch(() => ({ a: 1 }))).toEqualTypeOf<{
43 a: number
44 }>()
45 })
46β
47 it('should be void when the callback returns nothing', () => {
48 const localNotifyManager = createNotifyManager()
49β
50 expectTypeOf(localNotifyManager.batch(() => {})).toEqualTypeOf<void>()
51 })
52β
53 it('should only accept a callback taking no arguments', () => {
54 const localNotifyManager = createNotifyManager()
55β
56 expectTypeOf(localNotifyManager.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 localNotifyManager = 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 = localNotifyManager.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 localNotifyManager = createNotifyManager()
83β
84 const fn: (a: string) => string = (a) => a
85 const someFn = localNotifyManager.batchCalls(fn)
86β
87 expectTypeOf(someFn).returns.toEqualTypeOf<void>()
88 })
89β
90 it('should accept a callback without parameters', () => {
91 const localNotifyManager = createNotifyManager()
92β
93 const someFn = localNotifyManager.batchCalls(() => {})
94β
95 expectTypeOf(someFn).parameters.toEqualTypeOf<[]>()
96 })
97β
98 it('should preserve optional parameters', () => {
99 const localNotifyManager = createNotifyManager()
100β
101 const someFn = localNotifyManager.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 localNotifyManager = createNotifyManager()
110β
111 const someFn = localNotifyManager.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 localNotifyManager = createNotifyManager()
120β
121 expectTypeOf(
122 localNotifyManager.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 localNotifyManager.batchCalls<string>(() => {})
127 })
128 })
129β
130 describe('schedule', () => {
131 it('should only accept a callback taking no arguments', () => {
132 const localNotifyManager = createNotifyManager()
133β
134 expectTypeOf(localNotifyManager.schedule).parameters.toEqualTypeOf<
135 [callback: () => void]
136 >()
137 })
138β
139 it('should return void', () => {
140 const localNotifyManager = createNotifyManager()
141β
142 expectTypeOf(localNotifyManager.schedule).returns.toEqualTypeOf<void>()
143 })
144 })
145β
146 describe('setNotifyFunction', () => {
147 it('should only accept a function taking a callback', () => {
148 const localNotifyManager = createNotifyManager()
149β
150 expectTypeOf(
151 localNotifyManager.setNotifyFunction,
152 ).parameters.toEqualTypeOf<[fn: (callback: () => void) => void]>()
153 })
154β
155 it('should return void', () => {
156 const localNotifyManager = createNotifyManager()
157β
158 expectTypeOf(
159 localNotifyManager.setNotifyFunction,
160 ).returns.toEqualTypeOf<void>()
161 })
162 })
163β
164 describe('setBatchNotifyFunction', () => {
165 it('should only accept a function taking a callback', () => {
166 const localNotifyManager = createNotifyManager()
167β
168 expectTypeOf(
169 localNotifyManager.setBatchNotifyFunction,
170 ).parameters.toEqualTypeOf<[fn: (callback: () => void) => void]>()
171 })
172β
173 it('should return void', () => {
174 const localNotifyManager = createNotifyManager()
175β
176 expectTypeOf(
177 localNotifyManager.setBatchNotifyFunction,
178 ).returns.toEqualTypeOf<void>()
179 })
180 })
181β
182 describe('setScheduler', () => {
183 it('should only accept a function taking a callback', () => {
184 const localNotifyManager = createNotifyManager()
185β
186 expectTypeOf(localNotifyManager.setScheduler).parameters.toEqualTypeOf<
187 [fn: (callback: () => void) => void]
188 >()
189 })
190β
191 it('should accept the exported defaultScheduler', () => {
192 const localNotifyManager = createNotifyManager()
193β
194 expectTypeOf(defaultScheduler).toEqualTypeOf<
195 Parameters<typeof localNotifyManager.setScheduler>[0]
196 >()
197 })
198β
199 it('should return void', () => {
200 const localNotifyManager = createNotifyManager()
201β
202 expectTypeOf(
203 localNotifyManager.setScheduler,
204 ).returns.toEqualTypeOf<void>()
205 })
206 })
207})