-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorBox.test.tsx
More file actions
71 lines (62 loc) · 2.44 KB
/
Copy pathErrorBox.test.tsx
File metadata and controls
71 lines (62 loc) · 2.44 KB
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
import { describe, expect, it, jest } from '@jest/globals'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useState } from 'react'
import { ErrorBox } from './components'
import { EditableTextField } from './common/editingComponents'
import { DetailContext, type DetailContextType, modeOptionToMode } from './Context/DetailContext'
import { validateLocality } from '@/shared/validators/locality'
import type { EditDataType, LocalityDetailsType } from '@/shared/types'
import { Role } from '@/shared/types/misc'
jest.mock('@/hooks/user', () => ({
useUser: () => ({ role: Role.Admin }),
}))
const ErrorBoxWrapper = () => {
const initialEditData = {
min_age: '',
max_age: 10,
date_meth: 'absolute',
} as unknown as EditDataType<LocalityDetailsType>
const [editData, setEditData] = useState<EditDataType<LocalityDetailsType>>(initialEditData)
const [fieldsWithErrors, setFieldsWithErrors] = useState({})
const contextValue: DetailContextType<LocalityDetailsType> = {
data: initialEditData as unknown as LocalityDetailsType,
mode: modeOptionToMode.new,
setMode: () => undefined,
editData,
setEditData,
isDirty: false,
resetEditData: () => setEditData(initialEditData),
textField: () => <></>,
bigTextField: () => <></>,
dropdown: () => <></>,
dropdownWithSearch: () => <></>,
radioSelection: () => <></>,
validator: (nextEditData, field) => {
if (field === 'min_age') {
return validateLocality(nextEditData, field)
}
return { name: String(field), error: null }
},
fieldsWithErrors,
setFieldsWithErrors: updaterFn => setFieldsWithErrors(updaterFn),
}
return (
<DetailContext.Provider value={contextValue as DetailContextType<unknown>}>
<EditableTextField<LocalityDetailsType> field="min_age" type="number" />
<ErrorBox<LocalityDetailsType> />
</DetailContext.Provider>
)
}
describe('ErrorBox', () => {
it('shows the latest validator error for a number field after typing', async () => {
const user = userEvent.setup()
render(<ErrorBoxWrapper />)
const input = screen.getByRole<HTMLInputElement>('spinbutton')
await user.type(input, '-1')
await waitFor(() => {
expect(screen.getByText('Age (min): Min value must be a positive real value')).not.toBeNull()
})
expect(screen.queryByText('Age (min): This field is required')).toBeNull()
})
})