-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathReferenceArrayInput.spec.tsx
More file actions
306 lines (281 loc) · 10.3 KB
/
ReferenceArrayInput.spec.tsx
File metadata and controls
306 lines (281 loc) · 10.3 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import * as React from 'react';
import {
render,
screen,
waitFor,
within,
fireEvent,
} from '@testing-library/react';
import {
testDataProvider,
useChoicesContext,
CoreAdminContext,
Form,
useInput,
ResourceContextProvider,
} from 'ra-core';
import { QueryClient } from '@tanstack/react-query';
import { AdminContext } from '../AdminContext';
import { SimpleForm } from '../form';
import { DatagridInput } from './DatagridInput';
import { TextField } from '../field';
import { ReferenceArrayInput } from './ReferenceArrayInput';
import { SelectArrayInput } from './SelectArrayInput';
import { AsFilters, DifferentIdTypes } from './ReferenceArrayInput.stories';
describe('<ReferenceArrayInput />', () => {
const defaultProps = {
reference: 'tags',
source: 'tag_ids',
};
afterEach(async () => {
// wait for the getManyAggregate batch to resolve
await waitFor(() => new Promise(resolve => setTimeout(resolve, 0)));
});
it('should display an error if error is defined', async () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
render(
<AdminContext
queryClient={
new QueryClient({
defaultOptions: { queries: { retry: false } },
})
}
dataProvider={testDataProvider({
getList: () => Promise.reject(new Error('fetch error')),
})}
>
<ResourceContextProvider value="posts">
<SimpleForm onSubmit={jest.fn()}>
<ReferenceArrayInput {...defaultProps}>
<SelectArrayInput optionText="name" />
</ReferenceArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);
await waitFor(() => {
expect(screen.queryByText('fetch error')).not.toBeNull();
});
});
it('should pass the correct resource down to child component', async () => {
const MyComponent = () => {
const { resource } = useChoicesContext();
return <div>{resource}</div>;
};
render(
<AdminContext>
<ResourceContextProvider value="posts">
<SimpleForm onSubmit={jest.fn()}>
<ReferenceArrayInput {...defaultProps}>
<MyComponent />
</ReferenceArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);
await waitFor(() => {
expect(screen.queryByText('tags')).not.toBeNull();
});
});
it('should provide a ChoicesContext with all available choices', async () => {
const Children = () => {
const { total } = useChoicesContext({});
return <div aria-label="total">{total}</div>;
};
const dataProvider = testDataProvider({
getList: () =>
// @ts-ignore
Promise.resolve({
data: [{ id: 1 }, { id: 2 }],
total: 2,
}),
});
render(
<AdminContext dataProvider={dataProvider}>
<ResourceContextProvider value="posts">
<SimpleForm onSubmit={jest.fn()}>
<ReferenceArrayInput {...defaultProps}>
<Children />
</ReferenceArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);
await waitFor(() => {
expect(screen.getByLabelText('total').innerHTML).toEqual('2');
});
});
it('should apply default values', async () => {
const MyComponent = () => {
useInput({ source: 'tag_ids', defaultValue: [1, 2] });
const { allChoices } = useChoicesContext();
return <div>{allChoices?.map(item => item.id).join()}</div>;
};
const dataProvider = testDataProvider({
getMany: jest
.fn()
.mockResolvedValue({ data: [{ id: 1 }, { id: 2 }] }),
});
render(
<AdminContext dataProvider={dataProvider}>
<ResourceContextProvider value="posts">
<SimpleForm onSubmit={jest.fn()}>
<ReferenceArrayInput {...defaultProps}>
<MyComponent />
</ReferenceArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);
await waitFor(() => {
expect(dataProvider.getMany).toHaveBeenCalled();
});
await waitFor(() => {
expect(screen.queryByText('1,2')).not.toBeNull();
});
});
it('should allow to use a Datagrid', async () => {
const dataProvider = testDataProvider({
getList: () =>
// @ts-ignore
Promise.resolve({
data: [
{ id: 5, name: 'test1' },
{ id: 6, name: 'test2' },
],
total: 2,
}),
getMany: () =>
// @ts-ignore
Promise.resolve({
data: [{ id: 5, name: 'test1' }],
}),
});
render(
<AdminContext dataProvider={dataProvider}>
<ResourceContextProvider value="posts">
<SimpleForm
onSubmit={jest.fn()}
defaultValues={{ tag_ids: [5] }}
>
<ReferenceArrayInput reference="tags" source="tag_ids">
<DatagridInput>
<TextField source="name" />
</DatagridInput>
</ReferenceArrayInput>
</SimpleForm>
</ResourceContextProvider>
</AdminContext>
);
await waitFor(() => {
screen.getByText('test1');
screen.getByText('test2');
});
const getCheckbox1 = () =>
within(screen.queryByText('test1').closest('tr'))
.getByLabelText('ra.action.select_row')
.querySelector('input');
const getCheckbox2 = () =>
within(screen.queryByText('test2').closest('tr'))
.getByLabelText('ra.action.select_row')
.querySelector('input');
const getCheckboxAll = () =>
screen.getByLabelText('ra.action.select_all');
await waitFor(() => {
expect(getCheckbox1()?.checked).toEqual(true);
expect(getCheckbox2()?.checked).toEqual(false);
});
fireEvent.click(getCheckbox2());
await waitFor(() => {
expect(getCheckbox1()?.checked).toEqual(true);
expect(getCheckbox2()?.checked).toEqual(true);
expect(getCheckboxAll().checked).toEqual(true);
});
fireEvent.click(getCheckboxAll());
await waitFor(() => {
expect(getCheckbox1()?.checked).toEqual(false);
expect(getCheckbox2()?.checked).toEqual(false);
expect(getCheckboxAll().checked).toEqual(false);
});
fireEvent.click(getCheckboxAll());
await waitFor(() => {
expect(getCheckbox1()?.checked).toEqual(true);
expect(getCheckbox2()?.checked).toEqual(true);
expect(getCheckboxAll().checked).toEqual(true);
});
});
it('should accept meta in queryOptions', async () => {
const getList = jest
.fn()
.mockImplementationOnce(() =>
Promise.resolve({ data: [], total: 25 })
);
const dataProvider = testDataProvider({ getList });
render(
<CoreAdminContext dataProvider={dataProvider}>
<Form>
<ReferenceArrayInput
{...defaultProps}
queryOptions={{ meta: { foo: 'bar' } }}
>
<SelectArrayInput optionText="name" />
</ReferenceArrayInput>
</Form>
</CoreAdminContext>
);
await waitFor(() => {
expect(getList).toHaveBeenCalledWith('tags', {
filter: {},
pagination: { page: 1, perPage: 25 },
sort: { field: 'id', order: 'DESC' },
meta: { foo: 'bar' },
signal: undefined,
});
});
});
it('should support different types of ids', async () => {
render(<DifferentIdTypes />);
await screen.findByText('artist_1', {
selector: 'div.MuiChip-root .MuiChip-label',
});
expect(
screen.queryByText('artist_2', {
selector: 'div.MuiChip-root .MuiChip-label',
})
).not.toBeNull();
expect(
screen.queryByText('artist_3', { selector: 'div.MuiChip-root' })
).toBeNull();
});
it('should unselect a value when types of ids are different', async () => {
render(<DifferentIdTypes />);
const chip1 = await screen.findByText('artist_1', {
selector: '.MuiChip-label',
});
const chip2 = await screen.findByText('artist_2', {
selector: '.MuiChip-label',
});
if (chip2.nextSibling) fireEvent.click(chip2.nextSibling);
expect(
screen.queryByText('artist_2', {
selector: '.MuiChip-label',
})
).toBeNull();
if (chip1.nextSibling) fireEvent.click(chip1.nextSibling);
expect(
screen.queryByText('artist_1', {
selector: '.MuiChip-label',
})
).toBeNull();
});
it('can be used as a list filter', async () => {
render(<AsFilters />);
await screen.findByText('band_2');
fireEvent.click(screen.getByText('Members'));
fireEvent.click(screen.getByText('artist_2'));
await waitFor(() => {
expect(screen.queryByText('band_2')).toBeNull();
});
await screen.findByText('band_1');
});
});