|
| 1 | +import { |
| 2 | + initializeMocks, |
| 3 | + render, |
| 4 | + screen, |
| 5 | + fireEvent, |
| 6 | +} from '@src/testUtils'; |
| 7 | +import { LibraryBackupPage } from './LibraryBackupPage'; |
| 8 | + |
| 9 | +// Mock the hooks/context used by the page so we can render it in isolation. |
| 10 | +jest.mock('@src/library-authoring/common/context/LibraryContext', () => ({ |
| 11 | + useLibraryContext: () => ({ libraryId: 'lib:TestOrg:test-lib' }), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('@src/library-authoring/data/apiHooks', () => ({ |
| 15 | + useContentLibrary: () => ({ |
| 16 | + data: { |
| 17 | + title: 'My Test Library', |
| 18 | + slug: 'test-lib', |
| 19 | + org: 'TestOrg', |
| 20 | + }, |
| 21 | + }), |
| 22 | +})); |
| 23 | + |
| 24 | +// Mutable mocks varied per test |
| 25 | +const mockMutate = jest.fn(); |
| 26 | +let mockStatusData: any = {}; |
| 27 | +jest.mock('@src/library-authoring/backup-restore/data/hooks', () => ({ |
| 28 | + useCreateLibraryBackup: () => ({ |
| 29 | + mutate: mockMutate, |
| 30 | + error: null, |
| 31 | + }), |
| 32 | + useGetLibraryBackupStatus: () => ({ |
| 33 | + data: mockStatusData, |
| 34 | + }), |
| 35 | +})); |
| 36 | + |
| 37 | +describe('<LibraryBackupPage />', () => { |
| 38 | + beforeEach(() => { |
| 39 | + initializeMocks(); |
| 40 | + mockMutate.mockReset(); |
| 41 | + mockStatusData = {}; // reset status for each test |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders the backup page title and initial download button', () => { |
| 45 | + mockStatusData = {}; // no state yet |
| 46 | + render(<LibraryBackupPage />); |
| 47 | + expect(screen.getByText('Library Backup')).toBeVisible(); |
| 48 | + const button = screen.getByRole('button', { name: /Download Library Backup/ }); |
| 49 | + expect(button).toBeEnabled(); |
| 50 | + // aria-label includes library title via template |
| 51 | + expect(button).toHaveAttribute('aria-label', expect.stringContaining('My Test Library')); |
| 52 | + }); |
| 53 | + |
| 54 | + it('shows pending state disables button after starting backup', async () => { |
| 55 | + mockMutate.mockImplementation((_arg: any, { onSuccess }: any) => { |
| 56 | + onSuccess({ task_id: 'task-123' }); |
| 57 | + mockStatusData = { state: 'Pending' }; |
| 58 | + }); |
| 59 | + render(<LibraryBackupPage />); |
| 60 | + // More specific matcher for the initial state (Download Library Backup) |
| 61 | + const initialButton = screen.getByRole('button', { name: /Download Library Backup/i }); |
| 62 | + expect(initialButton).toBeEnabled(); |
| 63 | + fireEvent.click(initialButton); |
| 64 | + const pendingButton = await screen.findByRole('button', { name: /Preparing to download/i }); |
| 65 | + expect(pendingButton).toBeDisabled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('shows succeeded state uses ready text', () => { |
| 69 | + mockStatusData = { state: 'Succeeded', url: '/fake/path.tar.gz' }; |
| 70 | + render(<LibraryBackupPage />); |
| 71 | + const button = screen.getByRole('button'); |
| 72 | + // When succeeded the text should be the download ready variant |
| 73 | + expect(button).toHaveTextContent(/Download Library Backup/); // substring still present |
| 74 | + }); |
| 75 | +}); |
0 commit comments