|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { |
| 3 | + initializeMocks, |
| 4 | + render as testRender, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | +} from '@src/testUtils'; |
| 8 | +import { mockGetMigrationStatus } from '@src/data/api.mocks'; |
| 9 | +import { bulkModulestoreMigrateUrl } from '@src/data/api'; |
| 10 | +import { ImportDetailsPage } from './ImportDetailsPage'; |
| 11 | +import { LibraryProvider } from '../common/context/LibraryContext'; |
| 12 | +import { mockContentLibrary } from '../data/api.mocks'; |
| 13 | + |
| 14 | +mockContentLibrary.applyMock(); |
| 15 | +mockGetMigrationStatus.applyMock(); |
| 16 | +const { libraryId } = mockContentLibrary; |
| 17 | +const mockNavigate = jest.fn(); |
| 18 | +let axiosMock; |
| 19 | + |
| 20 | +// Mock the useCourseDetails hook |
| 21 | +jest.mock('@src/course-outline/data/apiHooks', () => ({ |
| 22 | + useCourseDetails: jest.fn().mockReturnValue({ isPending: false, data: { title: 'Test Course' } }), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('react-router-dom', () => ({ |
| 26 | + ...jest.requireActual('react-router-dom'), |
| 27 | + useNavigate: () => mockNavigate, |
| 28 | +})); |
| 29 | + |
| 30 | +const render = (migrationTaskId: string) => ( |
| 31 | + testRender( |
| 32 | + <ImportDetailsPage />, |
| 33 | + { |
| 34 | + extraWrapper: ({ children }: { children: React.ReactNode }) => ( |
| 35 | + <LibraryProvider libraryId={libraryId}> |
| 36 | + {children} |
| 37 | + </LibraryProvider> |
| 38 | + ), |
| 39 | + path: '/libraries/:libraryId/import/:courseId/:migrationTaskId', |
| 40 | + params: { |
| 41 | + libraryId, |
| 42 | + migrationTaskId, |
| 43 | + courseId: '1', |
| 44 | + }, |
| 45 | + }, |
| 46 | + ) |
| 47 | +); |
| 48 | + |
| 49 | +describe('', () => { |
| 50 | + beforeEach(() => { |
| 51 | + const newMocks = initializeMocks(); |
| 52 | + axiosMock = newMocks.axiosMock; |
| 53 | + }); |
| 54 | + |
| 55 | + it('should render loading state', () => { |
| 56 | + render(mockGetMigrationStatus.migrationIdLoading); |
| 57 | + expect(screen.getByText(/loading\.\.\./i)).toBeInTheDocument(); |
| 58 | + expect(screen.getByText(/help & support/i)).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should render In Progress state', async () => { |
| 62 | + render(mockGetMigrationStatus.migrationIdInProgress); |
| 63 | + expect(await screen.findByText(/test course is being imported/i)); |
| 64 | + expect(screen.getByRole('button', { |
| 65 | + name: /view imported content/i, |
| 66 | + })).toBeDisabled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should render Failed state', async () => { |
| 70 | + const user = userEvent.setup(); |
| 71 | + const url = bulkModulestoreMigrateUrl(); |
| 72 | + axiosMock.onPost(url).reply(200); |
| 73 | + render(mockGetMigrationStatus.migrationIdFailed); |
| 74 | + expect(await screen.findByText(/test course was not imported into your Library/i)); |
| 75 | + expect(screen.getByText(/import failed for the following reasons:/i)); |
| 76 | + const retryImport = screen.getByRole('button', { |
| 77 | + name: /re-try import/i, |
| 78 | + }); |
| 79 | + |
| 80 | + await user.click(retryImport); |
| 81 | + await waitFor(() => expect(axiosMock.history.post.length).toBe(1)); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should render Succeeded state', async () => { |
| 85 | + render(mockGetMigrationStatus.migrationId); |
| 86 | + expect(await screen.findByText( |
| 87 | + /test course has been imported to your library in a collection called test collection/i, |
| 88 | + )); |
| 89 | + |
| 90 | + const viewImportedContentBtn = screen.getByRole('button', { |
| 91 | + name: /view imported content/i, |
| 92 | + }); |
| 93 | + |
| 94 | + await viewImportedContentBtn.click(); |
| 95 | + await waitFor(() => expect(mockNavigate).toHaveBeenCalledWith('/library/lib:Axim:TEST/collection/coll')); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should render Partial Succeeded state', async () => { |
| 99 | + render(mockGetMigrationStatus.migrationIdPartial); |
| 100 | + expect(await screen.findByText(/partial import successful/i)).toBeInTheDocument(); |
| 101 | + |
| 102 | + expect(screen.getByText( |
| 103 | + /85% of course test course has been imported successfully/i, |
| 104 | + )).toBeInTheDocument(); |
| 105 | + |
| 106 | + expect(screen.getByRole('cell', { |
| 107 | + name: /legacy library content/i, |
| 108 | + })).toBeInTheDocument(); |
| 109 | + expect(screen.getByRole('cell', { |
| 110 | + name: /library_content/i, |
| 111 | + })).toBeInTheDocument(); |
| 112 | + expect(screen.getByRole('cell', { |
| 113 | + name: /the block has children, so it is not supported in content libraries/i, |
| 114 | + })); |
| 115 | + |
| 116 | + const viewImportedContentBtn = screen.getByRole('button', { |
| 117 | + name: /view imported content/i, |
| 118 | + }); |
| 119 | + |
| 120 | + await viewImportedContentBtn.click(); |
| 121 | + await waitFor(() => expect(mockNavigate).toHaveBeenCalledWith('/library/lib:Axim:TEST/collection/coll')); |
| 122 | + }); |
| 123 | +}); |
0 commit comments