Skip to content

Commit dfe480c

Browse files
committed
Fix remaining test
1 parent 3586d87 commit dfe480c

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

web_ui/src/pages/media/hooks/media-delete/media-delete.hook.test.tsx

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Copyright (C) 2022-2025 Intel Corporation
22
// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33

4-
import { QueryClient } from '@tanstack/react-query';
4+
import { QueryClient, useQueryClient } from '@tanstack/react-query';
55
import { waitFor } from '@testing-library/react';
66

77
import { MEDIA_TYPE } from '../../../../core/media/base-media.interface';
88
import { createInMemoryMediaService } from '../../../../core/media/services/in-memory-media-service/in-memory-media-service';
99
import { MediaService } from '../../../../core/media/services/media-service.interface';
10-
import { NOTIFICATION_TYPE } from '../../../../notification/notification-toast/notification-type.enum';
1110
import { getMockedProjectIdentifier } from '../../../../test-utils/mocked-items-factory/mocked-identifiers';
1211
import { getMockedImageMediaItem } from '../../../../test-utils/mocked-items-factory/mocked-media';
1312
import { renderHookWithProviders } from '../../../../test-utils/render-hook-with-providers';
@@ -16,18 +15,12 @@ import { filterPageMedias } from '../../utils';
1615
import { useDeleteMediaMutation } from './media-delete.hook';
1716

1817
const mockSetQueriesData = jest.fn();
19-
const mockAddNotification = jest.fn();
2018

2119
jest.mock('../../utils', () => ({
2220
...jest.requireActual('../../utils'),
2321
filterPageMedias: jest.fn(),
2422
}));
2523

26-
jest.mock('../../../../notification/notification.component', () => ({
27-
...jest.requireActual('../../../../notification/notification.component'),
28-
useNotification: () => ({ addNotification: mockAddNotification }),
29-
}));
30-
3124
const mockedImageMedia = getMockedImageMediaItem({
3225
name: 'image-1',
3326
identifier: { type: MEDIA_TYPE.IMAGE, imageId: '1111' },
@@ -42,15 +35,22 @@ const renderDeleteMediaMutationHook = ({
4235
}: {
4336
mediaService?: MediaService;
4437
} = {}) => {
45-
const queryClient = new QueryClient();
46-
queryClient.setQueriesData = mockSetQueriesData;
47-
48-
return renderHookWithProviders(useDeleteMediaMutation, {
49-
wrapper: ({ children }) => (
50-
<ProjectProvider projectIdentifier={getMockedProjectIdentifier()}>{children}</ProjectProvider>
51-
),
52-
providerProps: { mediaService, queryClient },
53-
});
38+
let queryClient: QueryClient;
39+
40+
return renderHookWithProviders(
41+
() => {
42+
queryClient = useQueryClient();
43+
queryClient.setQueriesData = mockSetQueriesData;
44+
45+
return useDeleteMediaMutation();
46+
},
47+
{
48+
wrapper: ({ children }) => (
49+
<ProjectProvider projectIdentifier={getMockedProjectIdentifier()}>{children}</ProjectProvider>
50+
),
51+
providerProps: { mediaService },
52+
}
53+
);
5454
};
5555

5656
const mockedResponse = {
@@ -85,7 +85,6 @@ describe('useDeleteMediaMutation', () => {
8585
await waitFor(() => {
8686
expect(filterPageMedias).toHaveBeenCalled();
8787
expect(mockSetQueriesData).toHaveBeenCalledTimes(1);
88-
expect(mockAddNotification).not.toHaveBeenCalled();
8988
});
9089
});
9190

@@ -106,10 +105,6 @@ describe('useDeleteMediaMutation', () => {
106105
await waitFor(() => {
107106
expect(filterPageMedias).toHaveBeenCalled();
108107
expect(mockSetQueriesData).toHaveBeenCalledTimes(2);
109-
expect(mockAddNotification).toHaveBeenCalledWith({
110-
message: `Media cannot be deleted. ${errorMessage}`,
111-
type: NOTIFICATION_TYPE.ERROR,
112-
});
113108
});
114109
});
115110
});

web_ui/src/providers/query-client-provider/query-client-provider.component.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ import { isAxiosError } from 'axios';
1717
import { NOTIFICATION_TYPE } from '../../notification/notification-toast/notification-type.enum';
1818
import { AddNotificationProps, useNotification } from '../../notification/notification.component';
1919

20+
declare module '@tanstack/react-query' {
21+
interface Register {
22+
queryMeta: {
23+
notifyOnError?: boolean;
24+
errorMessage?: string;
25+
disableGlobalErrorHandling?: boolean;
26+
};
27+
mutationMeta: {
28+
notifyOnError?: boolean;
29+
errorMessage?: string;
30+
};
31+
}
32+
}
33+
2034
export const createGetiQueryClient = ({
2135
defaultQueryOptions,
2236
addNotification,
@@ -30,6 +44,7 @@ export const createGetiQueryClient = ({
3044
onError: (error, query) => {
3145
if (isAxiosError(error) && query.meta && 'notifyOnError' in query.meta) {
3246
const message = query.meta.errorMessage;
47+
3348
if (query.meta.notifyOnError === true) {
3449
notify.current({
3550
message: typeof message === 'string' ? message : getErrorMessage(error),
@@ -47,11 +62,10 @@ export const createGetiQueryClient = ({
4762
});
4863

4964
const mutationCache = new MutationCache({
50-
onError: (error) => {
51-
// TODO: If we decide to add an "opt-out" mechanism we can use the same logic
52-
// as query cache and use the `meta` field
53-
if (isAxiosError(error)) {
54-
const message = error.message;
65+
onError: (error, _variables, _ctx, mutation) => {
66+
if (isAxiosError(error) && mutation.meta && 'notifyOnError' in mutation.meta) {
67+
const message = mutation.meta.errorMessage;
68+
5569
notify.current({
5670
message: typeof message === 'string' ? message : getErrorMessage(error),
5771
type: NOTIFICATION_TYPE.ERROR,
@@ -73,19 +87,6 @@ export const createGetiQueryClient = ({
7387
});
7488
};
7589

76-
declare module '@tanstack/react-query' {
77-
interface Register {
78-
queryMeta: {
79-
notifyOnError?: boolean;
80-
errorMessage?: string;
81-
disableGlobalErrorHandling?: boolean;
82-
};
83-
mutationMeta: {
84-
notifyOnError?: boolean;
85-
};
86-
}
87-
}
88-
8990
export const QueryClientProvider = ({
9091
children,
9192
defaultQueryOptions,

0 commit comments

Comments
 (0)