Skip to content

Commit 6bd7306

Browse files
committed
Add unit tests
1 parent a3009b6 commit 6bd7306

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
4+
import { Basic } from './InPlaceEditor.stories';
5+
6+
describe('InPlaceEditor', () => {
7+
it('should render the field value on mount', async () => {
8+
render(<Basic delay={0} />);
9+
await screen.findByText('John Doe');
10+
});
11+
it('should reveal an input on click', async () => {
12+
render(<Basic delay={0} />);
13+
const value = await screen.findByText('John Doe');
14+
value.click();
15+
await screen.findByDisplayValue('John Doe');
16+
});
17+
it('should let the user change the value', async () => {
18+
render(<Basic delay={0} />);
19+
const value = await screen.findByText('John Doe');
20+
value.click();
21+
const input = await screen.findByDisplayValue('John Doe');
22+
fireEvent.change(input, { target: { value: 'Jane Doe' } });
23+
fireEvent.blur(input);
24+
await screen.findByText('Jane Doe');
25+
});
26+
it('should revert to the previous version on error', async () => {
27+
render(<Basic delay={0} updateFails />);
28+
const value = await screen.findByText('John Doe');
29+
value.click();
30+
const input = await screen.findByDisplayValue('John Doe');
31+
fireEvent.change(input, { target: { value: 'Jane Doe' } });
32+
fireEvent.blur(input);
33+
await screen.findByText('Jane Doe');
34+
await screen.findByText('John Doe');
35+
});
36+
describe('notifyOnSuccess', () => {
37+
it('should show a notification on success', async () => {
38+
render(<Basic delay={0} notifyOnSuccess />);
39+
const value = await screen.findByText('John Doe');
40+
value.click();
41+
const input = await screen.findByDisplayValue('John Doe');
42+
fireEvent.change(input, { target: { value: 'Jane Doe' } });
43+
fireEvent.blur(input);
44+
await screen.findByText('Element updated');
45+
});
46+
});
47+
describe('showButtons', () => {
48+
it('should render save and cancel buttons', async () => {
49+
render(<Basic delay={0} showButtons />);
50+
const value = await screen.findByText('John Doe');
51+
value.click();
52+
await screen.findByLabelText('Save');
53+
await screen.findByLabelText('Cancel');
54+
});
55+
});
56+
});

packages/ra-ui-materialui/src/input/InPlaceEditor/InPlaceEditor.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export const Basic = ({
6363
mutationMode,
6464
notifyOnSuccess,
6565
showButtons,
66+
}: {
67+
delay?: number;
68+
updateFails?: boolean;
69+
mutationMode?: 'optimistic' | 'pessimistic' | 'undoable';
70+
notifyOnSuccess?: boolean;
71+
showButtons?: boolean;
6672
}) => {
6773
const dataProvider = fakeRestDataProvider(
6874
{ users: [{ id: 1, name: 'John Doe', age: 25, type: 'customer' }] },

0 commit comments

Comments
 (0)