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

bridgeStatusBadge.test.tsx

127 lines4.0 KB

1import { render } from 'solid-js/web'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3import { BridgeStatusBadge } from '../src/components/header/BridgeStatusBadge'
4import {
5 FormDevtoolsStoreProvider,
6 useFormDevtoolsStore,
7} from '../src/stores/formDevtoolsStore'
8import {
9 BRIDGE_STATUS_HEARTBEAT_INTERVAL_MS,
10 BRIDGE_STATUS_RESPONSE_TIMEOUT_MS,
11 formSelectorCache,
12} from '../src/stores/formSelectorStore'
13import { formDevtoolsEventClient } from '../src/eventClient.lib'
14import { connectTestEventBus } from './testEventBus'
15import type { FormDevtoolsStore } from '../src/stores/formDevtoolsStore'
16​
17const disposers: Array<() => void> = []
18​
19beforeEach(() => {
20 disposers.push(connectTestEventBus())
21})
22​
23afterEach(() => {
24 for (const dispose of disposers.splice(0)) dispose()
25 vi.useRealTimers()
26 formSelectorCache.setMountedForms([])
27 formSelectorCache.setRequestedFormId(null)
28 formSelectorCache.setBridgeStatus('checking')
29 formSelectorCache.setBridgeInstanceId(null)
30 formSelectorCache.setBridgeMountedFormCount(null)
31})
32​
33function StoreCapture(props: { onStore: (store: FormDevtoolsStore) => void }) {
34 props.onStore(useFormDevtoolsStore())
35 return null
37​
38function renderStatus(
39 onStore: (store: FormDevtoolsStore) => void = () => {},
40): HTMLDivElement {
41 const container = document.createElement('div')
42 const dispose = render(
43 () => (
44 <FormDevtoolsStoreProvider>
45 <StoreCapture onStore={onStore} />
46 <BridgeStatusBadge />
47 </FormDevtoolsStoreProvider>
48 ),
49 container,
50 )
51 disposers.push(dispose)
52 return container
54​
55describe('bridge status badge', () => {
56 it('distinguishes a connected bridge with no forms from no bridge', async () => {
57 vi.useFakeTimers()
58 let store!: FormDevtoolsStore
59 const container = renderStatus((current) => (store = current))
60​
61 expect(container.textContent).toContain('Checking form bridge')
62​
63 await vi.advanceTimersByTimeAsync(BRIDGE_STATUS_RESPONSE_TIMEOUT_MS)
64 expect(container.textContent).toContain('Form bridge not connected')
65​
66 let mountedFormCount = 0
67 const cleanupRequest = formDevtoolsEventClient.on(
68 'bridge-status-request',
69 (event) => {
70 formDevtoolsEventClient.emit('bridge-status-response', {
71 requestId: event.payload.requestId,
72 bridgeInstanceId: 'bridge-a',
73 mountedFormCount,
74 })
75 },
76 )
77 disposers.push(cleanupRequest)
78​
79 formSelectorCache.requestBridgeStatus()
80 expect(container.textContent).toContain(
81 'Bridge connected Β· no mounted forms',
82 )
83 expect(store.formSelector.bridgeInstanceId()).toBe('bridge-a')
84 expect(store.formSelector.bridgeMountedFormCount()).toBe(0)
85​
86 formSelectorCache.setMountedForms([
87 { instanceId: 'form-a', label: 'Form A' },
88 ])
89 expect(container.textContent).toContain(
90 'Bridge connected Β· no mounted forms',
91 )
92​
93 mountedFormCount = 1
94 formSelectorCache.requestBridgeStatus()
95 expect(container.textContent).toBe('')
96 expect(store.formSelector.bridgeMountedFormCount()).toBe(1)
97 })
98​
99 it('detects when a previously connected bridge stops answering', async () => {
100 vi.useFakeTimers()
101 const cleanupRequest = formDevtoolsEventClient.on(
102 'bridge-status-request',
103 (event) => {
104 formDevtoolsEventClient.emit('bridge-status-response', {
105 requestId: event.payload.requestId,
106 bridgeInstanceId: 'bridge-a',
107 mountedFormCount: 0,
108 })
109 },
110 )
111 let store!: FormDevtoolsStore
112 const container = renderStatus((current) => (store = current))
113​
114 expect(container.textContent).toContain(
115 'Bridge connected Β· no mounted forms',
116 )
117​
118 cleanupRequest()
119 await vi.advanceTimersByTimeAsync(
120 BRIDGE_STATUS_HEARTBEAT_INTERVAL_MS + BRIDGE_STATUS_RESPONSE_TIMEOUT_MS,
121 )
122​
123 expect(container.textContent).toContain('Form bridge not connected')
124 expect(store.formSelector.bridgeInstanceId()).toBeNull()
125 expect(store.formSelector.bridgeMountedFormCount()).toBeNull()
126 })
127})