forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateRow.test.tsx
More file actions
80 lines (66 loc) · 2.18 KB
/
CreateRow.test.tsx
File metadata and controls
80 lines (66 loc) · 2.18 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
72
73
74
75
76
77
78
79
80
import React from 'react';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { fireEvent, render, screen } from '@testing-library/react';
import CreateRow from './CreateRow';
const wrapper = ({ children }: { children: React.ReactNode; }) => (
<IntlProvider locale="en" messages={{}}>{children}</IntlProvider>
);
const baseProps = () => ({
draftError: '',
setDraftError: jest.fn(),
handleCreateRow: jest.fn(),
setIsCreatingTopRow: jest.fn(),
exitDraftWithoutSave: jest.fn(),
createRowMutation: { isPending: false },
validate: jest.fn((value: string) => value.trim().length > 0),
});
describe('CreateRow', () => {
it('saves on Enter when value is valid', () => {
const props = baseProps();
render(
<table>
<tbody>
<CreateRow {...(props as any)} />
</tbody>
</table>,
{ wrapper },
);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: ' new tag ' } });
fireEvent.keyDown(input, { key: 'Enter' });
expect(props.handleCreateRow).toHaveBeenCalledWith('new tag');
});
it('does not save on Enter when mutation is pending', () => {
const props = baseProps();
props.createRowMutation = { isPending: true };
render(
<table>
<tbody>
<CreateRow {...(props as any)} />
</tbody>
</table>,
{ wrapper },
);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: 'pending tag' } });
fireEvent.keyDown(input, { key: 'Enter' });
expect(props.handleCreateRow).not.toHaveBeenCalled();
});
it('cancels on Escape and resets draft state', () => {
const props = baseProps();
render(
<table>
<tbody>
<CreateRow {...(props as any)} />
</tbody>
</table>,
{ wrapper },
);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: 'will cancel' } });
fireEvent.keyDown(input, { key: 'Escape' });
expect(props.setDraftError).toHaveBeenCalledWith('');
expect(props.setIsCreatingTopRow).toHaveBeenCalledWith(false);
expect(props.exitDraftWithoutSave).toHaveBeenCalled();
});
});