|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | + |
| 3 | +import { |
| 4 | + initializeMocks, |
| 5 | + render as testRender, |
| 6 | + screen, |
| 7 | + waitFor, |
| 8 | +} from '@src/testUtils'; |
| 9 | + |
| 10 | +import { LibraryProvider } from '../common/context/LibraryContext'; |
| 11 | +import { |
| 12 | + mockContentLibrary, |
| 13 | + mockGetCourseImports, |
| 14 | +} from '../data/api.mocks'; |
| 15 | +import { type CourseImport } from '../data/api'; |
| 16 | +import { ImportedCourseCard } from './ImportedCourseCard'; |
| 17 | + |
| 18 | +initializeMocks(); |
| 19 | +mockContentLibrary.applyMock(); |
| 20 | +const { libraryId } = mockContentLibrary; |
| 21 | + |
| 22 | +const mockNavigate = jest.fn(); |
| 23 | +jest.mock('react-router-dom', () => ({ |
| 24 | + ...jest.requireActual('react-router-dom'), |
| 25 | + useNavigate: () => mockNavigate, |
| 26 | +})); |
| 27 | + |
| 28 | +const render = (courseImport: CourseImport) => ( |
| 29 | + testRender( |
| 30 | + <ImportedCourseCard courseImport={courseImport} />, |
| 31 | + { |
| 32 | + extraWrapper: ({ children }: { children: React.ReactNode }) => ( |
| 33 | + <LibraryProvider libraryId={mockContentLibrary.libraryId}> |
| 34 | + {children} |
| 35 | + </LibraryProvider> |
| 36 | + ), |
| 37 | + path: '/libraries/:libraryId/import-course', |
| 38 | + params: { libraryId }, |
| 39 | + }, |
| 40 | + ) |
| 41 | +); |
| 42 | + |
| 43 | +describe('<ImportedCourseCard>', () => { |
| 44 | + it('should render a card for a successful import', () => { |
| 45 | + const { succeedImport } = mockGetCourseImports; |
| 46 | + render(succeedImport); |
| 47 | + expect(screen.getByText(succeedImport.source.displayName)).toBeInTheDocument(); |
| 48 | + expect(screen.getByText(/100% Imported/)).toBeInTheDocument(); |
| 49 | + |
| 50 | + const courseLink = screen.getByRole('link', { name: succeedImport.source.displayName }); |
| 51 | + expect(courseLink).toHaveAttribute('href', `/course/${succeedImport.source.key}`); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should render a card for a successful import with a collection', async () => { |
| 55 | + const { succeedImportWithCollection } = mockGetCourseImports; |
| 56 | + render(succeedImportWithCollection); |
| 57 | + expect(screen.getByText(succeedImportWithCollection.source.displayName)).toBeInTheDocument(); |
| 58 | + expect(screen.getByText(/100% Imported/)).toBeInTheDocument(); |
| 59 | + |
| 60 | + const courseLink = screen.getByRole('link', { name: succeedImportWithCollection.source.displayName }); |
| 61 | + expect(courseLink).toHaveAttribute('href', `/course/${succeedImportWithCollection.source.key}`); |
| 62 | + |
| 63 | + const collectionLink = await screen.findByText(succeedImportWithCollection.targetCollection.title); |
| 64 | + userEvent.click(collectionLink); |
| 65 | + await waitFor(() => { |
| 66 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 67 | + { |
| 68 | + pathname: `/library/${libraryId}/collection/${succeedImportWithCollection.targetCollection.key}`, |
| 69 | + search: '', |
| 70 | + }, |
| 71 | + ); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should render a card for a failed import', () => { |
| 76 | + const { failImport } = mockGetCourseImports; |
| 77 | + render(failImport); |
| 78 | + expect(screen.getByText(failImport.source.displayName)).toBeInTheDocument(); |
| 79 | + expect(screen.getByText('Import Failed')).toBeInTheDocument(); |
| 80 | + |
| 81 | + const courseLink = screen.getByRole('link', { name: failImport.source.displayName }); |
| 82 | + expect(courseLink).toHaveAttribute('href', `/course/${failImport.source.key}`); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should render a card for an in-progress import', () => { |
| 86 | + const { inProgressImport } = mockGetCourseImports; |
| 87 | + render(inProgressImport); |
| 88 | + expect(screen.getByText(inProgressImport.source.displayName)).toBeInTheDocument(); |
| 89 | + expect(screen.getByText(/50% Imported/)).toBeInTheDocument(); |
| 90 | + |
| 91 | + const courseLink = screen.getByRole('link', { name: inProgressImport.source.displayName }); |
| 92 | + expect(courseLink).toHaveAttribute('href', `/course/${inProgressImport.source.key}`); |
| 93 | + }); |
| 94 | +}); |
0 commit comments