Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
refactor: unify test render context (#871)
Browse files Browse the repository at this point in the history
* refactor: fix warning when both project and projects are undefined

* refactor: unify test render context

* refactor: use render route option
  • Loading branch information
olav authored Apr 8, 2022
1 parent 224349d commit 6326d78
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export const ProjectsList: VFC<IProjectsListProps> = ({
projects,
project,
}) => {
let fields = projects && Array.isArray(projects) ? projects : [project];
let fields: string[] =
projects && Array.isArray(projects)
? projects
: project
? [project]
: [];

if (fields.length === 0) {
return <>*</>;
Expand Down
34 changes: 7 additions & 27 deletions src/component/user/Authentication/Authentication.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Authentication from 'component/user/Authentication/Authentication';
import { render, screen } from '@testing-library/react';
import { screen } from '@testing-library/react';
import {
LOGIN_PASSWORD_ID,
LOGIN_EMAIL_ID,
Expand All @@ -8,7 +8,7 @@ import {
SSO_LOGIN_BUTTON,
} from 'utils/testIds';
import React from 'react';
import { TestContext } from 'utils/testContext';
import { render } from 'utils/testRenderer';
import { testServerSetup, testServerRoute } from 'utils/testServer';

const server = testServerSetup();
Expand All @@ -22,11 +22,7 @@ test('should render password auth', async () => {
options: [],
});

render(
<TestContext>
<Authentication redirect="/" />
</TestContext>
);
render(<Authentication redirect="/" />);

await screen.findByTestId(AUTH_PAGE_ID);
expect(screen.getByTestId(LOGIN_EMAIL_ID)).toBeInTheDocument();
Expand All @@ -43,11 +39,7 @@ test('should not render password auth if defaultHidden is true', async () => {
options: [],
});

render(
<TestContext>
<Authentication redirect="/" />
</TestContext>
);
render(<Authentication redirect="/" />);

await screen.findByTestId(AUTH_PAGE_ID);
expect(screen.queryByTestId(LOGIN_EMAIL_ID)).not.toBeInTheDocument();
Expand All @@ -64,11 +56,7 @@ test('should render demo auth', async () => {
options: [],
});

render(
<TestContext>
<Authentication redirect="/" />
</TestContext>
);
render(<Authentication redirect="/" />);

await screen.findByTestId(AUTH_PAGE_ID);
expect(screen.getByTestId(LOGIN_EMAIL_ID)).toBeInTheDocument();
Expand All @@ -85,11 +73,7 @@ test('should render email auth', async () => {
options: [],
});

render(
<TestContext>
<Authentication redirect="/" />
</TestContext>
);
render(<Authentication redirect="/" />);

await screen.findByTestId(AUTH_PAGE_ID);
expect(screen.getByTestId(LOGIN_EMAIL_ID)).toBeInTheDocument();
Expand Down Expand Up @@ -121,11 +105,7 @@ const testSSOAuthOption = async (authOption: string) => {
type: 'password',
});

render(
<TestContext>
<Authentication redirect="/" />
</TestContext>
);
render(<Authentication redirect="/" />);

const ssoLink = await screen.findByTestId(testId);
expect(ssoLink.getAttribute('href')).toEqual(path);
Expand Down
10 changes: 3 additions & 7 deletions src/component/user/ForgottenPassword/ForgottenPassword.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { render, screen } from '@testing-library/react';
import { screen } from '@testing-library/react';
import { FORGOTTEN_PASSWORD_FIELD } from 'utils/testIds';
import React from 'react';
import { TestContext } from 'utils/testContext';
import ForgottenPassword from 'component/user/ForgottenPassword/ForgottenPassword';
import { render } from 'utils/testRenderer';

test('should render password auth', async () => {
render(
<TestContext>
<ForgottenPassword />
</TestContext>
);
render(<ForgottenPassword />);

await screen.findByTestId(FORGOTTEN_PASSWORD_FIELD);
});
13 changes: 3 additions & 10 deletions src/component/user/ResetPassword/ResetPassword.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { render, screen } from '@testing-library/react';
import { screen } from '@testing-library/react';
import { INVALID_TOKEN_BUTTON } from 'utils/testIds';
import React from 'react';
import { TestContext } from 'utils/testContext';
import ResetPassword from 'component/user/ResetPassword/ResetPassword';
import { MemoryRouter } from 'react-router-dom';
import { INVALID_TOKEN_ERROR } from 'hooks/api/getters/useResetPassword/useResetPassword';
import { testServerSetup, testServerRoute } from 'utils/testServer';
import { render } from 'utils/testRenderer';

const server = testServerSetup();

Expand All @@ -14,13 +13,7 @@ test('should render password auth', async () => {
name: INVALID_TOKEN_ERROR,
});

render(
<TestContext>
<MemoryRouter initialEntries={['/new-user?token=invalid']}>
<ResetPassword />
</MemoryRouter>
</TestContext>
);
render(<ResetPassword />, { route: '/new-user?token=invalid' });

await screen.findByTestId(INVALID_TOKEN_BUTTON);
});
15 changes: 0 additions & 15 deletions src/utils/testContext.tsx

This file was deleted.

18 changes: 16 additions & 2 deletions src/utils/testRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { BrowserRouter as Router } from 'react-router-dom';
import { render as rtlRender, RenderOptions } from '@testing-library/react';
import { SWRConfig } from 'swr';
import { ThemeProvider } from '@material-ui/core/styles';
import theme from 'themes/mainTheme';
import React from 'react';
import { BrowserRouter } from 'react-router-dom';

export const render = (
ui: JSX.Element,
Expand All @@ -11,7 +15,17 @@ export const render = (
window.history.pushState({}, 'Test page', route);

return rtlRender(ui, {
wrapper: Router,
wrapper: Wrapper,
...renderOptions,
});
};

const Wrapper: React.FC = ({ children }) => {
return (
<SWRConfig value={{ provider: () => new Map() }}>
<BrowserRouter>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</BrowserRouter>
</SWRConfig>
);
};

0 comments on commit 6326d78

Please sign in to comment.