-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathArrayField.spec.tsx
More file actions
214 lines (187 loc) · 6.62 KB
/
ArrayField.spec.tsx
File metadata and controls
214 lines (187 loc) · 6.62 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import * as React from 'react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import {
CoreAdminContext,
ResourceContextProvider,
testDataProvider,
} from 'ra-core';
import { ThemeProvider, createTheme } from '@mui/material';
import { ArrayField } from './ArrayField';
import { NumberField } from './NumberField';
import { TextField } from './TextField';
import { Datagrid } from '../list';
import { SimpleList } from '../list';
import { ListContext, TwoArrayFieldsSelection } from './ArrayField.stories';
beforeAll(() => {
jest.spyOn(console, 'error').mockImplementation((message, ...args) => {
if (
typeof message === 'string' &&
message.includes('React will try recreating this component tree')
) {
return;
}
console.warn(message, ...args); // Still log other errors
});
});
afterAll(() => {
jest.restoreAllMocks();
});
describe('<ArrayField />', () => {
const sort = { field: 'id', order: 'ASC' };
const DummyIterator = props => (
<Datagrid {...props} sort={sort}>
<NumberField source="id" />
<TextField source="foo" />
</Datagrid>
);
const Wrapper = ({ children }) => (
<ThemeProvider theme={createTheme()}>
<CoreAdminContext dataProvider={testDataProvider()}>
<ResourceContextProvider value="posts">
{children}
</ResourceContextProvider>
</CoreAdminContext>
</ThemeProvider>
);
it('should not fail for empty records', () => {
render(
<Wrapper>
<ArrayField
// @ts-expect-error source prop does not have a valid value
source="arr"
record={{ id: 123 }}
>
<DummyIterator />
</ArrayField>
</Wrapper>
);
});
it('should not fail when value is null', () => {
render(
<Wrapper>
<ArrayField source="arr" record={{ id: 123, arr: null }}>
<DummyIterator />
</ArrayField>
</Wrapper>
);
});
it('should render the alternative empty component', () => {
const { queryByText } = render(
<Wrapper>
<ArrayField
source="arr"
record={{
id: 123,
arr: [],
}}
>
<Datagrid empty={<div>No posts</div>}>
<NumberField source="id" />
</Datagrid>
</ArrayField>
</Wrapper>
);
expect(queryByText('No posts')).not.toBeNull();
});
it('should render the <Datagrid> iterator component', () => {
const { queryByText } = render(
<Wrapper>
<ArrayField
source="arr"
record={{
id: 123,
arr: [
{ id: 123, foo: 'bar' },
{ id: 456, foo: 'baz' },
],
}}
>
<DummyIterator />
</ArrayField>
</Wrapper>
);
// Test the datagrid know about the fields
expect(queryByText('resources.posts.fields.id')).not.toBeNull();
expect(queryByText('resources.posts.fields.foo')).not.toBeNull();
// Test the fields values
expect(queryByText('bar')).not.toBeNull();
expect(queryByText('123')).not.toBeNull();
expect(queryByText('baz')).not.toBeNull();
expect(queryByText('456')).not.toBeNull();
});
it('should render the <SimpleList> iterator component', () => {
const { queryByText } = render(
<Wrapper>
<ArrayField
source="arr"
record={{
id: 123,
arr: [
{ id: 123, foo: 'bar' },
{ id: 456, foo: 'baz' },
],
}}
>
<SimpleList
primaryText={record => record.foo}
secondaryText={record => record.id}
/>
</ArrayField>
</Wrapper>
);
// Test the fields values
expect(queryByText('bar')).not.toBeNull();
expect(queryByText('123')).not.toBeNull();
expect(queryByText('baz')).not.toBeNull();
expect(queryByText('456')).not.toBeNull();
});
it('should create a ListContext with working callbacks', async () => {
render(<ListContext />);
screen.getByText('War and Peace');
screen.getByText('Filter by title').click();
await waitFor(() => {
expect(screen.queryByText('War and Peace')).toBeNull();
});
const chip = screen.getByText('Resurrection');
expect(
(chip.parentNode as HTMLElement).className.includes(
'MuiChip-colorDefault'
)
).toBeTruthy();
chip.click();
await waitFor(() => {
expect(
(chip.parentNode as HTMLElement).className.includes(
'MuiChip-colorPrimary'
)
).toBeTruthy();
});
});
it('should not select the same id in both ArrayFields when selected in one', async () => {
render(<TwoArrayFieldsSelection />);
await waitFor(() => {
expect(screen.queryAllByRole('checkbox').length).toBeGreaterThan(2);
});
const checkboxes = screen.queryAllByRole('checkbox');
expect(checkboxes.length).toBeGreaterThan(3);
// Select an item in the memberships list
fireEvent.click(checkboxes[1]);
render(<TwoArrayFieldsSelection />);
await waitFor(() => {
expect(checkboxes[1]).toBeChecked();
});
expect(checkboxes[3]).not.toBeChecked();
fireEvent.click(checkboxes[3]); // Portfolios row 1
render(<TwoArrayFieldsSelection />);
await waitFor(() => {
expect(checkboxes[3]).toBeChecked();
expect(checkboxes[1]).toBeChecked(); // Membership remains checked
});
fireEvent.click(checkboxes[1]);
render(<TwoArrayFieldsSelection />);
await waitFor(() => {
expect(checkboxes[1]).not.toBeChecked();
expect(checkboxes[3]).toBeChecked(); // Portfolios remain selected
});
});
});