Skip to content

Commit

Permalink
FI-2000: Improve frontend testing coverage (#369)
Browse files Browse the repository at this point in the history
* add footer and presets tests

* add testsession and configmessages tests

* add test run button tests

* add test run mock data

* add test suite tests

* add progress bar tests

* rename requests test file

* update test list item tests

* add selection panel tests

* remove extra package lock

* add testsession and actionmodal tests

* fix selection type errors

* update types

* resolve semver vulnerability

* convert test session to vitest

* remove page test while actions are incorrectly mocking document title

* locally define globals

* add button tests for request list

---------

Co-authored-by: Alyssa Wang <[email protected]>
  • Loading branch information
AlyssaWang and AlyssaWang authored Jul 6, 2023
1 parent 87b7a5c commit 03e55f0
Show file tree
Hide file tree
Showing 42 changed files with 1,129 additions and 387 deletions.
28 changes: 28 additions & 0 deletions client/src/components/App/__tests__/Page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { RouterProvider, createMemoryRouter } from 'react-router-dom';
import { render /* , waitFor */ } from '@testing-library/react';
import { testSuites } from '~/components/App/__mocked_data__/mockData';
import Page from '../Page';

describe('The Page Component', () => {
test('sets page title on render', /* async */ () => {
const pageTitle = 'Inferno Test Suites';
const routes = [
{
path: '/',
element: (
<Page title={pageTitle}>
<div>child component</div>
</Page>
),
loader: () => testSuites,
},
];

const router = createMemoryRouter(routes, { initialEntries: ['/'] });
render(<RouterProvider router={router} />);

// TODO: comment out until Github actions can mock document title
// await waitFor(() => expect(document.title).toEqual(pageTitle));
});
});
1 change: 0 additions & 1 deletion client/src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const Footer: FC<FooterProps> = ({ version, linkList }) => {
// To test locally, set apiBase to 'http://127.0.0.1:4000/inferno-core/api-docs/'
const apiBase = 'https://inferno-framework.github.io/inferno-core/api-docs/';
const hostname = window.location.host;
console.log(window.location);
const fullHost = `${hostname}/${basePath}`;
const scheme = window.location.protocol;

Expand Down
19 changes: 19 additions & 0 deletions client/src/components/Footer/__tests__/Footer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import Footer from '..';
import ThemeProvider from 'components/ThemeProvider';
import { basePath } from '~/api/infernoApiService';

test('renders Inferno Footer', () => {
render(
Expand Down Expand Up @@ -38,3 +39,21 @@ test('can be given a list of links to display', () => {
expect(screen.getByRole('link', { name: /One/ })).toHaveAttribute('href', 'http://one.com');
expect(screen.getByRole('link', { name: /Two/ })).toHaveAttribute('href', 'http://two.com');
});

test('displays API link with scheme and host name', () => {
const apiBase = 'https://inferno-framework.github.io/inferno-core/api-docs/';
const hostname = window.location.host;
const fullHost = `${hostname}/${basePath}`;
const scheme = window.location.protocol;

render(
<ThemeProvider>
<Footer version={'dummyVersion'} />
</ThemeProvider>
);

expect(screen.getByRole('link', { name: /API/ })).toHaveAttribute(
'href',
`${apiBase}?scheme=${scheme}&host=${fullHost}`
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const testInputsDefaults: TestInput[] = [
},
];

test('Input modal not visible if visibility set to false', () => {
test('Modal visible and inputs are shown', () => {
render(
<ThemeProvider>
<SnackbarProvider>
Expand All @@ -55,11 +55,22 @@ test('Input modal not visible if visibility set to false', () => {
</SnackbarProvider>
</ThemeProvider>
);
const titleText = screen.queryByText('Test Inputs');
expect(titleText).toBeNull();

const titleText = screen.getByText('Modal Title');
expect(titleText).toBeVisible();

testInputs.forEach((input: TestInput) => {
if (input.optional) {
const inputField = screen.getByLabelText(input.name);
expect(inputField).toBeVisible();
} else {
const inputField = screen.getByLabelText(input.name + ' (required) *');
expect(inputField).toBeVisible();
}
});
});

test('Modal visible and inputs are shown', () => {
test('Pressing cancel hides the modal', () => {
render(
<ThemeProvider>
<SnackbarProvider>
Expand All @@ -76,21 +87,12 @@ test('Modal visible and inputs are shown', () => {
</ThemeProvider>
);

const titleText = screen.getByText('Modal Title');
expect(titleText).toBeVisible();

testInputs.forEach((input: TestInput) => {
if (input.optional) {
const inputField = screen.getByLabelText(input.name);
expect(inputField).toBeVisible();
} else {
const inputField = screen.getByLabelText(input.name + ' (required) *');
expect(inputField).toBeVisible();
}
});
const cancelButton = screen.getByTestId('cancel-button');
userEvent.click(cancelButton);
expect(hideModalMock).toHaveBeenCalled();
});

test('Pressing cancel hides the modal', () => {
test('Pressing submit hides the modal', () => {
render(
<ThemeProvider>
<SnackbarProvider>
Expand All @@ -107,8 +109,8 @@ test('Pressing cancel hides the modal', () => {
</ThemeProvider>
);

const cancelButton = screen.getByTestId('cancel-button');
userEvent.click(cancelButton);
const submitButton = screen.getByText('Submit');
userEvent.click(submitButton);
expect(hideModalMock).toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('should enable Start Testing when test suite is selected', () => {
</BrowserRouter>
);

const testSuiteElement = screen.getAllByTestId('testing-suite-option')[0];
const testSuiteElement = screen.getAllByTestId('list-option')[0];
const buttonElement = screen.getByTestId('go-button');

userEvent.click(testSuiteElement);
Expand Down
12 changes: 12 additions & 0 deletions client/src/components/PresetsSelector/__mocked_data__/mockData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PresetSummary } from 'models/testSuiteModels';

export const presets: PresetSummary[] = [
{
id: 'one',
title: 'Preset One',
},
{
id: 'two',
title: 'Preset Two',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { SnackbarProvider } from 'notistack';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ThemeProvider from '~/components/ThemeProvider';
import PresetsSelector from '../PresetsSelector';
import { presets } from '../__mocked_data__/mockData';

describe('The PresetsSelector Component', () => {
test('renders empty PresetsSelector', () => {
render(
<BrowserRouter>
<ThemeProvider>
<SnackbarProvider>
<PresetsSelector presets={[]} testSessionId="test-id" getSessionData={() => {}} />
</SnackbarProvider>
</ThemeProvider>
</BrowserRouter>
);

const selectionElement = screen.getByRole('button');
expect(selectionElement).toBeInTheDocument();
});

test('renders PresetsSelector with options', () => {
render(
<BrowserRouter>
<ThemeProvider>
<SnackbarProvider>
<PresetsSelector presets={presets} testSessionId="test-id" getSessionData={() => {}} />
</SnackbarProvider>
</ThemeProvider>
</BrowserRouter>
);

const selectionElement = screen.getByRole('button');
expect(selectionElement).toBeInTheDocument();
});

test('selects a preset', () => {
render(
<BrowserRouter>
<ThemeProvider>
<SnackbarProvider>
<PresetsSelector presets={presets} testSessionId="test-id" getSessionData={() => {}} />
</SnackbarProvider>
</ThemeProvider>
</BrowserRouter>
);

const selectionElement = screen.getByRole('button');
userEvent.click(selectionElement);

const presetChoice = screen.getByText('Preset One');
userEvent.click(presetChoice);

expect(selectionElement.textContent).toEqual('Preset One');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ export const codeResponseWithJSON: Request = {
response_body:
'{"resourceType": "OperationOutcome", "issue": [ {"severity": "error", "code": "processing", "diagnostics": "Bearer token is invalid or not supplied Supplied Bearer Token: null" } ]}',
};

export const mockedUpdateRequest = (requestId: string, resultId: string, request: Request) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const data = [requestId, resultId, request];
};

export const mockedRequestFunctions = {
updateRequest: mockedUpdateRequest,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { render, screen } from '@testing-library/react';
import { SnackbarProvider } from 'notistack';
import ThemeProvider from 'components/ThemeProvider';
import { testSuites } from '~/components/App/__mocked_data__/mockData';
import ConfigMessagesDetailsPanel from '../ConfigMessagesDetailsPanel';

test('renders ConfigMessagesDetailsPanel', () => {
render(
<BrowserRouter>
<ThemeProvider>
<SnackbarProvider>
<ConfigMessagesDetailsPanel testSuite={testSuites[0]} />
</SnackbarProvider>
</ThemeProvider>
</BrowserRouter>
);

const tabsList = screen.getAllByRole('tab');
expect(tabsList.length).toEqual(3);

expect(screen.getByLabelText('Errors')).toBeInTheDocument();
expect(screen.getByLabelText('Warnings')).toBeInTheDocument();
expect(screen.getByLabelText('Info')).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
mockedTest,
mockedTestGroup,
mockedTestSuite,
} from '~/components/_common/__mocked_data__/mockData';
import { RunnableType } from '~/models/testSuiteModels';

export const mockedRunTests = (runnableType: RunnableType, runnableId: string) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const data = [runnableType, runnableId];
};

export const mockedTestRunButtonData = {
test: mockedTest,
testGroup: mockedTestGroup,
testSuite: mockedTestSuite,
runTests: mockedRunTests,
};
Loading

0 comments on commit 03e55f0

Please sign in to comment.