@centreon/ui 26.12.1 → 26.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -3
- package/public/mockServiceWorker.js +31 -8
- package/src/Dashboard/Dashboard.browser.test.tsx +107 -0
- package/src/Form/Form.browser.test.tsx +445 -0
- package/src/Form/Inputs/ExclusiveCheckboxGroup.browser.test.tsx +304 -0
- package/src/Form/Inputs/SegmentedButtons.browser.test.tsx +113 -0
- package/src/InputField/Number/Number.browser.test.tsx +109 -0
- package/src/InputField/Select/Autocomplete/Connected/Multi/MultiConnectedAutocompleteField.browser.test.tsx +250 -0
- package/src/Listing/Listing.browser.test.tsx +476 -0
- package/src/Module/Module.browser.test.tsx +145 -0
- package/src/Pagination/Pagination.browser.test.tsx +184 -0
- package/src/RichTextEditor/RichTextEditor.browser.test.tsx +567 -0
- package/src/RichTextEditor/plugins/FloatingLinkEditorPlugin.tsx +3 -1
- package/src/TimePeriods/ResolutionTimePeriod.browser.test.tsx +143 -0
- package/src/TimePeriods/TimePeriods.browser.test.tsx +468 -0
- package/src/Typography/FluidTypography/FluidTypography.browser.test.tsx +54 -0
- package/src/api/useMutationQuery/useMutationQuery.browser.test.tsx +154 -0
- package/src/components/CollapsibleItem/CollapsibleItem.browser.test.tsx +120 -0
- package/src/components/CopyCommand/CopyCommand.browser.test.tsx +287 -0
- package/src/components/CrudPage/CrudPage.browser.test.tsx +659 -0
- package/src/components/DataTable/DataTable.browser.test.tsx +257 -0
- package/src/components/Form/AccessRights/AccessRights.browser.test.tsx +527 -0
- package/src/components/ItemComposition/ItemComposition.browser.test.tsx +172 -0
- package/src/components/Layout/PageLayout.browser.test.tsx +114 -0
- package/src/components/Modal/ConfirmationModal/ConfirmationModal.browser.test.tsx +237 -0
- package/src/components/SortableEntriesList/SortableEntriesList.browser.test.tsx +981 -0
- package/src/components/Tabs/Tabs.browser.test.tsx +115 -0
- package/src/utils/useFullscreen/Fullscreen.browser.test.tsx +176 -0
- package/src/utils/useLocale/useLocale.browser.test.tsx +49 -0
- package/src/utils/useLocaleTimezoneDate/{LocaleTimezoneDate.cypress.spec.tsx → LocaleTimezoneDate.browser.test.tsx} +24 -11
- package/src/Dashboard/Dashboard.cypress.spec.tsx +0 -71
- package/src/Form/Form.cypress.spec.tsx +0 -306
- package/src/Form/Inputs/ExclusiveCheckboxGroup.cypress.spec.tsx +0 -236
- package/src/Form/Inputs/SegmentedButtons.cypress.spec.tsx +0 -101
- package/src/InputField/Number/Number.cypress.spec.tsx +0 -89
- package/src/InputField/Select/Autocomplete/Connected/Multi/MultiConnectedAutocompleteField.cypress.spec.tsx +0 -182
- package/src/Listing/Listing.cypress.spec.tsx +0 -349
- package/src/Module/Module.cypress.spec.tsx +0 -127
- package/src/Pagination/Pagination.cypress.spec.tsx +0 -140
- package/src/RichTextEditor/RichTextEditor.cypress.spec.tsx +0 -408
- package/src/TimePeriods/ResolutionTimePeriod.cypress.spec.tsx +0 -74
- package/src/TimePeriods/TimePeriods.cypress.spec.tsx +0 -488
- package/src/Typography/FluidTypography/FluidTypography.cypress.spec.tsx +0 -27
- package/src/api/useMutationQuery/useMutationQuery.cypress.spec.tsx +0 -93
- package/src/components/CollapsibleItem/CollapsibleItem.cypress.spec.tsx +0 -76
- package/src/components/CopyCommand/CopyCommand.cypress.spec.tsx +0 -167
- package/src/components/CrudPage/CrudPage.cypress.spec.tsx +0 -561
- package/src/components/DataTable/DataTable.cypress.spec.tsx +0 -174
- package/src/components/Form/AccessRights/AccessRights.cypress.spec.tsx +0 -376
- package/src/components/ItemComposition/ItemComposition.cypress.spec.tsx +0 -112
- package/src/components/Layout/PageLayout.cypress.spec.tsx +0 -64
- package/src/components/Modal/ConfirmationModal/ConfirmationModal.cypress.spec.tsx +0 -171
- package/src/components/SortableEntriesList/SortableEntriesList.cypress.spec.tsx +0 -792
- package/src/components/Tabs/Tabs.cypress.spec.tsx +0 -69
- package/src/utils/useFullscreen/Fullscreen.cypress.spec.tsx +0 -130
- package/src/utils/useLocale/useLocale.cypress.spec.tsx +0 -40
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
import { FormControlLabel, Switch, Typography } from '@mui/material';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getRequestCalls,
|
|
5
|
+
interceptAPIRequest,
|
|
6
|
+
waitForRequest
|
|
7
|
+
} from '@test-setup/api';
|
|
8
|
+
import { render } from '@test-setup/render';
|
|
9
|
+
import { screen, waitFor, within } from '@testing-library/react';
|
|
10
|
+
import userEvent from '@testing-library/user-event';
|
|
11
|
+
import { atom, createStore, Provider, useAtom, useSetAtom } from 'jotai';
|
|
12
|
+
import { identity, isNil } from 'ramda';
|
|
13
|
+
import { type ChangeEvent, useEffect } from 'react';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
type Column,
|
|
17
|
+
ColumnType,
|
|
18
|
+
Method,
|
|
19
|
+
type SearchParameter,
|
|
20
|
+
SnackbarProvider,
|
|
21
|
+
TestQueryProvider
|
|
22
|
+
} from '../..';
|
|
23
|
+
import { CrudPage } from '.';
|
|
24
|
+
import { CrudPageRoot } from './CrudPageRoot';
|
|
25
|
+
import type { CrudPageRootProps } from './models';
|
|
26
|
+
|
|
27
|
+
interface Item {
|
|
28
|
+
id: number;
|
|
29
|
+
name: string;
|
|
30
|
+
description: string;
|
|
31
|
+
subItems: Array<{ id: number; name: string }>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Filters {
|
|
35
|
+
hasDescription: boolean;
|
|
36
|
+
isEven: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const generateItems = (count: number): Array<Item> =>
|
|
40
|
+
Array(count)
|
|
41
|
+
.fill(0)
|
|
42
|
+
.map((_, idx) => ({
|
|
43
|
+
description: `Description ${idx}`,
|
|
44
|
+
id: idx,
|
|
45
|
+
name: `Item ${idx}`,
|
|
46
|
+
subItems: [{ id: 1, name: 'SubItem' }]
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
const filtersAtom = atom<Filters>({
|
|
50
|
+
hasDescription: true,
|
|
51
|
+
isEven: false
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const Filters = (): JSX.Element => {
|
|
55
|
+
const [filters, setFilters] = useAtom(filtersAtom);
|
|
56
|
+
|
|
57
|
+
const change =
|
|
58
|
+
(property: string) =>
|
|
59
|
+
(event: ChangeEvent<HTMLInputElement>): void => {
|
|
60
|
+
setFilters((current) => ({
|
|
61
|
+
...current,
|
|
62
|
+
[property]: event.target.checked
|
|
63
|
+
}));
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<>
|
|
68
|
+
<FormControlLabel
|
|
69
|
+
control={
|
|
70
|
+
<Switch
|
|
71
|
+
checked={filters.hasDescription}
|
|
72
|
+
onChange={change('hasDescription')}
|
|
73
|
+
/>
|
|
74
|
+
}
|
|
75
|
+
label="Has description"
|
|
76
|
+
/>
|
|
77
|
+
<FormControlLabel
|
|
78
|
+
control={
|
|
79
|
+
<Switch checked={filters.isEven} onChange={change('isEven')} />
|
|
80
|
+
}
|
|
81
|
+
label="Is even"
|
|
82
|
+
/>
|
|
83
|
+
</>
|
|
84
|
+
);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const getSearchParameters = ({
|
|
88
|
+
filters
|
|
89
|
+
}: {
|
|
90
|
+
filters: Filters;
|
|
91
|
+
}): SearchParameter =>
|
|
92
|
+
({
|
|
93
|
+
conditions: [
|
|
94
|
+
{
|
|
95
|
+
field: 'hasDescription',
|
|
96
|
+
values: {
|
|
97
|
+
$in: filters.hasDescription
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
field: 'isEven',
|
|
102
|
+
values: {
|
|
103
|
+
$in: filters.isEven
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
}) as unknown as SearchParameter;
|
|
108
|
+
|
|
109
|
+
const labels = {
|
|
110
|
+
actions: {
|
|
111
|
+
create: 'Create item'
|
|
112
|
+
},
|
|
113
|
+
listing: {
|
|
114
|
+
search: 'Search'
|
|
115
|
+
},
|
|
116
|
+
title: 'Items',
|
|
117
|
+
welcome: {
|
|
118
|
+
description: 'This page handles item',
|
|
119
|
+
title: 'Welcome to the items page'
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const columns: Array<Column> = [
|
|
124
|
+
{
|
|
125
|
+
displaySubItemsCaret: true,
|
|
126
|
+
getFormattedString: (row): string => (row as unknown as Item).name,
|
|
127
|
+
id: 'name',
|
|
128
|
+
label: 'Name',
|
|
129
|
+
type: ColumnType.string
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
getFormattedString: (row): string => (row as unknown as Item).description,
|
|
133
|
+
id: 'description',
|
|
134
|
+
label: 'Description',
|
|
135
|
+
type: ColumnType.string
|
|
136
|
+
}
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
const defaultProps: CrudPageRootProps<Item, Filters, Item, Item> = {
|
|
140
|
+
baseEndpoint: '/listing',
|
|
141
|
+
columns,
|
|
142
|
+
deleteItem: {
|
|
143
|
+
deleteEndpoint: (item) =>
|
|
144
|
+
!isNil(item?.parent)
|
|
145
|
+
? `/listing/${item?.parent?.id}/subItems/${item?.id}`
|
|
146
|
+
: `/listing/${item?.id}`,
|
|
147
|
+
labels: {
|
|
148
|
+
cancel: 'Cancel',
|
|
149
|
+
confirm: 'Delete',
|
|
150
|
+
description: (item) =>
|
|
151
|
+
!isNil(item?.parent) ? (
|
|
152
|
+
<Typography>
|
|
153
|
+
The sub item <strong>{item?.name}</strong> from the item{' '}
|
|
154
|
+
<strong>{item?.parent?.name}</strong> will be deleted
|
|
155
|
+
</Typography>
|
|
156
|
+
) : (
|
|
157
|
+
<Typography>
|
|
158
|
+
The item <strong>{item?.name}</strong> will be deleted
|
|
159
|
+
</Typography>
|
|
160
|
+
),
|
|
161
|
+
successMessage: (item) =>
|
|
162
|
+
!isNil(item?.parent) ? 'Sub item deleted' : 'Item deleted',
|
|
163
|
+
title: (item) =>
|
|
164
|
+
!isNil(item?.parent) ? 'Delete sub item' : 'Delete item'
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
filters: <Filters />,
|
|
168
|
+
filtersAtom,
|
|
169
|
+
form: {
|
|
170
|
+
Form: ({ initialValues }) => {
|
|
171
|
+
const [askBeforeCloseForm, setAskBeforeCloseFormModal] = useAtom(
|
|
172
|
+
CrudPage.askBeforeCloseFormModalAtom
|
|
173
|
+
);
|
|
174
|
+
const setOpenFormModal = useSetAtom(CrudPage.openFormModalAtom);
|
|
175
|
+
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
if (!askBeforeCloseForm) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
setOpenFormModal(null);
|
|
182
|
+
setAskBeforeCloseFormModal(false);
|
|
183
|
+
}, [askBeforeCloseForm, setAskBeforeCloseFormModal, setOpenFormModal]);
|
|
184
|
+
|
|
185
|
+
return (
|
|
186
|
+
<Typography>
|
|
187
|
+
This is a placeholder for the form
|
|
188
|
+
<br />
|
|
189
|
+
Initial values: {JSON.stringify(initialValues)}
|
|
190
|
+
</Typography>
|
|
191
|
+
);
|
|
192
|
+
},
|
|
193
|
+
getItem: {
|
|
194
|
+
adapter: identity,
|
|
195
|
+
baseEndpoint: (id) => `/item/${id}`,
|
|
196
|
+
itemQueryKey: 'item'
|
|
197
|
+
},
|
|
198
|
+
labels: {
|
|
199
|
+
add: {
|
|
200
|
+
cancel: 'Cancel',
|
|
201
|
+
confirm: 'Add',
|
|
202
|
+
title: 'Add item'
|
|
203
|
+
},
|
|
204
|
+
update: {
|
|
205
|
+
cancel: 'Cancel',
|
|
206
|
+
confirm: 'Update',
|
|
207
|
+
title: 'Update item'
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
getSearchParameters,
|
|
212
|
+
labels,
|
|
213
|
+
queryKeyName: 'listing'
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const listing = {
|
|
217
|
+
meta: {
|
|
218
|
+
limit: 30,
|
|
219
|
+
page: 1,
|
|
220
|
+
total: 60
|
|
221
|
+
},
|
|
222
|
+
result: generateItems(30)
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const emptyListing = {
|
|
226
|
+
meta: {
|
|
227
|
+
limit: 30,
|
|
228
|
+
page: 1,
|
|
229
|
+
total: 0
|
|
230
|
+
},
|
|
231
|
+
result: []
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const basicLabels = {
|
|
235
|
+
cancel: 'Cancel',
|
|
236
|
+
confirm: 'Delete',
|
|
237
|
+
description: 'A small description',
|
|
238
|
+
successMessage: 'This is a success',
|
|
239
|
+
title: 'Title'
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const subItems = {
|
|
243
|
+
canCheckSubItems: false,
|
|
244
|
+
enable: true,
|
|
245
|
+
getRowProperty: (): string => 'subItems',
|
|
246
|
+
labelCollapse: 'Collapse',
|
|
247
|
+
labelExpand: 'Expand'
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const initialize = (
|
|
251
|
+
props: CrudPageRootProps<Item, Filters, Item, Item>
|
|
252
|
+
): void => {
|
|
253
|
+
interceptAPIRequest({
|
|
254
|
+
alias: 'getEmptyListing',
|
|
255
|
+
method: Method.GET,
|
|
256
|
+
path: '**/empty-listing**',
|
|
257
|
+
response: emptyListing
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
interceptAPIRequest({
|
|
261
|
+
alias: 'getListing',
|
|
262
|
+
method: Method.GET,
|
|
263
|
+
path: '**/listing**',
|
|
264
|
+
response: listing
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
interceptAPIRequest({
|
|
268
|
+
alias: 'deleteItem',
|
|
269
|
+
method: Method.DELETE,
|
|
270
|
+
path: '**/listing/1',
|
|
271
|
+
response: {}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
interceptAPIRequest({
|
|
275
|
+
alias: 'deleteSubItem',
|
|
276
|
+
method: Method.DELETE,
|
|
277
|
+
path: '**/listing/0/subItems/1',
|
|
278
|
+
response: {}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
interceptAPIRequest({
|
|
282
|
+
alias: 'getItem',
|
|
283
|
+
method: Method.GET,
|
|
284
|
+
path: '**/item/0',
|
|
285
|
+
response: generateItems(1)[0]
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const store = createStore();
|
|
289
|
+
|
|
290
|
+
render(
|
|
291
|
+
<div style={{ height: '95vh' }}>
|
|
292
|
+
<Provider store={store}>
|
|
293
|
+
<SnackbarProvider>
|
|
294
|
+
<TestQueryProvider>
|
|
295
|
+
<CrudPageRoot {...props} />
|
|
296
|
+
</TestQueryProvider>
|
|
297
|
+
</SnackbarProvider>
|
|
298
|
+
</Provider>
|
|
299
|
+
</div>
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// The deepest element whose whole text is the given one, even when it is
|
|
304
|
+
// split across elements (`<strong>`).
|
|
305
|
+
const byTextContent =
|
|
306
|
+
(text: string) =>
|
|
307
|
+
(_content: string, element: Element | null): boolean =>
|
|
308
|
+
element?.textContent === text &&
|
|
309
|
+
Array.from(element.children).every((child) => child.textContent !== text);
|
|
310
|
+
|
|
311
|
+
const formPlaceholder = /^This is a placeholder for the formInitial values:/;
|
|
312
|
+
|
|
313
|
+
// The modal closes once the deletion succeeded: the confirmation is gone and
|
|
314
|
+
// the listing is still there.
|
|
315
|
+
const expectDeleteModalClosed = async (title: string): Promise<void> => {
|
|
316
|
+
await waitFor(() => {
|
|
317
|
+
expect(screen.queryByText(title)).not.toBeInTheDocument();
|
|
318
|
+
});
|
|
319
|
+
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
|
320
|
+
expect(screen.getByText('Item 1')).toBeVisible();
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
describe('CrudPage', () => {
|
|
324
|
+
beforeEach(() => {
|
|
325
|
+
expect.hasAssertions();
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('displays a welcome message when no data are retrieved', async () => {
|
|
329
|
+
initialize({
|
|
330
|
+
...defaultProps,
|
|
331
|
+
baseEndpoint: '/empty-listing',
|
|
332
|
+
queryKeyName: 'empty-listing'
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
await waitForRequest('@getEmptyListing');
|
|
336
|
+
|
|
337
|
+
expect(await screen.findByText('Welcome to the items page')).toBeVisible();
|
|
338
|
+
expect(screen.getByText('This page handles item')).toBeVisible();
|
|
339
|
+
expect(screen.getByText('Items')).toBeVisible();
|
|
340
|
+
expect(
|
|
341
|
+
screen.getByText('Create item', { selector: 'button' })
|
|
342
|
+
).toBeVisible();
|
|
343
|
+
|
|
344
|
+
// The welcome state replaces the listing.
|
|
345
|
+
expect(screen.queryAllByTestId('Search')).toHaveLength(0);
|
|
346
|
+
expect(screen.queryByRole('table')).not.toBeInTheDocument();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('opens the form modal when no data are retrieved and the corresponding button is clicked', async () => {
|
|
350
|
+
initialize({
|
|
351
|
+
...defaultProps,
|
|
352
|
+
baseEndpoint: '/empty-listing',
|
|
353
|
+
queryKeyName: 'empty-listing'
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
await waitForRequest('@getEmptyListing');
|
|
357
|
+
|
|
358
|
+
expect(await screen.findByText('Welcome to the items page')).toBeVisible();
|
|
359
|
+
expect(screen.getByText('This page handles item')).toBeVisible();
|
|
360
|
+
expect(screen.getByText('Items')).toBeVisible();
|
|
361
|
+
await userEvent.click(
|
|
362
|
+
screen.getByText('Create item', { selector: 'button' })
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
expect(await screen.findByText('Add item')).toBeVisible();
|
|
366
|
+
expect(
|
|
367
|
+
screen.getByText('This is a placeholder for the form', { exact: false })
|
|
368
|
+
).toBeVisible();
|
|
369
|
+
expect(screen.getByText('Initial values:', { exact: false })).toBeVisible();
|
|
370
|
+
|
|
371
|
+
// The add form starts without initial values.
|
|
372
|
+
const dialog = screen.getByRole('dialog');
|
|
373
|
+
|
|
374
|
+
expect(within(dialog).getByText('Add item')).toBeVisible();
|
|
375
|
+
expect(
|
|
376
|
+
within(dialog).getByText(formPlaceholder, { exact: false })
|
|
377
|
+
).toHaveTextContent(/^This is a placeholder for the formInitial values:$/);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('displays items when items are retrieved', async () => {
|
|
381
|
+
initialize(defaultProps);
|
|
382
|
+
|
|
383
|
+
const { request } = await waitForRequest('@getListing');
|
|
384
|
+
const { searchParams } = request.url;
|
|
385
|
+
|
|
386
|
+
expect(searchParams.get('page')).toBe('1');
|
|
387
|
+
expect(searchParams.get('limit')).toBe('10');
|
|
388
|
+
expect(searchParams.get('sort_by')).toBe('{"name":"asc"}');
|
|
389
|
+
expect(searchParams.get('search')).toBe(
|
|
390
|
+
'{"$and":[{"$or":[{"name":{"$rg":""}}]},{"$and":[{"$or":[{"hasDescription":{"$in":true}}]},{"$or":[{"isEven":{"$in":false}}]}]}]}'
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
expect(await screen.findByText('Items')).toBeVisible();
|
|
394
|
+
expect(
|
|
395
|
+
await screen.findByText('Create item', { selector: 'button' })
|
|
396
|
+
).toBeVisible();
|
|
397
|
+
expect(await screen.findByText('Item 0')).toBeVisible();
|
|
398
|
+
expect(screen.getByText('Description 0')).toBeVisible();
|
|
399
|
+
|
|
400
|
+
// The column headers, the pagination of the 60 items, and the edit and
|
|
401
|
+
// delete actions of the first and last rows.
|
|
402
|
+
expect(screen.getByText('Name')).toBeVisible();
|
|
403
|
+
expect(screen.getByText('Description')).toBeVisible();
|
|
404
|
+
expect(screen.getByText('1-10 of 60')).toBeVisible();
|
|
405
|
+
expect(screen.getByTestId('edit-0')).toBeVisible();
|
|
406
|
+
expect(screen.getByTestId('delete-0')).toBeVisible();
|
|
407
|
+
expect(screen.getByTestId('edit-29')).toBeInTheDocument();
|
|
408
|
+
expect(screen.getByTestId('delete-29')).toBeInTheDocument();
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it('sends a request when filters and search are updated', async () => {
|
|
412
|
+
initialize(defaultProps);
|
|
413
|
+
|
|
414
|
+
await userEvent.type(
|
|
415
|
+
(await screen.findAllByTestId('Search'))[1],
|
|
416
|
+
'simple search'
|
|
417
|
+
);
|
|
418
|
+
await userEvent.click(screen.getByLabelText('filters'));
|
|
419
|
+
await userEvent.click(await screen.findByLabelText('Has description'));
|
|
420
|
+
await userEvent.click(screen.getByLabelText('Is even'));
|
|
421
|
+
|
|
422
|
+
expect(screen.getAllByTestId('Search')[1]).toHaveValue('simple search');
|
|
423
|
+
|
|
424
|
+
await userEvent.click(screen.getByLabelText('filters'));
|
|
425
|
+
|
|
426
|
+
await waitFor(() => {
|
|
427
|
+
expect(
|
|
428
|
+
screen.queryByLabelText('Has description')
|
|
429
|
+
).not.toBeInTheDocument();
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// The search is debounced (500 ms): rather than a fixed wait, wait for
|
|
433
|
+
// the latest answered request to carry the new search.
|
|
434
|
+
await waitFor(() => {
|
|
435
|
+
expect(
|
|
436
|
+
getRequestCalls('@getListing')
|
|
437
|
+
.filter(({ answered }) => answered)
|
|
438
|
+
.at(-1)
|
|
439
|
+
?.request.url.searchParams.get('search')
|
|
440
|
+
).toBe(
|
|
441
|
+
'{"$and":[{"$or":[{"name":{"$rg":"simple search"}}]},{"$and":[{"$or":[{"hasDescription":{"$in":false}}]},{"$or":[{"isEven":{"$in":true}}]}]}]}'
|
|
442
|
+
);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
expect(await screen.findByText('Item 0')).toBeVisible();
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it('displays the add modal when data are retrieved and the corresponding is clicked', async () => {
|
|
449
|
+
initialize(defaultProps);
|
|
450
|
+
|
|
451
|
+
await waitForRequest('@getListing');
|
|
452
|
+
|
|
453
|
+
await userEvent.click(
|
|
454
|
+
await screen.findByText('Create item', { selector: 'button' })
|
|
455
|
+
);
|
|
456
|
+
|
|
457
|
+
expect(await screen.findByText('Add item')).toBeVisible();
|
|
458
|
+
expect(
|
|
459
|
+
screen.getByText('This is a placeholder for the form', { exact: false })
|
|
460
|
+
).toBeVisible();
|
|
461
|
+
expect(screen.getByText('Initial values:', { exact: false })).toBeVisible();
|
|
462
|
+
|
|
463
|
+
// The add form starts without initial values.
|
|
464
|
+
expect(
|
|
465
|
+
within(screen.getByRole('dialog')).getByText(formPlaceholder)
|
|
466
|
+
).toHaveTextContent(/^This is a placeholder for the formInitial values:$/);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe('Delete with advanced labels', () => {
|
|
470
|
+
it('deletes an item when items are retrieved and the corresponding button is clicked', async () => {
|
|
471
|
+
initialize(defaultProps);
|
|
472
|
+
|
|
473
|
+
await userEvent.click(await screen.findByTestId('delete-1'));
|
|
474
|
+
|
|
475
|
+
expect(await screen.findByText('Delete item')).toBeVisible();
|
|
476
|
+
expect(
|
|
477
|
+
screen.getByText(byTextContent('The item Item 1 will be deleted'))
|
|
478
|
+
).toBeVisible();
|
|
479
|
+
|
|
480
|
+
await userEvent.click(
|
|
481
|
+
within(screen.getByRole('dialog')).getByRole('button', {
|
|
482
|
+
name: 'Delete'
|
|
483
|
+
})
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
await waitForRequest('@deleteItem');
|
|
487
|
+
|
|
488
|
+
expect(await screen.findByText('Item deleted')).toBeVisible();
|
|
489
|
+
|
|
490
|
+
await expectDeleteModalClosed('Delete item');
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it('deletes a sub-item when items are retrieved and the corresponding button is clicked', async () => {
|
|
494
|
+
initialize({
|
|
495
|
+
...defaultProps,
|
|
496
|
+
subItems
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
await userEvent.click(await screen.findByTestId('Expand 0'));
|
|
500
|
+
await userEvent.click(await screen.findByTestId('delete-0-1'));
|
|
501
|
+
|
|
502
|
+
expect(await screen.findByText('Delete sub item')).toBeVisible();
|
|
503
|
+
expect(
|
|
504
|
+
screen.getByText(
|
|
505
|
+
byTextContent(
|
|
506
|
+
'The sub item SubItem from the item Item 0 will be deleted'
|
|
507
|
+
)
|
|
508
|
+
)
|
|
509
|
+
).toBeVisible();
|
|
510
|
+
|
|
511
|
+
await userEvent.click(
|
|
512
|
+
within(screen.getByRole('dialog')).getByRole('button', {
|
|
513
|
+
name: 'Delete'
|
|
514
|
+
})
|
|
515
|
+
);
|
|
516
|
+
|
|
517
|
+
await waitForRequest('@deleteSubItem');
|
|
518
|
+
|
|
519
|
+
expect(await screen.findByText('Sub item deleted')).toBeVisible();
|
|
520
|
+
|
|
521
|
+
await expectDeleteModalClosed('Delete sub item');
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
describe('Delete with basic labels', () => {
|
|
526
|
+
it('deletes an item when items are retrieved and the corresponding button is clicked', async () => {
|
|
527
|
+
initialize({
|
|
528
|
+
...defaultProps,
|
|
529
|
+
deleteItem: {
|
|
530
|
+
...defaultProps.deleteItem,
|
|
531
|
+
labels: basicLabels
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
await userEvent.click(await screen.findByTestId('delete-1'));
|
|
536
|
+
|
|
537
|
+
expect(await screen.findByText('Title')).toBeVisible();
|
|
538
|
+
expect(screen.getByText('A small description')).toBeVisible();
|
|
539
|
+
|
|
540
|
+
await userEvent.click(
|
|
541
|
+
within(screen.getByRole('dialog')).getByRole('button', {
|
|
542
|
+
name: 'Delete'
|
|
543
|
+
})
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
await waitForRequest('@deleteItem');
|
|
547
|
+
|
|
548
|
+
expect(await screen.findByText('This is a success')).toBeVisible();
|
|
549
|
+
|
|
550
|
+
await expectDeleteModalClosed('Title');
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('deletes a sub-item when items are retrieved and the corresponding button is clicked', async () => {
|
|
554
|
+
initialize({
|
|
555
|
+
...defaultProps,
|
|
556
|
+
deleteItem: {
|
|
557
|
+
...defaultProps.deleteItem,
|
|
558
|
+
labels: basicLabels
|
|
559
|
+
},
|
|
560
|
+
subItems
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
await userEvent.click(await screen.findByTestId('Expand 0'));
|
|
564
|
+
await userEvent.click(await screen.findByTestId('delete-0-1'));
|
|
565
|
+
|
|
566
|
+
expect(await screen.findByText('Title')).toBeVisible();
|
|
567
|
+
expect(screen.getByText('A small description')).toBeVisible();
|
|
568
|
+
|
|
569
|
+
await userEvent.click(
|
|
570
|
+
within(screen.getByRole('dialog')).getByRole('button', {
|
|
571
|
+
name: 'Delete'
|
|
572
|
+
})
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
await waitForRequest('@deleteSubItem');
|
|
576
|
+
|
|
577
|
+
expect(await screen.findByText('This is a success')).toBeVisible();
|
|
578
|
+
|
|
579
|
+
await expectDeleteModalClosed('Title');
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it('cannot delete a sub-item when items are retrieved and the corresponding button is clicked', async () => {
|
|
584
|
+
initialize({
|
|
585
|
+
...defaultProps,
|
|
586
|
+
deleteItem: {
|
|
587
|
+
...defaultProps.deleteItem,
|
|
588
|
+
labels: basicLabels
|
|
589
|
+
},
|
|
590
|
+
subItems: {
|
|
591
|
+
...subItems,
|
|
592
|
+
canDeleteSubItems: false
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
await userEvent.click(await screen.findByTestId('Expand 0'));
|
|
597
|
+
|
|
598
|
+
// The sub-item is displayed, without any action,
|
|
599
|
+
// and its parent keeps its own.
|
|
600
|
+
expect(await screen.findByText('SubItem')).toBeVisible();
|
|
601
|
+
expect(screen.queryByTestId('delete-0-1')).not.toBeInTheDocument();
|
|
602
|
+
expect(screen.queryByTestId('edit-0-1')).not.toBeInTheDocument();
|
|
603
|
+
expect(screen.getByTestId('edit-0')).toBeVisible();
|
|
604
|
+
expect(screen.getByTestId('delete-0')).toBeVisible();
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
it('opens the update form when the corresponding button is clicked', async () => {
|
|
608
|
+
initialize(defaultProps);
|
|
609
|
+
|
|
610
|
+
await userEvent.click(await screen.findByTestId('edit-0'));
|
|
611
|
+
|
|
612
|
+
await waitForRequest('@getItem');
|
|
613
|
+
|
|
614
|
+
expect(await screen.findByText('Update item')).toBeVisible();
|
|
615
|
+
expect(
|
|
616
|
+
screen.getByText('This is a placeholder for the form', { exact: false })
|
|
617
|
+
).toBeVisible();
|
|
618
|
+
expect(
|
|
619
|
+
await screen.findByText(
|
|
620
|
+
'Initial values: {"description":"Description 0","id":0,"name":"Item 0","subItems":[{"id":1,"name":"SubItem"}]}',
|
|
621
|
+
{ exact: false }
|
|
622
|
+
)
|
|
623
|
+
).toBeVisible();
|
|
624
|
+
|
|
625
|
+
// The update form is a dialog over the listing.
|
|
626
|
+
expect(
|
|
627
|
+
within(screen.getByRole('dialog')).getByText('Update item')
|
|
628
|
+
).toBeVisible();
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it('closes the update form when the update ubtton is clicked and the corresponding button is clicked', async () => {
|
|
632
|
+
initialize(defaultProps);
|
|
633
|
+
|
|
634
|
+
await userEvent.click(await screen.findByTestId('edit-0'));
|
|
635
|
+
|
|
636
|
+
await waitForRequest('@getItem');
|
|
637
|
+
|
|
638
|
+
expect(await screen.findByText('Update item')).toBeVisible();
|
|
639
|
+
expect(
|
|
640
|
+
screen.getByText('This is a placeholder for the form', { exact: false })
|
|
641
|
+
).toBeVisible();
|
|
642
|
+
expect(
|
|
643
|
+
await screen.findByText(
|
|
644
|
+
'Initial values: {"description":"Description 0","id":0,"name":"Item 0","subItems":[{"id":1,"name":"SubItem"}]}',
|
|
645
|
+
{ exact: false }
|
|
646
|
+
)
|
|
647
|
+
).toBeVisible();
|
|
648
|
+
|
|
649
|
+
await userEvent.click(screen.getByLabelText('close'));
|
|
650
|
+
|
|
651
|
+
await waitFor(() => {
|
|
652
|
+
expect(screen.queryByText('Update item')).not.toBeInTheDocument();
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
// The listing is back, without any dialog.
|
|
656
|
+
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
|
657
|
+
expect(screen.getByText('Item 0')).toBeVisible();
|
|
658
|
+
});
|
|
659
|
+
});
|