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

subscribable.test-d.tsx

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