-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtestUtils.ts
More file actions
68 lines (58 loc) · 2.08 KB
/
Copy pathtestUtils.ts
File metadata and controls
68 lines (58 loc) · 2.08 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
loadingSpinnerIsNotInDocument,
screen,
userEvent,
waitFor,
within,
} from '../../../utils/testUtils';
import { Registration } from '../../registration/types';
export const getSignupsPageElement = (
key: 'menu' | 'searchInput' | 'toggle'
) => {
switch (key) {
case 'menu':
return screen.getByRole('region', { name: /valinnat/i });
case 'searchInput':
return screen.getByRole('textbox', { name: 'Hae osallistujia' });
case 'toggle':
return screen.getByRole('button', { name: /valinnat/i });
}
};
export const openSignupsPageMenu = async () => {
const user = userEvent.setup();
const toggleButton = getSignupsPageElement('toggle');
await user.click(toggleButton);
const menu = getSignupsPageElement('menu');
return { menu, toggleButton };
};
export const shouldExportSignupsAsExcel = async (
registration: Registration
) => {
global.URL.createObjectURL = vi.fn(() => 'https://test.com');
global.URL.revokeObjectURL = vi.fn();
const user = userEvent.setup();
await loadingSpinnerIsNotInDocument(10000);
const link: any = { click: vi.fn(), remove: vi.fn() };
const { menu } = await openSignupsPageMenu();
const exportAsExcelButton = await within(menu).getByRole('button', {
name: 'Lataa osallistujalista (Excel)',
});
// Mock document.createElement which is needed by downloadBlob. RenderComponent needs
// createElement so do this after rendering components to avoid errors
const createElement = document.createElement;
document.createElement = vi
.fn()
.mockImplementation((tagName: string, ...args: any[]) => {
if (tagName === 'a') return link;
return createElement.call(document, tagName, ...args);
});
await user.click(exportAsExcelButton);
await waitFor(() =>
expect(link.download).toBe(`registered_persons_${registration.id}`)
);
expect(link.href).toBe('https://test.com');
expect(link.click).toHaveBeenCalledTimes(1);
// Restore original createElement to avoid unexpected side effects
document.createElement = createElement;
};