|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { |
| 3 | + initializeMocks, |
| 4 | + render, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | + within, |
| 8 | +} from '../../testUtils'; |
| 9 | +import { |
| 10 | + mockContentLibrary, |
| 11 | + mockXBlockFields, |
| 12 | + mockGetContainerMetadata, |
| 13 | + mockGetContainerChildren, |
| 14 | + mockLibraryBlockMetadata, |
| 15 | +} from '../data/api.mocks'; |
| 16 | +import { mockContentSearchConfig, mockGetBlockTypes } from '../../search-manager/data/api.mock'; |
| 17 | +import { mockBroadcastChannel, mockClipboardEmpty } from '../../generic/data/api.mock'; |
| 18 | +import LibraryLayout from '../LibraryLayout'; |
| 19 | + |
| 20 | +const path = '/library/:libraryId/*'; |
| 21 | +const libraryTitle = mockContentLibrary.libraryData.title; |
| 22 | + |
| 23 | +mockClipboardEmpty.applyMock(); |
| 24 | +mockGetContainerMetadata.applyMock(); |
| 25 | +mockGetContainerChildren.applyMock(); |
| 26 | +mockContentSearchConfig.applyMock(); |
| 27 | +mockGetBlockTypes.applyMock(); |
| 28 | +mockContentLibrary.applyMock(); |
| 29 | +mockXBlockFields.applyMock(); |
| 30 | +mockLibraryBlockMetadata.applyMock(); |
| 31 | +mockBroadcastChannel(); |
| 32 | + |
| 33 | +describe('<LibraryUnitPage />', () => { |
| 34 | + beforeEach(() => { |
| 35 | + initializeMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + const renderLibraryUnitPage = (unitId?: string, libraryId?: string) => { |
| 39 | + const libId = libraryId || mockContentLibrary.libraryId; |
| 40 | + const uId = unitId || mockGetContainerMetadata.containerId; |
| 41 | + render(<LibraryLayout />, { |
| 42 | + path, |
| 43 | + routerProps: { |
| 44 | + initialEntries: [`/library/${libId}/unit/${uId}`], |
| 45 | + }, |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + it('shows the spinner before the query is complete', async () => { |
| 50 | + // This mock will never return data about the collection (it loads forever): |
| 51 | + renderLibraryUnitPage(mockGetContainerMetadata.containerIdLoading); |
| 52 | + const spinner = screen.getByRole('status'); |
| 53 | + expect(spinner.textContent).toEqual('Loading...'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('shows an error component if no unit returned', async () => { |
| 57 | + // This mock will simulate incorrect unit id |
| 58 | + renderLibraryUnitPage(mockGetContainerMetadata.containerIdError); |
| 59 | + const errorMessage = 'Not found'; |
| 60 | + expect(await screen.findByRole('alert')).toHaveTextContent(errorMessage); |
| 61 | + }); |
| 62 | + |
| 63 | + it('shows unit data', async () => { |
| 64 | + renderLibraryUnitPage(); |
| 65 | + expect((await screen.findAllByText(libraryTitle))[0]).toBeInTheDocument(); |
| 66 | + // Unit title |
| 67 | + expect((await screen.findAllByText(mockGetContainerMetadata.containerData.displayName))[0]).toBeInTheDocument(); |
| 68 | + // unit info button |
| 69 | + expect(await screen.findByRole('button', { name: 'Unit Info' })).toBeInTheDocument(); |
| 70 | + expect((await screen.findAllByRole('button', { name: 'Drag to reorder' })).length).toEqual(3); |
| 71 | + // check all children components are rendered. |
| 72 | + expect(await screen.findByText('text block 0')).toBeInTheDocument(); |
| 73 | + expect(await screen.findByText('text block 1')).toBeInTheDocument(); |
| 74 | + expect(await screen.findByText('text block 2')).toBeInTheDocument(); |
| 75 | + // 3 preview iframes |
| 76 | + expect((await screen.findAllByTestId('block-preview')).length).toEqual(3); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should open and close the unit sidebar', async () => { |
| 80 | + renderLibraryUnitPage(); |
| 81 | + |
| 82 | + // sidebar should be visible by default |
| 83 | + const sidebar = await screen.findByTestId('library-sidebar'); |
| 84 | + |
| 85 | + const { findByText } = within(sidebar); |
| 86 | + |
| 87 | + // The mock data for the sidebar has a title of "Test Unit" |
| 88 | + expect(await findByText('Test Unit')).toBeInTheDocument(); |
| 89 | + |
| 90 | + // should close if open |
| 91 | + userEvent.click(await screen.findByText('Unit Info')); |
| 92 | + await waitFor(() => expect(screen.queryByTestId('library-sidebar')).not.toBeInTheDocument()); |
| 93 | + |
| 94 | + // Open again |
| 95 | + userEvent.click(await screen.findByText('Unit Info')); |
| 96 | + expect(await screen.findByTestId('library-sidebar')).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should open and component sidebar on component selection', async () => { |
| 100 | + renderLibraryUnitPage(); |
| 101 | + |
| 102 | + const component = await screen.findByText('text block 0'); |
| 103 | + userEvent.click(component); |
| 104 | + const sidebar = await screen.findByTestId('library-sidebar'); |
| 105 | + |
| 106 | + const { findByRole, findByText } = within(sidebar); |
| 107 | + |
| 108 | + // The mock data for the sidebar has a title of "text block 0" |
| 109 | + expect(await findByText('text block 0')).toBeInTheDocument(); |
| 110 | + |
| 111 | + const closeButton = await findByRole('button', { name: /close/i }); |
| 112 | + userEvent.click(closeButton); |
| 113 | + await waitFor(() => expect(screen.queryByTestId('library-sidebar')).not.toBeInTheDocument()); |
| 114 | + }); |
| 115 | +}); |
0 commit comments