From cde2ec56d89cfee99c587a17df2c6e5fb42fa08d Mon Sep 17 00:00:00 2001 From: Gustav Hansen Date: Tue, 5 Apr 2022 16:18:52 +0200 Subject: [PATCH] ML: Add tests for useRemoveAsset hook --- .../src/hooks/tests/useRemoveAsset.test.js | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 packages/core/upload/admin/src/hooks/tests/useRemoveAsset.test.js diff --git a/packages/core/upload/admin/src/hooks/tests/useRemoveAsset.test.js b/packages/core/upload/admin/src/hooks/tests/useRemoveAsset.test.js new file mode 100644 index 0000000000..ddc0e40511 --- /dev/null +++ b/packages/core/upload/admin/src/hooks/tests/useRemoveAsset.test.js @@ -0,0 +1,150 @@ +import React from 'react'; +import { IntlProvider } from 'react-intl'; +import { QueryClientProvider, QueryClient, useQueryClient } from 'react-query'; +import { renderHook, act } from '@testing-library/react-hooks'; + +import { NotificationsProvider, useNotification } from '@strapi/helper-plugin'; +import { deleteRequest } from '../../utils/deleteRequest'; + +import { useRemoveAsset } from '../useRemoveAsset'; + +const ASSET_FIXTURE = { + id: 1, +}; + +console.error = jest.fn(); + +jest.mock('../../utils/deleteRequest', () => ({ + ...jest.requireActual('../../utils/deleteRequest'), + deleteRequest: jest.fn().mockResolvedValue({ id: 1 }), +})); + +const notificationStatusMock = jest.fn(); + +jest.mock('@strapi/helper-plugin', () => ({ + ...jest.requireActual('@strapi/helper-plugin'), + useNotification: () => notificationStatusMock, +})); + +const refetchQueriesMock = jest.fn(); + +jest.mock('react-query', () => ({ + ...jest.requireActual('react-query'), + useQueryClient: () => ({ + refetchQueries: refetchQueriesMock, + }), +})); + +const client = new QueryClient({ + defaultOptions: { + queries: { + retry: false, + }, + }, +}); + +// eslint-disable-next-line react/prop-types +function ComponentFixture({ children }) { + return ( + + jest.fn()}> + + {children} + + + + ); +} + +function setup(...args) { + return new Promise(resolve => { + act(() => { + resolve(renderHook(() => useRemoveAsset(...args), { wrapper: ComponentFixture })); + }); + }); +} + +describe('useRemoveAsset', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + test('calls the proper endpoint', async () => { + const { + result: { current }, + waitFor, + } = await setup(); + const { removeAsset } = current; + + await act(async () => { + await removeAsset(ASSET_FIXTURE); + }); + + await waitFor(() => expect(deleteRequest).toBeCalledWith('files', ASSET_FIXTURE)); + }); + + test('calls toggleNotification in case of an success', async () => { + const toggleNotification = useNotification(); + const { + result: { current }, + waitFor, + } = await setup(); + const { removeAsset } = current; + + try { + await act(async () => { + await removeAsset(ASSET_FIXTURE); + }); + } catch (err) { + // ... + } + + await waitFor(() => + expect(toggleNotification).toHaveBeenCalledWith(expect.objectContaining({ type: 'success' })) + ); + }); + + test('does call refetchQueries in case of success', async () => { + const queryClient = useQueryClient(); + const { + result: { current }, + waitFor, + } = await setup(); + const { removeAsset } = current; + + await act(async () => { + await removeAsset(ASSET_FIXTURE); + }); + + await waitFor(() => + expect(queryClient.refetchQueries).toHaveBeenCalledWith(['upload', 'assets'], { + active: true, + }) + ); + }); + + test('calls toggleNotification in case of an error', async () => { + deleteRequest.mockRejectedValue({ message: 'error-msg' }); + + const toggleNotification = useNotification(); + const { + result: { current }, + waitFor, + } = await setup(); + const { removeAsset } = current; + + try { + await act(async () => { + await removeAsset(ASSET_FIXTURE); + }); + } catch (err) { + // ... + } + + await waitFor(() => + expect(toggleNotification).toHaveBeenCalledWith( + expect.objectContaining({ type: 'warning', message: 'error-msg' }) + ) + ); + }); +});