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

formDevtoolsStore.test.tsx

142 lines4.3 KB

1import { render } from 'solid-js/web'
2import { afterEach, beforeEach, describe, expect, it } from 'vitest'
3import {
4 FormDevtoolsStoreProvider,
5 useFormDevtoolsStore,
6} from '../src/stores/formDevtoolsStore'
7import { formDevtoolsEventClient } from '../src/eventClient.lib'
8import { formSelectorCache } from '../src/stores/formSelectorStore'
9import { connectTestEventBus } from './testEventBus'
10import type { FormDevtoolsStore } from '../src/stores/formDevtoolsStore'
11​
12const disposers: Array<() => void> = []
13​
14beforeEach(() => {
15 disposers.push(connectTestEventBus())
16})
17​
18afterEach(() => {
19 for (const dispose of disposers.splice(0)) dispose()
20 formSelectorCache.setMountedForms([])
21 formSelectorCache.setRequestedFormId(null)
22 formSelectorCache.setBridgeStatus('checking')
23 formSelectorCache.setBridgeInstanceId(null)
24 formSelectorCache.setBridgeMountedFormCount(null)
25})
26​
27function StoreCapture(props: { onStore: (store: FormDevtoolsStore) => void }) {
28 const store = useFormDevtoolsStore()
29 props.onStore(store)
30 return null
32​
33function renderStore(onStore: (store: FormDevtoolsStore) => void) {
34 const disposeRoot = render(
35 () => (
36 <FormDevtoolsStoreProvider>
37 <StoreCapture onStore={onStore} />
38 </FormDevtoolsStoreProvider>
39 ),
40 document.createElement('div'),
41 )
42 let isDisposed = false
43 const dispose = () => {
44 if (isDisposed) return
45 isDisposed = true
46 disposeRoot()
47 }
48​
49 disposers.push(dispose)
50 return dispose
52​
53describe('form devtools store provider', () => {
54 it('shares its cache while owning computations per store instance', () => {
55 let first!: FormDevtoolsStore
56 let second!: FormDevtoolsStore
57​
58 renderStore((store) => (first = store))
59 renderStore((store) => (second = store))
60​
61 first.formSelector.setMountedForms([
62 { instanceId: 'form-a', label: 'Form A' },
63 ])
64​
65 expect(first.formSelector.mountedForms).toBe(
66 second.formSelector.mountedForms,
67 )
68 expect(first.formSelector.selectedForm).not.toBe(
69 second.formSelector.selectedForm,
70 )
71 expect(first.fieldList.rowsByPath).toBe(second.fieldList.rowsByPath)
72 expect(first.fieldList.fieldRows).not.toBe(second.fieldList.fieldRows)
73 expect(first.formSelector.selectedForm()?.label).toBe('Form A')
74 expect(second.formSelector.selectedForm()?.label).toBe('Form A')
75 })
76​
77 it('retains the shared field subscription until the last instance releases it', () => {
78 let subscribeCount = 0
79 let unsubscribeCount = 0
80​
81 formSelectorCache.setMountedForms([
82 { instanceId: 'form-a', label: 'Form A' },
83 ])
84 const cleanupSubscribe = formDevtoolsEventClient.on(
85 'field-list-subscribe',
86 () => subscribeCount++,
87 )
88 const cleanupUnsubscribe = formDevtoolsEventClient.on(
89 'field-list-unsubscribe',
90 () => unsubscribeCount++,
91 )
92 disposers.push(cleanupSubscribe, cleanupUnsubscribe)
93 const disposeFirst = renderStore(() => {})
94 const disposeSecond = renderStore(() => {})
95​
96 expect(subscribeCount).toBe(1)
97​
98 disposeFirst()
99 expect(unsubscribeCount).toBe(0)
100​
101 disposeSecond()
102 expect(unsubscribeCount).toBe(1)
103 })
104​
105 it('retains shared field-detail subscriptions until the last instance releases them', () => {
106 let subscribeCount = 0
107 let unsubscribeCount = 0
108 let first!: FormDevtoolsStore
109 let second!: FormDevtoolsStore
110​
111 formSelectorCache.setMountedForms([
112 { instanceId: 'form-a', label: 'Form A' },
113 ])
114 const cleanupSubscribe = formDevtoolsEventClient.on(
115 'field-detail-subscribe',
116 () => subscribeCount++,
117 )
118 const cleanupUnsubscribe = formDevtoolsEventClient.on(
119 'field-detail-unsubscribe',
120 () => unsubscribeCount++,
121 )
122 disposers.push(cleanupSubscribe, cleanupUnsubscribe)
123 const disposeFirst = renderStore((store) => (first = store))
124 const disposeSecond = renderStore((store) => (second = store))
125​
126 first.fieldList.applySnapshot({
127 formInstanceId: 'form-a',
128 fields: [{ fieldId: 'field-name', path: 'name' }],
129 })
130 first.fieldList.setSelectedFieldPath('name')
131​
132 expect(first.fieldList.mainPanelFieldRows()).toHaveLength(1)
133 expect(second.fieldList.mainPanelFieldRows()).toHaveLength(1)
134 expect(subscribeCount).toBe(1)
135​
136 disposeFirst()
137 expect(unsubscribeCount).toBe(0)
138​
139 disposeSecond()
140 expect(unsubscribeCount).toBe(1)
141 })
142})