Open History to see the last commit
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})