forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryBackupPage.test.tsx
More file actions
182 lines (165 loc) · 6.54 KB
/
LibraryBackupPage.test.tsx
File metadata and controls
182 lines (165 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {
fireEvent,
initializeMocks,
render,
screen,
} from '@src/testUtils';
import { act } from '@testing-library/react';
import { LibraryBackupStatus } from './data/constants';
import { LibraryBackupPage } from './LibraryBackupPage';
import messages from './messages';
// Mock the hooks/context used by the page so we can render it in isolation.
jest.mock('@src/library-authoring/common/context/LibraryContext', () => ({
useLibraryContext: () => ({ libraryId: 'lib:TestOrg:test-lib' }),
}));
jest.mock('@edx/frontend-platform/i18n', () => ({
...jest.requireActual('@edx/frontend-platform/i18n'),
useIntl: () => ({
formatMessage: (message) => message.defaultMessage,
}),
}));
const mockLibraryData: { data: any } = { data: {} };
jest.mock('@src/library-authoring/data/apiHooks', () => ({
useContentLibrary: () => (mockLibraryData),
}));
// Mutable mocks varied per test
const mockMutate = jest.fn();
let mockStatusData: any = {};
let mockMutationError: any = null; // allows testing mutation error branch
jest.mock('@src/library-authoring/backup-restore/data/hooks', () => ({
useCreateLibraryBackup: () => ({
mutate: mockMutate,
error: mockMutationError,
}),
useGetLibraryBackupStatus: () => ({
data: mockStatusData,
}),
}));
describe('<LibraryBackupPage />', () => {
beforeEach(() => {
initializeMocks();
mockMutate.mockReset();
mockStatusData = {};
mockLibraryData.data = {
title: 'My Test Library',
slug: 'test-lib',
org: 'TestOrg',
};
mockMutationError = null;
});
it('returns NotFoundAlert if no libraryData', () => {
mockLibraryData.data = undefined;
render(<LibraryBackupPage />);
expect(screen.getByText(/Not Found/i)).toBeVisible();
});
it('renders the backup page title and initial download button', () => {
mockStatusData = {};
render(<LibraryBackupPage />);
expect(screen.getByText(messages.backupPageTitle.defaultMessage)).toBeVisible();
const button = screen.getByRole('button', { name: messages.downloadAriaLabel.defaultMessage });
expect(button).toBeEnabled();
});
it('shows pending state disables button after starting backup', async () => {
mockMutate.mockImplementation((_arg: any, { onSuccess }: any) => {
onSuccess({ task_id: 'task-123' });
mockStatusData = { state: LibraryBackupStatus.Pending };
});
render(<LibraryBackupPage />);
const initialButton = screen.getByRole('button', { name: messages.downloadAriaLabel.defaultMessage });
expect(initialButton).toBeEnabled();
fireEvent.click(initialButton);
const pendingText = await screen.findByText(messages.backupPending.defaultMessage);
const pendingButton = pendingText.closest('button');
expect(pendingButton).toBeDisabled();
});
it('shows exporting state disables button and changes text', async () => {
mockMutate.mockImplementation((_arg: any, { onSuccess }: any) => {
onSuccess({ task_id: 'task-123' });
mockStatusData = { state: LibraryBackupStatus.Exporting };
});
render(<LibraryBackupPage />);
const initialButton = screen.getByRole('button', { name: messages.downloadAriaLabel.defaultMessage });
fireEvent.click(initialButton);
const exportingText = await screen.findByText(messages.backupExporting.defaultMessage);
const exportingButton = exportingText.closest('button');
expect(exportingButton).toBeDisabled();
});
it('shows succeeded state uses ready text and triggers download', () => {
mockStatusData = { state: 'Succeeded', url: '/fake/path.tar.gz' };
const downloadSpy = jest.spyOn(document, 'createElement');
render(<LibraryBackupPage />);
const button = screen.getByRole('button');
expect(button).toHaveTextContent(/Download Library Backup/);
fireEvent.click(button);
expect(downloadSpy).toHaveBeenCalledWith('a');
downloadSpy.mockRestore();
});
it('shows failed state and error alert', () => {
mockStatusData = { state: LibraryBackupStatus.Failed };
render(<LibraryBackupPage />);
expect(screen.getByText(messages.backupFailedError.defaultMessage)).toBeVisible();
const button = screen.getByRole('button');
expect(button).toBeEnabled();
});
it('covers timeout cleanup on unmount', () => {
mockMutate.mockImplementation((_arg: any, { onSuccess }: any) => {
onSuccess({ task_id: 'task-123' });
mockStatusData = { state: LibraryBackupStatus.Pending };
});
const { unmount } = render(<LibraryBackupPage />);
const button = screen.getByRole('button');
fireEvent.click(button);
unmount();
// No assertion needed, just coverage for cleanup
});
it('covers fallback download logic', () => {
mockStatusData = { state: LibraryBackupStatus.Succeeded, url: '/fake/path.tar.gz' };
// Spy on createElement to force click failure for anchor
const originalCreate = document.createElement.bind(document);
const createSpy = jest.spyOn(document, 'createElement').mockImplementation((tagName: string) => {
const el = originalCreate(tagName);
if (tagName === 'a') {
// Force failure when click is invoked
(el as any).click = () => { throw new Error('fail'); };
}
return el;
});
// Stub window.location.href writable
const originalLocation = window.location;
// Use a minimal fake location object
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window.location;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.location = { href: '' };
render(<LibraryBackupPage />);
const button = screen.getByRole('button');
fireEvent.click(button);
expect(window.location.href).toContain('/fake/path.tar.gz');
// restore
createSpy.mockRestore();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.location = originalLocation;
});
it('executes timeout callback clearing task and re-enabling button after 5 minutes', () => {
jest.useFakeTimers();
mockMutate.mockImplementation((_arg: any, { onSuccess }: any) => {
onSuccess({ task_id: 'task-123' });
mockStatusData = { state: LibraryBackupStatus.Pending };
});
render(<LibraryBackupPage />);
const button = screen.getByRole('button');
expect(button).toBeEnabled();
fireEvent.click(button);
// Now in progress
expect(button).toBeDisabled();
act(() => {
jest.advanceTimersByTime(5 * 60 * 1000); // advance 5 minutes
});
// After timeout callback, should be enabled again
expect(button).toBeEnabled();
jest.useRealTimers();
});
});