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

fieldListMetaBadges.test.tsx

100 lines3.1 KB

1import { render } from 'solid-js/web'
2import { afterEach, describe, expect, it } from 'vitest'
3import { FieldListMetaBadges } from '../src/components/fields/leftPanel/FieldListMetaBadges'
4import {
5 FormDevtoolsStoreProvider,
6 useFormDevtoolsStore,
7} from '../src/stores/formDevtoolsStore'
8import { fieldListCache } from '../src/stores/fieldListStore'
9import { connectTestEventBus } from './testEventBus'
10import type { FormDevtoolsStore } from '../src/stores/formDevtoolsStore'
11import type { FieldId, FormId } from '../src/types/branded'
12​
13const formId = 'form-a' as FormId
14const fieldId = 'field-a' as FieldId
15const disposers: Array<() => void> = []
16​
17afterEach(() => {
18 for (const dispose of disposers.splice(0)) dispose()
19 fieldListCache.setSubscribedFormId(null)
20 fieldListCache.clearRows()
21})
22​
23function StoreCapture(props: { onStore: (store: FormDevtoolsStore) => void }) {
24 props.onStore(useFormDevtoolsStore())
25 return null
27​
28describe('field list meta badges', () => {
29 it('renders active non-default summary badges', () => {
30 disposers.push(connectTestEventBus())
31 const container = document.createElement('div')
32 let store!: FormDevtoolsStore
33 disposers.push(
34 render(
35 () => (
36 <FormDevtoolsStoreProvider>
37 <StoreCapture onStore={(value) => (store = value)} />
38 <FieldListMetaBadges fieldId={fieldId} isMounted />
39 </FormDevtoolsStoreProvider>
40 ),
41 container,
42 ),
43 )
44 store.fieldList.setSubscribedFormId(formId)
45 store.fieldList.applySnapshot({
46 formInstanceId: formId,
47 fields: [
48 {
49 fieldId,
50 path: 'name',
51 summary: {
52 isTouched: true,
53 isBlurred: true,
54 isLongValidating: true,
55 isDefaultValue: false,
56 validity: 'invalidHidden',
57 },
58 },
59 ],
60 })
61​
62 expect(container.textContent).not.toContain('Touched')
63 expect(container.textContent).toContain('Blurred')
64 expect(container.textContent).toContain('Validating')
65 expect(container.textContent).toContain('Invalid (hidden)')
66 expect(container.textContent).toContain('Non-default value')
67 expect(container.textContent).not.toContain('Dirty')
68​
69 store.fieldList.applyPatch({
70 formInstanceId: formId,
71 upsert: [{ fieldId, setSummary: { validity: 'invalid' } }],
72 })
73​
74 expect(container.textContent).toContain('Invalid')
75 expect(container.textContent).toContain('Validating')
76 expect(container.textContent).not.toContain('Invalid (hidden)')
77​
78 store.fieldList.applyPatch({
79 formInstanceId: formId,
80 upsert: [
81 {
82 fieldId,
83 clearSummary: [
84 'isTouched',
85 'isBlurred',
86 'isLongValidating',
87 'isDefaultValue',
88 'validity',
89 ],
90 },
91 ],
92 })
93​
94 expect(container.textContent).not.toContain('Touched')
95 expect(container.textContent).not.toContain('Blurred')
96 expect(container.textContent).not.toContain('Validating')
97 expect(container.textContent).not.toContain('Invalid')
98 expect(container.textContent).not.toContain('Non-default value')
99 })
100})