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

subscribable.test-d.tsx

190 lines6.5 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { Subscribable } from '../subscribable'
3​
4describe('subscribable', () => {
5 describe('Subscribable', () => {
6 it('should constrain TListener to a function type', () => {
7 // @ts-expect-error a listener must extend Function
8 new Subscribable<string>()
9​
10 expectTypeOf(new Subscribable<() => void>()).toEqualTypeOf<
11 Subscribable<() => void>
12 >()
13 })
14​
15 it('should accept TListener with parameters and a return type', () => {
16 expectTypeOf(new Subscribable<(value: number) => string>()).toEqualTypeOf<
17 Subscribable<(value: number) => string>
18 >()
19 })
20​
21 it('should accept any Function subtype as TListener, not just a call signature', () => {
22 // `Function` is wider than `(...args: Array<any>) => any`: a construct
23 // signature and a function carrying extra properties both satisfy the
24 // former but not the latter.
25 type Constructor = new () => object
26 // oxlint-disable-next-line typescript/no-unsafe-function-type -- checks that 'TListener' accepts any 'Function' subtype
27 interface CallableWithProperty extends Function {
28 custom: number
29 }
30​
31 expectTypeOf(new Subscribable<Constructor>()).toEqualTypeOf<
32 Subscribable<Constructor>
33 >()
34 expectTypeOf(new Subscribable<CallableWithProperty>()).toEqualTypeOf<
35 Subscribable<CallableWithProperty>
36 >()
37 })
38​
39 it('should not default TListener', () => {
40 // @ts-expect-error a listener type must be provided
41 type WithoutArgument = Subscribable
42​
43 // the `@ts-expect-error` above is the verification here: omitting the
44 // argument has to be an error. It leaves the alias as the error type,
45 // so there is no positive assertion to pair it with -- these only
46 // record that the erroring alias is `any` while a supplied one is not.
47 expectTypeOf<WithoutArgument>().toBeAny()
48 expectTypeOf<Subscribable<() => void>>().not.toBeAny()
49 })
50​
51 it('should only expose subscribe and hasListeners', () => {
52 expectTypeOf<keyof Subscribable<() => void>>().toEqualTypeOf<
53 'subscribe' | 'hasListeners'
54 >()
55 })
56​
57 describe('constructor', () => {
58 it('should not accept any argument', () => {
59 expectTypeOf<
60 ConstructorParameters<typeof Subscribable>
61 >().toEqualTypeOf<[]>()
62 })
63 })
64​
65 describe('subscribe', () => {
66 it('should accept the listener type as its only parameter', () => {
67 const subscribable = new Subscribable<(value: number) => string>()
68​
69 expectTypeOf(subscribable.subscribe).parameters.toEqualTypeOf<
70 [listener: (value: number) => string]
71 >()
72 })
73​
74 it('should return an unsubscribe function that takes no argument and returns void', () => {
75 const subscribable = new Subscribable<() => void>()
76​
77 expectTypeOf(subscribable.subscribe).returns.toEqualTypeOf<() => void>()
78 expectTypeOf(subscribable.subscribe(() => {})).toEqualTypeOf<
79 () => void
80 >()
81 })
82​
83 it('should reject a listener that does not match the listener type', () => {
84 const subscribable = new Subscribable<(value: number) => string>()
85​
86 // @ts-expect-error the listener must return a string
87 subscribable.subscribe((value: number) => value)
88​
89 expectTypeOf(
90 subscribable.subscribe((value: number) => String(value)),
91 ).toEqualTypeOf<() => void>()
92 })
93 })
94​
95 describe('hasListeners', () => {
96 it('should take no argument and return a boolean', () => {
97 const subscribable = new Subscribable<() => void>()
98​
99 expectTypeOf(subscribable.hasListeners).parameters.toEqualTypeOf<[]>()
100 expectTypeOf(subscribable.hasListeners).returns.toEqualTypeOf<boolean>()
101 })
102 })
103​
104 describe('listeners', () => {
105 it('should not be accessible from outside the class', () => {
106 const subscribable = new Subscribable<() => void>()
107​
108 // @ts-expect-error listeners is protected
109 subscribable.listeners
110​
111 expectTypeOf(subscribable).not.toHaveProperty('listeners')
112 })
113​
114 it('should be a set of listeners inside the class', () => {
115 class CustomSubscribable extends Subscribable<(value: number) => void> {
116 getListeners() {
117 expectTypeOf(this.listeners).toEqualTypeOf<
118 Set<(value: number) => void>
119 >()
120 return this.listeners
121 }
122 }
123​
124 expectTypeOf(new CustomSubscribable().getListeners()).toEqualTypeOf<
125 Set<(value: number) => void>
126 >()
127 })
128​
129 it('should stay writable inside the class', () => {
130 class CustomSubscribable extends Subscribable<() => void> {
131 replaceListeners() {
132 this.listeners = new Set<() => void>()
133 }
134 }
135​
136 expectTypeOf<CustomSubscribable['replaceListeners']>().toEqualTypeOf<
137 () => void
138 >()
139 })
140 })
141​
142 describe('onSubscribe', () => {
143 it('should not be accessible from outside the class', () => {
144 const subscribable = new Subscribable<() => void>()
145​
146 // @ts-expect-error onSubscribe is protected
147 subscribable.onSubscribe()
148​
149 expectTypeOf(subscribable).not.toHaveProperty('onSubscribe')
150 })
151​
152 it('should take no argument and return void inside the class', () => {
153 class CustomSubscribable extends Subscribable<() => void> {
154 protected override onSubscribe(): void {
155 expectTypeOf(super.onSubscribe).parameters.toEqualTypeOf<[]>()
156 expectTypeOf(super.onSubscribe).returns.toEqualTypeOf<void>()
157 }
158 }
159​
160 expectTypeOf(
161 new CustomSubscribable(),
162 ).toEqualTypeOf<CustomSubscribable>()
163 })
164 })
165​
166 describe('onUnsubscribe', () => {
167 it('should not be accessible from outside the class', () => {
168 const subscribable = new Subscribable<() => void>()
169​
170 // @ts-expect-error onUnsubscribe is protected
171 subscribable.onUnsubscribe()
172​
173 expectTypeOf(subscribable).not.toHaveProperty('onUnsubscribe')
174 })
175​
176 it('should take no argument and return void inside the class', () => {
177 class CustomSubscribable extends Subscribable<() => void> {
178 protected override onUnsubscribe(): void {
179 expectTypeOf(super.onUnsubscribe).parameters.toEqualTypeOf<[]>()
180 expectTypeOf(super.onUnsubscribe).returns.toEqualTypeOf<void>()
181 }
182 }
183​
184 expectTypeOf(
185 new CustomSubscribable(),
186 ).toEqualTypeOf<CustomSubscribable>()
187 })
188 })
189 })
190})