|
1 | 1 | import * as React from 'react';
|
2 | 2 | import { FunctionComponent, ReactElement } from 'react';
|
3 | 3 | import { render, fireEvent } from '@testing-library/react';
|
4 |
| -import { Form } from 'react-final-form'; |
| 4 | +import { Form, useFormState } from 'react-final-form'; |
5 | 5 | import FormWithRedirect from './FormWithRedirect';
|
6 | 6 | import useInput, { InputProps } from './useInput';
|
7 | 7 | import { required } from './validate';
|
@@ -235,4 +235,97 @@ describe('useInput', () => {
|
235 | 235 | );
|
236 | 236 | expect(queryByDisplayValue('99')).toBeNull();
|
237 | 237 | });
|
| 238 | + |
| 239 | + const BooleanInput = ({ |
| 240 | + source, |
| 241 | + initialValue, |
| 242 | + }: { |
| 243 | + source: string; |
| 244 | + initialValue?: boolean; |
| 245 | + }) => ( |
| 246 | + <Input |
| 247 | + source={source} |
| 248 | + initialValue={initialValue} |
| 249 | + type="checkbox" |
| 250 | + resource="posts" |
| 251 | + > |
| 252 | + {() => <BooleanInputValue source={source} />} |
| 253 | + </Input> |
| 254 | + ); |
| 255 | + |
| 256 | + const BooleanInputValue = ({ source }) => { |
| 257 | + const values = useFormState().values; |
| 258 | + return ( |
| 259 | + <> |
| 260 | + {typeof values[source] === 'undefined' |
| 261 | + ? 'undefined' |
| 262 | + : values[source] |
| 263 | + ? 'true' |
| 264 | + : 'false'} |
| 265 | + </> |
| 266 | + ); |
| 267 | + }; |
| 268 | + |
| 269 | + it('does not change the value if the field is of type checkbox and has no value', () => { |
| 270 | + const { queryByText } = renderWithRedux( |
| 271 | + <FormWithRedirect |
| 272 | + onSubmit={jest.fn()} |
| 273 | + record={{ id: 1 }} |
| 274 | + render={() => <BooleanInput source="is_published" />} |
| 275 | + /> |
| 276 | + ); |
| 277 | + expect(queryByText('undefined')).not.toBeNull(); |
| 278 | + }); |
| 279 | + |
| 280 | + it('applies the initialValue true when the field is of type checkbox and has no value', () => { |
| 281 | + const { queryByText } = renderWithRedux( |
| 282 | + <FormWithRedirect |
| 283 | + onSubmit={jest.fn()} |
| 284 | + record={{ id: 1 }} |
| 285 | + render={() => ( |
| 286 | + <BooleanInput source="is_published" initialValue={true} /> |
| 287 | + )} |
| 288 | + /> |
| 289 | + ); |
| 290 | + expect(queryByText('true')).not.toBeNull(); |
| 291 | + }); |
| 292 | + |
| 293 | + it('applies the initialValue false when the field is of type checkbox and has no value', () => { |
| 294 | + const { queryByText } = renderWithRedux( |
| 295 | + <FormWithRedirect |
| 296 | + onSubmit={jest.fn()} |
| 297 | + record={{ id: 1 }} |
| 298 | + render={() => ( |
| 299 | + <BooleanInput source="is_published" initialValue={false} /> |
| 300 | + )} |
| 301 | + /> |
| 302 | + ); |
| 303 | + expect(queryByText('false')).not.toBeNull(); |
| 304 | + }); |
| 305 | + |
| 306 | + it('does not apply the initialValue true when the field is of type checkbox and has a value', () => { |
| 307 | + const { queryByText } = renderWithRedux( |
| 308 | + <FormWithRedirect |
| 309 | + onSubmit={jest.fn()} |
| 310 | + record={{ id: 1, is_published: false }} |
| 311 | + render={() => ( |
| 312 | + <BooleanInput source="is_published" initialValue={true} /> |
| 313 | + )} |
| 314 | + /> |
| 315 | + ); |
| 316 | + expect(queryByText('false')).not.toBeNull(); |
| 317 | + }); |
| 318 | + |
| 319 | + it('does not apply the initialValue false when the field is of type checkbox and has a value', () => { |
| 320 | + const { queryByText } = renderWithRedux( |
| 321 | + <FormWithRedirect |
| 322 | + onSubmit={jest.fn()} |
| 323 | + record={{ id: 1, is_published: true }} |
| 324 | + render={() => ( |
| 325 | + <BooleanInput source="is_published" initialValue={false} /> |
| 326 | + )} |
| 327 | + /> |
| 328 | + ); |
| 329 | + expect(queryByText('true')).not.toBeNull(); |
| 330 | + }); |
238 | 331 | });
|
0 commit comments