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

onlineManager.test-d.tsx

100 lines3.3 KB

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