Open History to see the last commit
1import { Show } from 'solid-js'
2import { render } from 'solid-js/web'
3import { afterEach, describe, expect, it } from 'vitest'
4import { FieldDetailCard } from '../src/components/fields/fieldDetails/FieldDetailCard'
5import {
6 FormDevtoolsStoreProvider,
7 useFormDevtoolsStore,
8} from '../src/stores/formDevtoolsStore'
9import { connectTestEventBus } from './testEventBus'
10import type { FormDevtoolsStore } from '../src/stores/formDevtoolsStore'
11import type { DevtoolsFieldDetail } from '../src/eventClientTypes'
12import type { FormId } from '../src/types/branded'
13β
14const disposers: Array<() => void> = []
15β
16afterEach(() => {
17 for (const dispose of disposers.splice(0)) dispose()
18})
19β
20function CardHarness(props: { capture: (store: FormDevtoolsStore) => void }) {
21 const store = useFormDevtoolsStore()
22 props.capture(store)
23β
24 return (
25 <Show when={store.fieldList.selectedFieldRow()}>
26 {(field) => <FieldDetailCard field={field()} />}
27 </Show>
28 )
29}
30β
31describe('FieldDetailCard', () => {
32 it('renders subscribed details while sourcing badges from field meta', () => {
33 const disconnectEventBus = connectTestEventBus()
34 const container = document.createElement('div')
35 let store!: FormDevtoolsStore
36 const dispose = render(
37 () => (
38 <FormDevtoolsStoreProvider>
39 <CardHarness capture={(value) => (store = value)} />
40 </FormDevtoolsStoreProvider>
41 ),
42 container,
43 )
44 disposers.push(dispose, disconnectEventBus)
45β
46 const formInstanceId = 'form-a' as FormId
47 store.fieldList.setSubscribedFormId(formInstanceId)
48 store.fieldList.applySnapshot({
49 formInstanceId,
50 fields: [
51 {
52 fieldId: 'field-name',
53 path: 'user.name',
54 summary: { validity: 'invalidHidden' },
55 },
56 ],
57 })
58 store.fieldList.setSelectedFieldPath('user.name')
59β
60 expect(container.querySelector('[data-slot="skeleton"]')).not.toBeNull()
61β
62 const detail = {
63 formInstanceId,
64 fieldId: 'field-name',
65 settings: store.fieldDetails.getFieldDetailSettings('field-name'),
66 state: {
67 value: 'Grace',
68 meta: {
69 isTouched: true,
70 isDirty: true,
71 isPristine: false,
72 isDefaultValue: false,
73 isBlurred: false,
74 isValidating: false,
75 isSelfTouched: true,
76 isSelfDirty: true,
77 isSelfValidating: false,
78 isSelfValid: true,
79 isValid: true,
80 isInvalid: false,
81 subfields: {
82 isEveryValid: true,
83 isAnyInvalid: false,
84 isEveryPristine: true,
85 isSomeDirty: false,
86 isSomeTouched: false,
87 isSomeValidating: false,
88 },
89 errors: [],
90 original: {
91 errors: [
92 {
93 error: { message: 'First hidden error' },
94 source: {
95 scope: 'field',
96 validatorIndex: 0,
97 validatorType: 'callback',
98 },
99 sourceEvent: 'change',
100 },
101 {
102 error: { message: 'Second hidden error' },
103 source: {
104 scope: 'field',
105 validatorIndex: 0,
106 validatorType: 'callback',
107 },
108 sourceEvent: 'change',
109 },
110 ],
111 isValid: false,
112 isInvalid: true,
113 },
114 },
115 },
116 relations: {
117 directChildCount: 0,
118 listensTo: [],
119 listenedToBy: [],
120 },
121 defaultValue: 'Ada',
122 } satisfies DevtoolsFieldDetail
123β
124 store.fieldDetails.applyDetail(detail)
125β
126 expect(container.querySelector('[data-slot="skeleton"]')).toBeNull()
127 expect(container.textContent).toContain('Grace')
128 expect(container.textContent).toContain('Ada')
129 expect(container.textContent).toContain('Invalid (hidden)')
130 expect(
131 Array.from(container.querySelectorAll('[data-slot="item-title"]')).map(
132 (element) => element.textContent,
133 ),
134 ).toEqual(['First hidden error', 'Second hidden error'])
135 expect(
136 Array.from(
137 container.querySelectorAll('[data-slot="item-description"]'),
138 ).map((description) =>
139 Array.from(description.querySelectorAll('span')).map(
140 (label) => label.textContent,
141 ),
142 ),
143 ).toEqual([
144 ['Field', 'Callback', 'change'],
145 ['Field', 'Callback', 'change'],
146 ])
147 expect(container.textContent).toContain('Pristine')
148 expect(container.textContent).not.toContain('Dirty')
149 expect(store.fieldDetails.getFieldDetail('field-name')?.state.meta).toEqual(
150 detail.state.meta,
151 )
152β
153 store.fieldList.applyPatch({
154 formInstanceId,
155 upsert: [
156 {
157 fieldId: 'field-name',
158 setSummary: {
159 isDirty: true,
160 isTouched: true,
161 isDefaultValue: false,
162 validity: 'valid',
163 },
164 },
165 ],
166 })
167β
168 expect(container.textContent).toContain('Valid')
169 expect(container.textContent).toContain('Dirty')
170 expect(container.textContent).toContain('Touched')
171 expect(container.textContent).toContain('Non-default value')
172 expect(container.textContent).not.toContain('Invalid (hidden)')
173 expect(container.querySelector('[aria-label="Debug field"]')).toBeNull()
174β
175 store.fieldDetails.applyDetail({
176 ...detail,
177 state: {
178 ...detail.state,
179 meta: {
180 ...detail.state.meta,
181 errors: [],
182 original: { errors: [], isValid: true, isInvalid: false },
183 },
184 },
185 })
186 expect(container.querySelector('[aria-label="Debug field"]')).not.toBeNull()
187β
188 store.fieldList.applyPatch({
189 formInstanceId,
190 upsert: [{ fieldId: 'field-name', isMounted: false }],
191 })
192 expect(container.querySelector('[aria-label="Debug field"]')).toBeNull()
193 })
194})