|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import type MockAdapter from 'axios-mock-adapter'; |
| 3 | +import { QueryClient } from '@tanstack/react-query'; |
| 4 | + |
| 5 | +import { act } from 'react'; |
| 6 | +import { |
| 7 | + initializeMocks, |
| 8 | + fireEvent, |
| 9 | + render, |
| 10 | + screen, |
| 11 | + waitFor, |
| 12 | +} from '../../testUtils'; |
| 13 | +import { |
| 14 | + getLibraryContainerApiUrl, |
| 15 | + getLibraryContainerChildrenApiUrl, |
| 16 | + getLibraryContainersApiUrl, |
| 17 | +} from '../data/api'; |
| 18 | +import { |
| 19 | + mockContentLibrary, |
| 20 | + mockXBlockFields, |
| 21 | + mockGetContainerMetadata, |
| 22 | + mockGetContainerChildren, |
| 23 | + mockLibraryBlockMetadata, |
| 24 | +} from '../data/api.mocks'; |
| 25 | +import { mockContentSearchConfig, mockGetBlockTypes, mockSearchResult } from '../../search-manager/data/api.mock'; |
| 26 | +import { mockClipboardEmpty } from '../../generic/data/api.mock'; |
| 27 | +import LibraryLayout from '../LibraryLayout'; |
| 28 | +import { ToastActionData, ToastProvider } from '../../generic/toast-context'; |
| 29 | +import mockResult from '../__mocks__/subsection-single.json'; |
| 30 | +import { ContainerType } from '../../generic/key-utils'; |
| 31 | +import ContainerRemover from './ContainerRemover'; |
| 32 | +import { LibraryProvider } from '../common/context/LibraryContext'; |
| 33 | + |
| 34 | +const path = '/library/:libraryId/*'; |
| 35 | +const libraryTitle = mockContentLibrary.libraryData.title; |
| 36 | + |
| 37 | +let axiosMock: MockAdapter; |
| 38 | +let queryClient: QueryClient; |
| 39 | +let mockShowToast: (message: string, action?: ToastActionData | undefined) => void; |
| 40 | + |
| 41 | +mockClipboardEmpty.applyMock(); |
| 42 | +mockGetContainerMetadata.applyMock(); |
| 43 | +mockGetContainerChildren.applyMock(); |
| 44 | +mockContentSearchConfig.applyMock(); |
| 45 | +mockGetBlockTypes.applyMock(); |
| 46 | +mockContentLibrary.applyMock(); |
| 47 | +mockXBlockFields.applyMock(); |
| 48 | +mockLibraryBlockMetadata.applyMock(); |
| 49 | + |
| 50 | +const mockClose = jest.fn(); |
| 51 | + |
| 52 | +const { libraryId } = mockContentLibrary; |
| 53 | +const renderModal = (element: React.ReactNode) => { |
| 54 | + render( |
| 55 | + <ToastProvider> |
| 56 | + <LibraryProvider libraryId={libraryId}> |
| 57 | + {element} |
| 58 | + </LibraryProvider> |
| 59 | + </ToastProvider>, |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +jest.mock('react-router-dom', () => ({ |
| 64 | + ...jest.requireActual('react-router-dom'), |
| 65 | + useParams: jest.fn().mockImplementation(() => ({ |
| 66 | + containerId: mockGetContainerChildren.unitIdWithDuplicate, |
| 67 | + })), |
| 68 | +})); |
| 69 | + |
| 70 | +describe('<ContainerRemover />', () => { |
| 71 | + beforeEach(() => { |
| 72 | + ({ axiosMock, mockShowToast, queryClient } = initializeMocks()); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(() => { |
| 76 | + jest.clearAllMocks(); |
| 77 | + axiosMock.restore(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('triggers update container children api call when duplicates are present', async () => { |
| 81 | + const user = userEvent.setup(); |
| 82 | + const url = getLibraryContainerChildrenApiUrl(mockGetContainerChildren.unitIdWithDuplicate); |
| 83 | + axiosMock.onPatch(url).reply(200); |
| 84 | + const result = await mockGetContainerChildren(mockGetContainerChildren.unitIdWithDuplicate); |
| 85 | + const resultIds = result.map((obj) => obj.id); |
| 86 | + renderModal(<ContainerRemover |
| 87 | + close={mockClose} |
| 88 | + containerKey={result[0].id} |
| 89 | + displayName="Title" |
| 90 | + index={0} |
| 91 | + />) |
| 92 | + const btn = await screen.findByRole('button', { name: 'Remove' }); |
| 93 | + await user.click(btn); |
| 94 | + |
| 95 | + await waitFor(() => { |
| 96 | + expect(axiosMock.history.patch[0].url).toEqual(url); |
| 97 | + // Only the first element is removed even though the last element has the same id. |
| 98 | + expect(JSON.parse(axiosMock.history.patch[0].data).usage_keys).toEqual(resultIds.slice(1)); |
| 99 | + }); |
| 100 | + expect(mockClose).toHaveBeenCalled(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments