|
1 | 1 | import * as React from 'react';
|
2 | 2 | import expect from 'expect';
|
3 |
| -import { render, screen } from '@testing-library/react'; |
| 3 | +import { fireEvent, render, screen } from '@testing-library/react'; |
4 | 4 | import { testDataProvider } from 'ra-core';
|
5 | 5 |
|
6 | 6 | import { AdminContext } from '../AdminContext';
|
7 | 7 | import { SimpleForm } from './SimpleForm';
|
8 | 8 | import { TextInput } from '../input';
|
| 9 | +import { GlobalValidation, InputBasedValidation } from './SimpleForm.stories'; |
9 | 10 |
|
10 | 11 | describe('<SimpleForm />', () => {
|
11 | 12 | it('should embed a form with given component children', () => {
|
@@ -36,4 +37,66 @@ describe('<SimpleForm />', () => {
|
36 | 37 | );
|
37 | 38 | expect(screen.queryByLabelText('ra.action.save')).not.toBeNull();
|
38 | 39 | });
|
| 40 | + |
| 41 | + describe('validation', () => { |
| 42 | + it('should support translations with global validation', async () => { |
| 43 | + const mock = jest |
| 44 | + .spyOn(console, 'warn') |
| 45 | + .mockImplementation(() => {}); |
| 46 | + render(<GlobalValidation />); |
| 47 | + fireEvent.change(await screen.findByLabelText('Title'), { |
| 48 | + target: { value: '' }, |
| 49 | + }); |
| 50 | + fireEvent.change(await screen.findByLabelText('Author'), { |
| 51 | + target: { value: '' }, |
| 52 | + }); |
| 53 | + fireEvent.change(await screen.findByLabelText('Year'), { |
| 54 | + target: { value: '2003' }, |
| 55 | + }); |
| 56 | + fireEvent.click(await screen.findByLabelText('Save')); |
| 57 | + await screen.findByText('The title is required'); |
| 58 | + await screen.findByText('The author is required'); |
| 59 | + await screen.findByText('The year must be less than 2000'); |
| 60 | + expect(mock).toHaveBeenCalledWith( |
| 61 | + "Missing translation for key 'The title is required'" |
| 62 | + ); |
| 63 | + expect(mock).not.toHaveBeenCalledWith( |
| 64 | + "Missing translation for key 'The author is required'" |
| 65 | + ); |
| 66 | + expect(mock).not.toHaveBeenCalledWith( |
| 67 | + "Missing translation for key 'The year must be less than 2000'" |
| 68 | + ); |
| 69 | + mock.mockRestore(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should support translations with per input validation', async () => { |
| 73 | + const mock = jest |
| 74 | + .spyOn(console, 'warn') |
| 75 | + .mockImplementation(() => {}); |
| 76 | + render(<InputBasedValidation />); |
| 77 | + fireEvent.change(await screen.findByLabelText('Title *'), { |
| 78 | + target: { value: '' }, |
| 79 | + }); |
| 80 | + fireEvent.change(await screen.findByLabelText('Author *'), { |
| 81 | + target: { value: '' }, |
| 82 | + }); |
| 83 | + fireEvent.change(await screen.findByLabelText('Year'), { |
| 84 | + target: { value: '2003' }, |
| 85 | + }); |
| 86 | + fireEvent.click(await screen.findByLabelText('Save')); |
| 87 | + await screen.findByText('The title is required'); |
| 88 | + await screen.findByText('The author is required'); |
| 89 | + await screen.findByText('The year must be less than 2000'); |
| 90 | + expect(mock).toHaveBeenCalledWith( |
| 91 | + "Missing translation for key 'The title is required'" |
| 92 | + ); |
| 93 | + expect(mock).not.toHaveBeenCalledWith( |
| 94 | + "Missing translation for key 'The author is required'" |
| 95 | + ); |
| 96 | + expect(mock).not.toHaveBeenCalledWith( |
| 97 | + "Missing translation for key 'The year must be less than 2000'" |
| 98 | + ); |
| 99 | + mock.mockRestore(); |
| 100 | + }); |
| 101 | + }); |
39 | 102 | });
|
0 commit comments