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

focusManager.test-d.tsx

108 lines3.6 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { focusManager } from '..'
3import type { FocusManager } from '..'
4​
5describe('focusManager', () => {
6 describe('FocusManager', () => {
7 it('should type the singleton as a FocusManager', () => {
8 expectTypeOf(focusManager).toEqualTypeOf<FocusManager>()
9 })
10​
11 it('should only expose its documented members', () => {
12 expectTypeOf<keyof FocusManager>().toEqualTypeOf<
13 | 'subscribe'
14 | 'hasListeners'
15 | 'setEventListener'
16 | 'setFocused'
17 | 'onFocus'
18 | 'isFocused'
19 >()
20 })
21 })
22​
23 describe('subscribe', () => {
24 it('should take a listener of the focus state and return an unsubscribe function', () => {
25 expectTypeOf(focusManager.subscribe).parameters.toEqualTypeOf<
26 [listener: (focused: boolean) => void]
27 >()
28 expectTypeOf(focusManager.subscribe).returns.toEqualTypeOf<() => void>()
29 })
30​
31 it('should reject a listener that takes a non-boolean focus state', () => {
32 // @ts-expect-error the focus state handed to a listener is a boolean
33 const unsubscribe = focusManager.subscribe((focused: string) => focused)
34​
35 expectTypeOf(unsubscribe).toEqualTypeOf<() => void>()
36 })
37 })
38​
39 describe('hasListeners', () => {
40 it('should take no arguments and return a boolean', () => {
41 expectTypeOf(focusManager.hasListeners).parameters.toEqualTypeOf<[]>()
42 expectTypeOf(focusManager.hasListeners).returns.toEqualTypeOf<boolean>()
43 })
44 })
45​
46 describe('setEventListener', () => {
47 it('should only accept a setup function and return void', () => {
48 expectTypeOf(focusManager.setEventListener).parameters.toEqualTypeOf<
49 [
50 setup: (
51 setFocused: (focused?: boolean) => void,
52 ) => (() => void) | undefined,
53 ]
54 >()
55 expectTypeOf(focusManager.setEventListener).returns.toEqualTypeOf<void>()
56 })
57​
58 it('should hand the setup function a setFocused callback whose argument is optional', () => {
59 // Unlike `onlineManager`, the focus callback may be called with no
60 // argument at all to re-evaluate the current focus state.
61 type Setup = Parameters<FocusManager['setEventListener']>[0]
62​
63 expectTypeOf<Parameters<Setup>[0]>().toEqualTypeOf<
64 (focused?: boolean) => void
65 >()
66 expectTypeOf<Parameters<Parameters<Setup>[0]>['length']>().toEqualTypeOf<
67 0 | 1
68 >()
69 })
70​
71 it('should let the setup function opt out of returning a cleanup function', () => {
72 type Setup = Parameters<FocusManager['setEventListener']>[0]
73​
74 expectTypeOf<ReturnType<Setup>>().toEqualTypeOf<
75 (() => void) | undefined
76 >()
77 })
78 })
79​
80 describe('setFocused', () => {
81 it('should take an optional boolean and return void', () => {
82 expectTypeOf(focusManager.setFocused).parameters.toEqualTypeOf<
83 [focused?: boolean]
84 >()
85 expectTypeOf(focusManager.setFocused).returns.toEqualTypeOf<void>()
86 })
87​
88 it('should be callable with no argument to fall back to the default check', () => {
89 expectTypeOf<
90 Parameters<FocusManager['setFocused']>['length']
91 >().toEqualTypeOf<0 | 1>()
92 })
93 })
94​
95 describe('onFocus', () => {
96 it('should take no arguments and return void', () => {
97 expectTypeOf(focusManager.onFocus).parameters.toEqualTypeOf<[]>()
98 expectTypeOf(focusManager.onFocus).returns.toEqualTypeOf<void>()
99 })
100 })
101​
102 describe('isFocused', () => {
103 it('should take no arguments and return a boolean', () => {
104 expectTypeOf(focusManager.isFocused).parameters.toEqualTypeOf<[]>()
105 expectTypeOf(focusManager.isFocused).returns.toEqualTypeOf<boolean>()
106 })
107 })
108})