replace axiosInstance in the useBulkRemove hook to replace the api call with the useFetchClient get method and update test

This commit is contained in:
Simone Taeggi 2023-01-25 16:26:04 +01:00
parent 40dcc501d4
commit 8f0366eac8
2 changed files with 22 additions and 23 deletions

View File

@ -4,9 +4,8 @@ import { QueryClientProvider, QueryClient, useQueryClient } from 'react-query';
import { renderHook, act } from '@testing-library/react-hooks';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { NotificationsProvider, useNotification } from '@strapi/helper-plugin';
import { NotificationsProvider, useNotification, useFetchClient } from '@strapi/helper-plugin';
import { axiosInstance } from '../../utils';
import { useBulkRemove } from '../useBulkRemove';
const FIXTURE_ASSETS = [
@ -33,9 +32,12 @@ const FIXTURE_FOLDERS = [
},
];
jest.mock('../../utils', () => ({
...jest.requireActual('../../utils'),
axiosInstance: {
const notificationStatusMock = jest.fn();
jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'),
useNotification: () => notificationStatusMock,
useFetchClient: jest.fn().mockReturnValue({
post: jest.fn((url, payload) => {
const res = { data: { data: {} } };
@ -49,14 +51,7 @@ jest.mock('../../utils', () => ({
return Promise.resolve(res);
}),
},
}));
const notificationStatusMock = jest.fn();
jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'),
useNotification: () => notificationStatusMock,
}),
}));
const refetchQueriesMock = jest.fn();
@ -111,15 +106,13 @@ describe('useBulkRemove', () => {
result: { current },
} = await setup();
const { remove } = current;
const { post } = useFetchClient();
await act(async () => {
await remove(FIXTURE_ASSETS);
});
expect(axiosInstance.post).toHaveBeenCalledWith(
'/upload/actions/bulk-delete',
expect.any(Object)
);
expect(post).toHaveBeenCalledWith('/upload/actions/bulk-delete', expect.any(Object));
});
test('does properly collect all asset ids', async () => {
@ -127,12 +120,13 @@ describe('useBulkRemove', () => {
result: { current },
} = await setup();
const { remove } = current;
const { post } = useFetchClient();
await act(async () => {
await remove(FIXTURE_ASSETS);
});
expect(axiosInstance.post).toHaveBeenCalledWith(expect.any(String), {
expect(post).toHaveBeenCalledWith(expect.any(String), {
fileIds: FIXTURE_ASSETS.map(({ id }) => id),
});
});
@ -142,12 +136,13 @@ describe('useBulkRemove', () => {
result: { current },
} = await setup();
const { remove } = current;
const { post } = useFetchClient();
await act(async () => {
await remove(FIXTURE_FOLDERS);
});
expect(axiosInstance.post).toHaveBeenCalledWith(expect.any(String), {
expect(post).toHaveBeenCalledWith(expect.any(String), {
folderIds: FIXTURE_FOLDERS.map(({ id }) => id),
});
});
@ -157,12 +152,13 @@ describe('useBulkRemove', () => {
result: { current },
} = await setup();
const { remove } = current;
const { post } = useFetchClient();
await act(async () => {
await remove([...FIXTURE_FOLDERS, ...FIXTURE_ASSETS]);
});
expect(axiosInstance.post).toHaveBeenCalledWith(expect.any(String), {
expect(post).toHaveBeenCalledWith(expect.any(String), {
fileIds: FIXTURE_ASSETS.map(({ id }) => id),
folderIds: FIXTURE_FOLDERS.map(({ id }) => id),
});

View File

@ -1,13 +1,14 @@
import { useMutation, useQueryClient } from 'react-query';
import { useNotification } from '@strapi/helper-plugin';
import { useNotification, useFetchClient } from '@strapi/helper-plugin';
import pluginId from '../pluginId';
import { axiosInstance, getRequestUrl, getTrad } from '../utils';
import { getRequestUrl, getTrad } from '../utils';
export const useBulkRemove = () => {
const toggleNotification = useNotification();
const queryClient = useQueryClient();
const url = getRequestUrl('actions/bulk-delete');
const { post } = useFetchClient();
const bulkRemoveQuery = (filesAndFolders) => {
const payload = filesAndFolders.reduce((acc, selected) => {
@ -23,7 +24,9 @@ export const useBulkRemove = () => {
return acc;
}, {});
return axiosInstance.post(url, payload);
console.log('useBulkRemove new', url);
return post(url, payload);
};
const mutation = useMutation(bulkRemoveQuery, {