Skip to content

Commit 65df32b

Browse files
committed
Tests
1 parent 4edc679 commit 65df32b

File tree

4 files changed

+105
-48
lines changed

4 files changed

+105
-48
lines changed

test/helpers/future-memory-router.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import {MemoryRouter} from 'react-router-dom';
3+
4+
// @ts-expect-error does not exist on
5+
const {routerFuture} = global;
6+
7+
export default function FutureMemoryRouter(props: Parameters<typeof MemoryRouter>[0]) {
8+
return <MemoryRouter {...props} future={routerFuture} />;
9+
}

test/src/components/shell.test.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import {render, screen} from '@testing-library/preact';
33
import AppElement from '~/components/shell/shell';
4-
import {BrowserRouter, MemoryRouter as MR} from 'react-router-dom';
4+
import * as RRD from 'react-router-dom';
5+
import MR from '~/../../test/helpers/future-memory-router';
56
import ReactModal from 'react-modal';
67

7-
// @ts-expect-error does not exist on
8-
const {routerFuture} = global;
8+
const {BrowserRouter} = RRD;
99

1010
jest.mock('react-router-dom', () => {
1111
const actualRouterDom = jest.requireActual('react-router-dom');
@@ -37,7 +37,7 @@ describe('shell', () => {
3737
console.warn = jest.fn();
3838
console.debug = jest.fn();
3939
(BrowserRouter as jest.Mock).mockImplementationOnce(({children}) => (
40-
<MR initialEntries={['/embedded/contact']} future={routerFuture}>{children}</MR>
40+
<MR initialEntries={['/embedded/contact']} >{children}</MR>
4141
));
4242

4343
render(AppElement);
@@ -46,7 +46,7 @@ describe('shell', () => {
4646
});
4747
it('Delivers normal contact page', async () => {
4848
(BrowserRouter as jest.Mock).mockImplementationOnce(({children}) => (
49-
<MR initialEntries={['/contact']} future={routerFuture}>{children}</MR>
49+
<MR initialEntries={['/contact']}>{children}</MR>
5050
));
5151

5252
render(AppElement);
@@ -63,4 +63,15 @@ describe('shell', () => {
6363
(ReactModal.setAppElement as jest.Mock).mockImplementation(() => externalResolution('ok'));
6464
await modalCalled;
6565
});
66+
it('delivers press page in a portal', async () => {
67+
(BrowserRouter as jest.Mock).mockImplementationOnce(({children}) => (
68+
<MR initialEntries={['/some-portal/press']}>{children}</MR>
69+
));
70+
jest.spyOn(RRD, 'useParams').mockReturnValue({portal: 'some-portal', '*': '/contact'});
71+
72+
render(AppElement);
73+
const pressLink = await screen.findByRole('link', {name: 'Press'});
74+
75+
expect(pressLink.getAttribute('href')).toBe('/some-portal/press');
76+
});
6677
});

test/src/pages/adoption/adoption.test.js

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React from 'react';
2+
import {render, screen} from '@testing-library/preact';
3+
import userEvent from '@testing-library/user-event';
4+
import AdoptionForm from '~/pages/adoption/adoption';
5+
import FormHeader from '~/components/form-header/form-header';
6+
import MemoryRouter from '~/../../test/helpers/future-memory-router';
7+
import {MainClassContextProvider} from '~/contexts/main-class';
8+
import {SharedDataContextProvider} from '~/contexts/shared-data';
9+
import {LanguageContextProvider} from '~/contexts/language';
10+
import usePortalContext, {PortalContextProvider} from '~/contexts/portal';
11+
12+
describe('adoption-form', () => {
13+
const saveWarn = console.warn;
14+
15+
beforeAll(() => {
16+
console.warn = jest.fn();
17+
});
18+
afterAll(() => {
19+
console.warn = saveWarn;
20+
});
21+
22+
beforeEach(async () => {
23+
render(
24+
<LanguageContextProvider>
25+
<SharedDataContextProvider>
26+
<MemoryRouter
27+
initialEntries={['/details/books/college-algebra', '/adoption']}
28+
>
29+
<MainClassContextProvider>
30+
<AdoptionForm />
31+
</MainClassContextProvider>
32+
</MemoryRouter>
33+
</SharedDataContextProvider>
34+
</LanguageContextProvider>
35+
);
36+
await screen.findByText(/Let us know you're using/);
37+
});
38+
39+
it('creates with role selector', () =>
40+
expect(screen.queryAllByRole('option', {hidden: true})).toHaveLength(8));
41+
42+
it('form appears when role is selected', async () => {
43+
const listBoxes = screen.queryAllByRole('listbox');
44+
const user = userEvent.setup();
45+
46+
await user.click(listBoxes[1]);
47+
const options = await screen.findAllByRole('option', {hidden: true});
48+
const instructorOption = options.find(
49+
(o) => o.textContent === 'Instructor'
50+
);
51+
52+
await user.click(instructorOption as HTMLElement);
53+
await screen.findByRole('form');
54+
});
55+
});
56+
57+
describe('form-header in portal', () => {
58+
function Component() {
59+
const {setPortal} = usePortalContext();
60+
61+
setPortal('some-portal');
62+
63+
return <FormHeader prefix="adoption" />;
64+
}
65+
66+
it('rewrites links', async () => {
67+
render(
68+
<LanguageContextProvider>
69+
<MemoryRouter initialEntries={['/some-portal/adoption']}>
70+
<PortalContextProvider>
71+
<Component />
72+
</PortalContextProvider>
73+
</MemoryRouter>
74+
</LanguageContextProvider>
75+
);
76+
const link = await screen.findByRole('link');
77+
78+
expect(link.getAttribute('href')).toBe('/some-portal/interest');
79+
});
80+
});

0 commit comments

Comments
 (0)