|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { |
| 3 | + render, screen, waitFor, initializeMocks, |
| 4 | +} from '../../testUtils'; |
| 5 | +import { LibraryProvider } from '../common/context/LibraryContext'; |
| 6 | +import CreateContainerModal from './CreateContainerModal'; |
| 7 | +import AddContent from '../add-content/AddContent'; |
| 8 | +import { mockContentLibrary, mockBlockTypesMetadata } from '../data/api.mocks'; |
| 9 | +import { getLibraryContainerApiUrl, getLibraryContainersApiUrl } from '../data/api'; |
| 10 | + |
| 11 | +const { libraryId } = mockContentLibrary; |
| 12 | + |
| 13 | +const newSectionId = 'lct:org:lib:section:new-section'; |
| 14 | + |
| 15 | +describe('CreateContainerModal container linking', () => { |
| 16 | + let axiosMock; |
| 17 | + let mockShowToast; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + const mocks = initializeMocks(); |
| 21 | + axiosMock = mocks.axiosMock; |
| 22 | + mockShowToast = mocks.mockShowToast; |
| 23 | + jest.clearAllMocks(); |
| 24 | + mockContentLibrary.applyMock(); |
| 25 | + mockBlockTypesMetadata.applyMock(); |
| 26 | + axiosMock.onPost(getLibraryContainersApiUrl(libraryId)).reply(200, { |
| 27 | + id: newSectionId, |
| 28 | + containerType: 'section', |
| 29 | + displayName: 'Test Container', |
| 30 | + publishedDisplayName: 'Test Container', |
| 31 | + created: '2024-09-19T10:00:00Z', |
| 32 | + createdBy: 'test_author', |
| 33 | + lastPublished: null, |
| 34 | + publishedBy: null, |
| 35 | + lastDraftCreated: null, |
| 36 | + lastDraftCreatedBy: null, |
| 37 | + modified: '2024-09-20T11:00:00Z', |
| 38 | + hasUnpublishedChanges: true, |
| 39 | + collections: [], |
| 40 | + tagsCount: 0, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + function renderWithProvider(content, options) { |
| 45 | + const { containerId, routeType = 'library' } = options || {}; |
| 46 | + const paths = { |
| 47 | + library: '/library/:libraryId', |
| 48 | + section: '/library/:libraryId/section/:containerId', |
| 49 | + subsection: '/library/:libraryId/subsection/:containerId', |
| 50 | + collection: '/library/:libraryId/collection/:collectionId', |
| 51 | + }; |
| 52 | + |
| 53 | + const params = { libraryId, ...containerId && { containerId } }; |
| 54 | + |
| 55 | + return render(content, { |
| 56 | + path: paths[routeType], |
| 57 | + params, |
| 58 | + extraWrapper: ({ children: wrappedChildren }) => ( |
| 59 | + <LibraryProvider libraryId={libraryId}> |
| 60 | + {wrappedChildren} |
| 61 | + </LibraryProvider> |
| 62 | + ), |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + it('links container to collection when inside a collection', async () => { |
| 67 | + renderWithProvider( |
| 68 | + <> |
| 69 | + <AddContent /> |
| 70 | + <CreateContainerModal /> |
| 71 | + </>, |
| 72 | + {}, |
| 73 | + ); |
| 74 | + // Disambiguate: select the "Section" button by exact match |
| 75 | + const sectionButton = await screen.findByRole('button', { name: /^Section$/ }); |
| 76 | + userEvent.click(sectionButton); |
| 77 | + const nameInput = await screen.findByLabelText(/name your section/i); |
| 78 | + userEvent.type(nameInput, 'Test Section'); |
| 79 | + const createButton = await screen.findByRole('button', { name: /create/i }); |
| 80 | + userEvent.click(createButton); |
| 81 | + await waitFor(() => { |
| 82 | + expect(axiosMock.history.post).toHaveLength(1); |
| 83 | + }); |
| 84 | + expect(axiosMock.history.post[0].url).toMatch(/\/api\/libraries\/.*\/containers/); |
| 85 | + expect(JSON.parse(axiosMock.history.post[0].data)).toEqual({ |
| 86 | + can_stand_alone: true, |
| 87 | + container_type: 'section', |
| 88 | + display_name: 'Test Section', |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('links container to section when inside a section', async () => { |
| 93 | + axiosMock.onPost(getLibraryContainerApiUrl(newSectionId)).reply(200, { |
| 94 | + id: newSectionId, |
| 95 | + containerType: 'section', |
| 96 | + displayName: 'Test Container', |
| 97 | + publishedDisplayName: 'Test Container', |
| 98 | + created: '2024-09-19T10:00:00Z', |
| 99 | + }); |
| 100 | + renderWithProvider( |
| 101 | + <> |
| 102 | + <AddContent /> |
| 103 | + <CreateContainerModal /> |
| 104 | + </>, |
| 105 | + { containerId: newSectionId, routeType: 'section' }, |
| 106 | + ); |
| 107 | + |
| 108 | + const subsectionButton = await screen.findByRole('button', { name: /^Subsection$/ }); |
| 109 | + userEvent.click(subsectionButton); |
| 110 | + const nameInput = await screen.findByLabelText(/name your subsection/i); |
| 111 | + userEvent.type(nameInput, 'Test Subsection'); |
| 112 | + const createButton = await screen.findByRole('button', { name: /create/i }); |
| 113 | + userEvent.click(createButton); |
| 114 | + await waitFor(() => { |
| 115 | + expect(axiosMock.history.post[0].url).toMatch(/\/api\/libraries\/.*\/containers/); |
| 116 | + }); |
| 117 | + expect(JSON.parse(axiosMock.history.post[0].data)).toEqual({ |
| 118 | + can_stand_alone: false, |
| 119 | + container_type: 'subsection', |
| 120 | + display_name: 'Test Subsection', |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('handles linking error gracefully', async () => { |
| 125 | + axiosMock.onPost(getLibraryContainersApiUrl(libraryId)).reply(500); |
| 126 | + renderWithProvider( |
| 127 | + <> |
| 128 | + <AddContent /> |
| 129 | + <CreateContainerModal /> |
| 130 | + </>, |
| 131 | + {}, |
| 132 | + ); |
| 133 | + // Disambiguate: select the "Section" button by exact match |
| 134 | + const sectionButton = await screen.findByRole('button', { name: /^Section$/ }); |
| 135 | + userEvent.click(sectionButton); |
| 136 | + const nameInput = await screen.findByLabelText(/name your section/i); |
| 137 | + userEvent.type(nameInput, 'Test Section'); |
| 138 | + const createButton = await screen.findByRole('button', { name: /create/i }); |
| 139 | + userEvent.click(createButton); |
| 140 | + await waitFor(() => { |
| 141 | + expect(mockShowToast).toHaveBeenCalledWith(expect.stringMatching(/error/i)); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments