Skip to content

Commit 37d0ba9

Browse files
committed
Tests
1 parent 6eb691b commit 37d0ba9

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
@@ -2,11 +2,11 @@ import React from 'react';
22
import {describe, it, expect} from '@jest/globals';
33
import {render, screen} from '@testing-library/preact';
44
import AppElement from '~/components/shell/shell';
5-
import {BrowserRouter, MemoryRouter as MR} from 'react-router-dom';
5+
import * as RRD from 'react-router-dom';
6+
import MR from '~/../../test/helpers/future-memory-router';
67
import ReactModal from 'react-modal';
78

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

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

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

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

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)