-
Notifications
You must be signed in to change notification settings - Fork 395
/
StudioFormBuilder.test.tsx
129 lines (113 loc) · 3.59 KB
/
StudioFormBuilder.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable camelcase */
import {SanityClient} from '@sanity/client'
import {defineType, Path} from '@sanity/types'
import {render} from '@testing-library/react'
import React, {useMemo} from 'react'
import {createMockSanityClient} from '../../../test/mocks/mockSanityClient'
import {createTestProvider} from '../../../test/testUtils/TestProvider'
import {createPatchChannel} from '../patch'
import {useFormState} from '../store/useFormState'
import {EMPTY_ARRAY} from '../utils/empty'
import {useWorkspace} from '../../studio'
import {StudioFormBuilder, StudioFormBuilderProps} from './StudioFormBuilder'
const schemaTypes = [
defineType({
type: 'document',
name: 'test',
title: 'Test',
fields: [
{
type: 'string',
name: 'title',
title: 'Title',
},
],
}),
]
describe('StudioFormBuilder', () => {
it('should render a studio form', async () => {
const client = createMockSanityClient() as unknown as SanityClient
const TestProvider = await createTestProvider({
client,
config: {
name: 'default',
projectId: 'test',
dataset: 'test',
schema: {types: schemaTypes},
},
})
const focusPath: Path = []
const openPath: Path = []
const value = {_id: 'test', _type: 'test'}
const onChange = jest.fn()
const onFieldGroupSelect = jest.fn()
const onPathBlur = jest.fn()
const onPathFocus = jest.fn()
const onPathOpen = jest.fn()
const onSelectFieldGroup = jest.fn()
const onSetFieldSetCollapsed = jest.fn()
const onSetPathCollapsed = jest.fn()
function TestForm() {
const {schema} = useWorkspace()
const schemaType = schema.get('test')
if (!schemaType) {
throw new Error('missing schema type')
}
if (schemaType.jsonType !== 'object') {
throw new Error('schema type is not an object')
}
const patchChannel = useMemo(() => createPatchChannel(), [])
const formState = useFormState(schemaType, {
value,
comparisonValue: value,
focusPath,
collapsedPaths: undefined,
collapsedFieldSets: undefined,
fieldGroupState: undefined,
openPath,
presence: [],
validation: [],
})
const formBuilderProps: StudioFormBuilderProps = useMemo(
() => ({
__internal_patchChannel: patchChannel,
changesOpen: false,
changed: false,
collapsedFieldSets: undefined,
collapsedPaths: undefined,
focused: formState?.focused,
focusPath: formState?.focusPath || EMPTY_ARRAY,
groups: formState?.groups || EMPTY_ARRAY,
id: formState?.id || '',
level: formState?.level || 0,
members: formState?.members || EMPTY_ARRAY,
onChange,
onFieldGroupSelect,
onPathBlur,
onPathFocus,
onPathOpen,
onSelectFieldGroup,
onSetFieldSetCollapsed,
onSetPathCollapsed,
path: EMPTY_ARRAY,
presence: EMPTY_ARRAY,
schemaType: formState?.schemaType || schemaType,
validation: EMPTY_ARRAY,
value: formState?.value,
}),
[formState, patchChannel, schemaType]
)
return <StudioFormBuilder {...formBuilderProps} />
}
const result = render(
<TestProvider>
<TestForm />
</TestProvider>
)
const titleField = await result.findByTestId('field-title')
expect(removeClasses(titleField.outerHTML)).toMatchSnapshot()
})
})
function removeClasses(html: string) {
return html.replace(/\s+class=".*?"/g, '')
}