|
| 1 | +import * as React from 'react'; |
| 2 | +import expect from 'expect'; |
| 3 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import { required, testDataProvider } from 'ra-core'; |
| 5 | +import { format } from 'date-fns'; |
| 6 | +import { useFormState } from 'react-hook-form'; |
| 7 | + |
| 8 | +import { AdminContext } from '../AdminContext'; |
| 9 | +import { SimpleForm, Toolbar } from '../form'; |
| 10 | +import { TimeInput } from './TimeInput'; |
| 11 | +import { ArrayInput, SimpleFormIterator } from './ArrayInput'; |
| 12 | +import { SaveButton } from '../button'; |
| 13 | + |
| 14 | +describe('<TimeInput />', () => { |
| 15 | + const defaultProps = { |
| 16 | + resource: 'posts', |
| 17 | + source: 'publishedAt', |
| 18 | + }; |
| 19 | + |
| 20 | + it('should render a time input', () => { |
| 21 | + render( |
| 22 | + <AdminContext dataProvider={testDataProvider()}> |
| 23 | + <SimpleForm onSubmit={jest.fn()}> |
| 24 | + <TimeInput {...defaultProps} /> |
| 25 | + </SimpleForm> |
| 26 | + </AdminContext> |
| 27 | + ); |
| 28 | + const input = screen.getByLabelText( |
| 29 | + 'resources.posts.fields.publishedAt' |
| 30 | + ) as HTMLInputElement; |
| 31 | + expect(input.type).toBe('time'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should not make the form dirty on initialization', () => { |
| 35 | + const publishedAt = new Date(); |
| 36 | + const FormState = () => { |
| 37 | + const { isDirty } = useFormState(); |
| 38 | + |
| 39 | + return <p>Dirty: {isDirty.toString()}</p>; |
| 40 | + }; |
| 41 | + render( |
| 42 | + <AdminContext dataProvider={testDataProvider()}> |
| 43 | + <SimpleForm |
| 44 | + onSubmit={jest.fn()} |
| 45 | + record={{ id: 1, publishedAt: publishedAt.toISOString() }} |
| 46 | + > |
| 47 | + <TimeInput {...defaultProps} /> |
| 48 | + <FormState /> |
| 49 | + </SimpleForm> |
| 50 | + </AdminContext> |
| 51 | + ); |
| 52 | + expect(screen.getByDisplayValue(format(publishedAt, 'HH:mm'))); |
| 53 | + expect(screen.queryByText('Dirty: false')).not.toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should display a default value inside an ArrayInput', () => { |
| 57 | + const date = new Date('Wed Oct 05 2011 16:48:00 GMT+0200'); |
| 58 | + const backlinksDefaultValue = [ |
| 59 | + { |
| 60 | + date, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + render( |
| 64 | + <AdminContext dataProvider={testDataProvider()}> |
| 65 | + <SimpleForm onSubmit={jest.fn()}> |
| 66 | + <ArrayInput |
| 67 | + defaultValue={backlinksDefaultValue} |
| 68 | + source="backlinks" |
| 69 | + > |
| 70 | + <SimpleFormIterator> |
| 71 | + <TimeInput source="date" /> |
| 72 | + </SimpleFormIterator> |
| 73 | + </ArrayInput> |
| 74 | + </SimpleForm> |
| 75 | + </AdminContext> |
| 76 | + ); |
| 77 | + |
| 78 | + expect(screen.getByDisplayValue(format(date, 'HH:mm'))); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should submit the form default value with its timezone', async () => { |
| 82 | + const publishedAt = new Date('Wed Oct 05 2011 16:48:00 GMT+0200'); |
| 83 | + const onSubmit = jest.fn(); |
| 84 | + render( |
| 85 | + <AdminContext dataProvider={testDataProvider()}> |
| 86 | + <SimpleForm |
| 87 | + onSubmit={onSubmit} |
| 88 | + defaultValues={{ publishedAt }} |
| 89 | + toolbar={ |
| 90 | + <Toolbar> |
| 91 | + <SaveButton alwaysEnable /> |
| 92 | + </Toolbar> |
| 93 | + } |
| 94 | + > |
| 95 | + <TimeInput {...defaultProps} /> |
| 96 | + </SimpleForm> |
| 97 | + </AdminContext> |
| 98 | + ); |
| 99 | + expect( |
| 100 | + screen.queryByDisplayValue(format(publishedAt, 'HH:mm')) |
| 101 | + ).not.toBeNull(); |
| 102 | + fireEvent.click(screen.getByLabelText('ra.action.save')); |
| 103 | + await waitFor(() => { |
| 104 | + expect(onSubmit).toHaveBeenCalledWith({ |
| 105 | + publishedAt, |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should submit the input default value with its timezone', async () => { |
| 111 | + const publishedAt = new Date('Wed Oct 05 2011 16:48:00 GMT+0200'); |
| 112 | + const onSubmit = jest.fn(); |
| 113 | + render( |
| 114 | + <AdminContext dataProvider={testDataProvider()}> |
| 115 | + <SimpleForm |
| 116 | + onSubmit={onSubmit} |
| 117 | + toolbar={ |
| 118 | + <Toolbar> |
| 119 | + <SaveButton alwaysEnable /> |
| 120 | + </Toolbar> |
| 121 | + } |
| 122 | + > |
| 123 | + <TimeInput {...defaultProps} defaultValue={publishedAt} /> |
| 124 | + </SimpleForm> |
| 125 | + </AdminContext> |
| 126 | + ); |
| 127 | + expect( |
| 128 | + screen.queryByDisplayValue(format(publishedAt, 'HH:mm')) |
| 129 | + ).not.toBeNull(); |
| 130 | + fireEvent.click(screen.getByLabelText('ra.action.save')); |
| 131 | + await waitFor(() => { |
| 132 | + expect(onSubmit).toHaveBeenCalledWith({ |
| 133 | + publishedAt, |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('error message', () => { |
| 139 | + it('should not be displayed if field is pristine', () => { |
| 140 | + render( |
| 141 | + <AdminContext dataProvider={testDataProvider()}> |
| 142 | + <SimpleForm onSubmit={jest.fn()}> |
| 143 | + <TimeInput {...defaultProps} validate={required()} /> |
| 144 | + </SimpleForm> |
| 145 | + </AdminContext> |
| 146 | + ); |
| 147 | + expect(screen.queryByText('ra.validation.required')).toBeNull(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should be displayed if field has been touched and is invalid', async () => { |
| 151 | + render( |
| 152 | + <AdminContext dataProvider={testDataProvider()}> |
| 153 | + <SimpleForm onSubmit={jest.fn()} mode="onBlur"> |
| 154 | + <TimeInput {...defaultProps} validate={required()} /> |
| 155 | + </SimpleForm> |
| 156 | + </AdminContext> |
| 157 | + ); |
| 158 | + const input = screen.getByLabelText( |
| 159 | + 'resources.posts.fields.publishedAt *' |
| 160 | + ); |
| 161 | + fireEvent.blur(input); |
| 162 | + await waitFor(() => { |
| 163 | + expect( |
| 164 | + screen.queryByText('ra.validation.required') |
| 165 | + ).not.toBeNull(); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should be displayed if field has been touched multiple times and is invalid', async () => { |
| 170 | + const onSubmit = jest.fn(); |
| 171 | + render( |
| 172 | + <AdminContext dataProvider={testDataProvider()}> |
| 173 | + <SimpleForm onSubmit={onSubmit}> |
| 174 | + <TimeInput {...defaultProps} validate={required()} /> |
| 175 | + </SimpleForm> |
| 176 | + </AdminContext> |
| 177 | + ); |
| 178 | + const input = screen.getByLabelText( |
| 179 | + 'resources.posts.fields.publishedAt *' |
| 180 | + ); |
| 181 | + fireEvent.change(input, { |
| 182 | + target: { value: new Date().toISOString() }, |
| 183 | + }); |
| 184 | + fireEvent.blur(input); |
| 185 | + await waitFor(() => { |
| 186 | + expect(screen.queryByText('ra.validation.required')).toBeNull(); |
| 187 | + }); |
| 188 | + fireEvent.change(input, { |
| 189 | + target: { value: '' }, |
| 190 | + }); |
| 191 | + fireEvent.blur(input); |
| 192 | + fireEvent.click(screen.getByText('ra.action.save')); |
| 193 | + await waitFor(() => { |
| 194 | + expect( |
| 195 | + screen.queryByText('ra.validation.required') |
| 196 | + ).not.toBeNull(); |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments