mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 17:00:55 +00:00
replace axiosInstance in the useEditFolder hook to replace the api call with the useFetchClient post and put methods and update test
This commit is contained in:
parent
0227276d32
commit
ab8ffd0686
@ -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 { useEditFolder } from '../useEditFolder';
|
import { useEditFolder } from '../useEditFolder';
|
||||||
|
|
||||||
const FOLDER_CREATE_FIXTURE = {
|
const FOLDER_CREATE_FIXTURE = {
|
||||||
@ -20,19 +19,15 @@ const FOLDER_EDIT_FIXTURE = {
|
|||||||
parent: 1,
|
parent: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
jest.mock('../../utils', () => ({
|
|
||||||
...jest.requireActual('../../utils'),
|
|
||||||
axiosInstance: {
|
|
||||||
put: jest.fn().mockResolvedValue({ name: 'folder-edited' }),
|
|
||||||
post: jest.fn().mockResolvedValue({ name: 'folder-created' }),
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const notificationStatusMock = jest.fn();
|
const notificationStatusMock = jest.fn();
|
||||||
|
|
||||||
jest.mock('@strapi/helper-plugin', () => ({
|
jest.mock('@strapi/helper-plugin', () => ({
|
||||||
...jest.requireActual('@strapi/helper-plugin'),
|
...jest.requireActual('@strapi/helper-plugin'),
|
||||||
useNotification: () => notificationStatusMock,
|
useNotification: () => notificationStatusMock,
|
||||||
|
useFetchClient: jest.fn().mockReturnValue({
|
||||||
|
put: jest.fn().mockResolvedValue({ name: 'folder-edited' }),
|
||||||
|
post: jest.fn().mockResolvedValue({ name: 'folder-created' }),
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const refetchQueriesMock = jest.fn();
|
const refetchQueriesMock = jest.fn();
|
||||||
@ -83,6 +78,7 @@ describe('useEditFolder', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('calls the proper endpoint when creating a folder (post)', async () => {
|
test('calls the proper endpoint when creating a folder (post)', async () => {
|
||||||
|
const { post } = useFetchClient();
|
||||||
const {
|
const {
|
||||||
result: { current },
|
result: { current },
|
||||||
} = await setup();
|
} = await setup();
|
||||||
@ -92,10 +88,12 @@ describe('useEditFolder', () => {
|
|||||||
await editFolder(FOLDER_CREATE_FIXTURE);
|
await editFolder(FOLDER_CREATE_FIXTURE);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(axiosInstance.post).toHaveBeenCalledWith('/upload/folders/', expect.any(Object));
|
expect(post).toHaveBeenCalledWith('/upload/folders/', expect.any(Object));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('calls the proper endpoint when creating a folder (put)', async () => {
|
test('calls the proper endpoint when creating a folder (put)', async () => {
|
||||||
|
const { put } = useFetchClient();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
result: { current },
|
result: { current },
|
||||||
} = await setup();
|
} = await setup();
|
||||||
@ -111,10 +109,11 @@ describe('useEditFolder', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(axiosInstance.put).toHaveBeenCalledWith('/upload/folders/2', expect.any(Object));
|
expect(put).toHaveBeenCalledWith('/upload/folders/2', expect.any(Object));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('calls the proper endpoint when editing a folder', async () => {
|
test('calls the proper endpoint when editing a folder', async () => {
|
||||||
|
const { put } = useFetchClient();
|
||||||
const {
|
const {
|
||||||
result: { current },
|
result: { current },
|
||||||
} = await setup();
|
} = await setup();
|
||||||
@ -130,7 +129,7 @@ describe('useEditFolder', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(axiosInstance.put).toHaveBeenCalledWith('/upload/folders/2', expect.any(Object));
|
expect(put).toHaveBeenCalledWith('/upload/folders/2', expect.any(Object));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('does not call toggleNotification in case of success', async () => {
|
test('does not call toggleNotification in case of success', async () => {
|
||||||
|
@ -1,20 +1,22 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
|
import { useFetchClient } from '@strapi/helper-plugin';
|
||||||
|
|
||||||
import pluginId from '../pluginId';
|
import pluginId from '../pluginId';
|
||||||
import { axiosInstance, getRequestUrl } from '../utils';
|
import { getRequestUrl } from '../utils';
|
||||||
|
|
||||||
const editFolderRequest = ({ attrs, id }) => {
|
const editFolderRequest = (put, post, { attrs, id }) => {
|
||||||
const isEditing = !!id;
|
const isEditing = !!id;
|
||||||
const method = isEditing ? 'put' : 'post';
|
const method = isEditing ? put : post;
|
||||||
const url = getRequestUrl(`folders/${id ?? ''}`);
|
const url = getRequestUrl(`folders/${id ?? ''}`);
|
||||||
|
|
||||||
return axiosInstance[method](url, attrs).then((res) => res.data);
|
return method(url, attrs).then((res) => res.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useEditFolder = () => {
|
export const useEditFolder = () => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const { put, post } = useFetchClient();
|
||||||
|
|
||||||
const mutation = useMutation((...args) => editFolderRequest(...args), {
|
const mutation = useMutation((...args) => editFolderRequest(put, post, ...args), {
|
||||||
onSuccess() {
|
onSuccess() {
|
||||||
queryClient.refetchQueries([pluginId, 'folders'], { active: true });
|
queryClient.refetchQueries([pluginId, 'folders'], { active: true });
|
||||||
queryClient.refetchQueries([pluginId, 'folder', 'structure'], { active: true });
|
queryClient.refetchQueries([pluginId, 'folder', 'structure'], { active: true });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user