Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: unit tests for CohortsOverlapDiagram #1238

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const CohortsOverlapDiagram = ({
statusStudyPopulationAndCaseAndControl,
].some((status) => status === 'loading')
) {
return <Spin />;
return <Spin data-testid='loading-spinner' />;
}
const eulerArgs = {
set1Size: selectedStudyPopulationCohort.size,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { QueryClient, QueryClientProvider } from 'react-query';
import CohortsOverlapDiagram from './CohortsOverlapDiagram';
import ValidState from '../../../TestData/States/ValidState';
import { SourceContextProvider } from '../../../Utils/Source';
import {
fetchSimpleOverlapInfo,
useSourceFetch,
} from '../../../Utils/cohortMiddlewareApi';

// Mock relevant API calls:
jest.mock('../../../Utils/cohortMiddlewareApi');
fetchSimpleOverlapInfo.mockResolvedValue({
cohort_overlap: {
case_control_overlap: 123,
},
});
useSourceFetch.mockResolvedValue({
sourceId: 2, loading: false,
});

// Other generic arguments and functions shared by tests below:
const mockedQueryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
});

// wrapper render method:
const renderDiagram = (
selectedStudyPopulationCohort, dispatch) => render(
<QueryClientProvider client={mockedQueryClient}>
<SourceContextProvider>
<CohortsOverlapDiagram
dispatch={dispatch}
selectedStudyPopulationCohort={selectedStudyPopulationCohort}
selectedCaseCohort={selectedStudyPopulationCohort}
selectedControlCohort={selectedStudyPopulationCohort}
selectedCovariates={[]}
outcome={null}
/>
</SourceContextProvider>
</QueryClientProvider>,
);

// Tests:
describe('CohortsOverlapDiagram component', () => {
beforeEach(() => {
// workaround for issue https://github.com/jsdom/jsdom/issues/1664 / https://stackoverflow.com/questions/53725064/d3node-getcomputedtextlength-is-not-a-function:
window.SVGElement.prototype.getComputedTextLength = () => 12345;
});

afterEach(() => {
delete window.SVGElement.prototype.getComputedTextLength;
});

const flushPromises = () => new Promise(setImmediate);
const {
selectedStudyPopulationCohort,
} = ValidState;

it('should show a spinner and then, once data promises are resolved, it should render an Euler diagram', async () => {
renderDiagram(selectedStudyPopulationCohort, jest.fn());
// first thing that appears is a spinner, while web-service responses are pending:
expect(screen.getByTestId('loading-spinner')).toBeInTheDocument();
expect(screen.queryByTestId('euler-diagram')).toBeNull();
// flush the pending mock web-service responses:
await flushPromises();
// now we should expect to see the diagram, and the spinner to be gone:
expect(screen.getByTestId('euler-diagram')).toBeInTheDocument();
expect(screen.queryByTestId('loading-spinner')).toBeNull();
});
});
2 changes: 1 addition & 1 deletion src/Analysis/GWASV2/TestData/States/ValidState.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ValidState = {
},
selectedStudyPopulationCohort: {
cohort_definition_id: 404,
cohort_name: 'Catch All (do not run generate)',
cohort_name: 'Mock study population cohort',
size: 510904,
},
covariates: [
Expand Down