-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathPlatformAbout.test.tsx
More file actions
128 lines (115 loc) · 4.41 KB
/
PlatformAbout.test.tsx
File metadata and controls
128 lines (115 loc) · 4.41 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
import {
PageDialogProvider,
PageSettingsContext,
usePageDialog,
type IPageSettings,
} from '@ansible/ansible-ui-framework';
import { awxAPI } from '@ansible/awx-ui/common/api/awx-utils';
import { render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { edaAPI } from '@ansible/eda-ui/common/eda-utils';
import { hubAPI } from '@ansible/hub-ui/common/api/formatPath';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import type { ComponentProps } from 'react';
import { useEffect } from 'react';
import { SWRConfig } from 'swr';
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
import platformLogo from '../assets/platform-logo.svg?url';
import platformLogoWhite from '../assets/platform-logo-white.svg?url';
import { PlatformAbout } from './PlatformAbout';
vi.mock('@patternfly/react-core', async (importOriginal) => {
const actual = await importOriginal<typeof import('@patternfly/react-core')>();
const PfAboutModal = actual.AboutModal;
return {
...actual,
AboutModal: (props: ComponentProps<typeof PfAboutModal>) => (
<PfAboutModal {...props} disableFocusTrap />
),
};
});
function OpenPlatformAboutDialog() {
const [, setDialog] = usePageDialog();
useEffect(() => {
setDialog(<PlatformAbout platformVersion="2.5" />);
}, [setDialog]);
return null;
}
function mountAbout(settings: IPageSettings) {
return render(
<SWRConfig value={{ provider: () => new Map(), dedupingInterval: 0 }}>
<PageSettingsContext.Provider value={[settings, vi.fn()]}>
<PageDialogProvider>
<OpenPlatformAboutDialog />
</PageDialogProvider>
</PageSettingsContext.Provider>
</SWRConfig>
);
}
async function expectAboutDialogAbsent() {
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
}
async function setupLightAboutDialog() {
const user = userEvent.setup();
mountAbout({ activeTheme: 'light' });
const dialog = await screen.findByRole('dialog');
return { user, dialog };
}
describe('PlatformAbout', () => {
const server = setupServer(
http.get(awxAPI`/ping/`, () => HttpResponse.json({ version: '4.5.0' })),
http.get(hubAPI`/`, () => HttpResponse.json({ galaxy_ng_version: '4.9.0' })),
http.get(edaAPI`/config/`, () => HttpResponse.json({ version: '1.2.0' }))
);
beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
it('should display complete content with versions from APIs', async () => {
mountAbout({ activeTheme: 'light' });
const dialog = await screen.findByRole('dialog');
expect(within(dialog).getByText(/Ansible Automation Platform/)).toBeInTheDocument();
expect(within(dialog).getByText(/Copyright.*Red Hat/)).toBeInTheDocument();
await waitFor(() => {
expect(within(dialog).getByText('Automation Controller Version')).toBeInTheDocument();
expect(within(dialog).getByText('4.5.0')).toBeInTheDocument();
});
await waitFor(() => {
expect(within(dialog).getByText('Event-Driven Ansible Version')).toBeInTheDocument();
expect(within(dialog).getByText('1.2.0')).toBeInTheDocument();
});
await waitFor(() => {
expect(within(dialog).getByText('Automation Hub Version')).toBeInTheDocument();
expect(within(dialog).getByText('4.9.0')).toBeInTheDocument();
});
});
it.each([
{
label: 'light',
activeTheme: 'light' as const,
expectedSrc: platformLogo,
},
{
label: 'dark',
activeTheme: 'dark' as const,
expectedSrc: platformLogoWhite,
},
])('should set brand image src for %# $label theme', async ({ activeTheme, expectedSrc }) => {
mountAbout({ activeTheme });
const dialog = await screen.findByRole('dialog');
expect(within(dialog).getByRole('img', { name: 'Brand Logo' })).toHaveAttribute(
'src',
expectedSrc
);
});
it('should close when the close button is activated', async () => {
const { user } = await setupLightAboutDialog();
await user.click(screen.getByRole('button', { name: /close/i }));
await expectAboutDialogAbsent();
});
it('should close when Escape is pressed', async () => {
const { user, dialog } = await setupLightAboutDialog();
dialog.focus();
await user.keyboard('{Escape}');
await expectAboutDialogAbsent();
});
});