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

View File

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