@centreon/ui 26.11.14 → 26.12.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centreon/ui",
3
- "version": "26.11.14",
3
+ "version": "26.12.0",
4
4
  "description": "Centreon UI Components",
5
5
  "scripts": {
6
6
  "update:deps": "pnpx npm-check-updates -i --format group",
@@ -8,6 +8,8 @@ import {
8
8
  resetMocks,
9
9
  waitFor
10
10
  } from '../../../../../test/testRenderer';
11
+ import fetchMock from 'jest-fetch-mock';
12
+
11
13
  import buildListingEndpoint from '../../../../api/buildListingEndpoint';
12
14
  import type {
13
15
  BuildListingEndpointParameters,
@@ -127,4 +129,39 @@ describe(SingleConnectedAutocompleteField, () => {
127
129
  );
128
130
  });
129
131
  });
132
+
133
+ it('leaves no request rejected unhandled when it is closed while its options load', async () => {
134
+ const unhandledRejections: Array<unknown> = [];
135
+ const onUnhandledRejection = (reason: unknown): void => {
136
+ unhandledRejections.push(reason);
137
+ };
138
+
139
+ process.on('unhandledRejection', onUnhandledRejection);
140
+
141
+ try {
142
+ // Options that never arrive: the request is still running at unmount,
143
+ // which cancels it.
144
+ fetchMock.mockResponse(() => new Promise(() => undefined));
145
+
146
+ const { getByLabelText, unmount } =
147
+ renderSingleConnectedAutocompleteField();
148
+
149
+ fireEvent.click(getByLabelText('Open'));
150
+
151
+ await waitFor(() => {
152
+ expect(getFetchCall(0)).toEqual(`${baseEndpoint}?page=1`);
153
+ });
154
+
155
+ unmount();
156
+
157
+ // Unhandled rejections are reported once the microtask queue drains.
158
+ await new Promise((resolve) => {
159
+ setTimeout(resolve, 50);
160
+ });
161
+
162
+ expect(unhandledRejections).toEqual([]);
163
+ } finally {
164
+ process.off('unhandledRejection', onUnhandledRejection);
165
+ }
166
+ });
130
167
  });
@@ -1,6 +1,7 @@
1
1
  import { CircularProgress, useTheme } from '@mui/material';
2
2
  import type { AutocompleteRenderOptionState } from '@mui/material/Autocomplete';
3
3
 
4
+ import { isCancelledError } from '@tanstack/react-query';
4
5
  import {
5
6
  equals,
6
7
  has,
@@ -307,52 +308,62 @@ const ConnectedAutocompleteField = (
307
308
  );
308
309
 
309
310
  const fetchOptionsAndPrefetchNextOptions = useCallback((): void => {
310
- fetchQuery().then((newOptions) => {
311
- const isError = has('isError', newOptions);
312
-
313
- if (isError) {
314
- return;
315
- }
316
-
317
- const moreOptions = page > 1 ? options : [];
318
-
319
- const { result, limit, total } = getOptionResult(newOptions);
320
-
321
- const formattedList = changeIdValue
322
- ? result.map((item) => ({
323
- ...item,
324
- id: changeIdValue(item)
325
- }))
326
- : result;
327
-
328
- if (!isEmpty(labelKey) && !isNil(labelKey)) {
329
- const list = formattedList.map((item) =>
330
- renameKey({ key: labelKey, newKey: 'name', object: item })
331
- );
332
- setOptions(moreOptions.concat(list as Array<TData>));
333
-
334
- return;
335
- }
336
- setOptions(moreOptions.concat(formattedList));
337
-
338
- setOptions(moreOptions.concat(formattedList as Array<TData>));
339
-
340
- const newMaxPage = Math.ceil(total / limit);
341
-
342
- setMaxPage(newMaxPage);
343
- if (equals(newMaxPage, page)) {
344
- return;
345
- }
346
-
347
- prefetchNextPage({
348
- getPrefetchQueryKey: (newPage: number) => [
349
- `autocomplete-${props.label}`,
350
- newPage,
351
- searchParameter
352
- ],
353
- page
311
+ fetchQuery()
312
+ .then((newOptions) => {
313
+ const isError = has('isError', newOptions);
314
+
315
+ if (isError) {
316
+ return;
317
+ }
318
+
319
+ const moreOptions = page > 1 ? options : [];
320
+
321
+ const { result, limit, total } = getOptionResult(newOptions);
322
+
323
+ const formattedList = changeIdValue
324
+ ? result.map((item) => ({
325
+ ...item,
326
+ id: changeIdValue(item)
327
+ }))
328
+ : result;
329
+
330
+ if (!isEmpty(labelKey) && !isNil(labelKey)) {
331
+ const list = formattedList.map((item) =>
332
+ renameKey({ key: labelKey, newKey: 'name', object: item })
333
+ );
334
+ setOptions(moreOptions.concat(list as Array<TData>));
335
+
336
+ return;
337
+ }
338
+ setOptions(moreOptions.concat(formattedList));
339
+
340
+ setOptions(moreOptions.concat(formattedList as Array<TData>));
341
+
342
+ const newMaxPage = Math.ceil(total / limit);
343
+
344
+ setMaxPage(newMaxPage);
345
+ if (equals(newMaxPage, page)) {
346
+ return;
347
+ }
348
+
349
+ prefetchNextPage({
350
+ getPrefetchQueryKey: (newPage: number) => [
351
+ `autocomplete-${props.label}`,
352
+ newPage,
353
+ searchParameter
354
+ ],
355
+ page
356
+ });
357
+ })
358
+ // The request is cancelled when the field unmounts while it loads: there
359
+ // is nothing left to update, and nothing went wrong.
360
+ .catch((error) => {
361
+ if (isCancelledError(error)) {
362
+ return;
363
+ }
364
+
365
+ throw error;
354
366
  });
355
- });
356
367
  }, [
357
368
  changeIdValue,
358
369
  fetchQuery,
@@ -48,6 +48,7 @@ const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
48
48
  return (
49
49
  <MuiIconButton
50
50
  className={classes.iconButton}
51
+ color={muiColorMap[variant]}
51
52
  data-size={size}
52
53
  data-variant={variant}
53
54
  disabled={disabled}
@@ -55,8 +56,6 @@ const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
55
56
  ref={ref}
56
57
  size={size}
57
58
  {...attr}
58
- // Mui overrides
59
- color={muiColorMap[variant]}
60
59
  >
61
60
  {icon}
62
61
  </MuiIconButton>