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

More frontend tests #468

Merged
merged 3 commits into from
Aug 23, 2021
Merged
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
@@ -1,9 +1,23 @@
import { render } from '@testing-library/react';
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react';
import { getSurveyForViewResponse } from 'test-helpers/survey-helpers';
import React from 'react';
import { codes } from 'test-helpers/code-helpers';
import SurveyProprietaryData from './SurveyProprietaryData';
import { getProjectForViewResponse } from 'test-helpers/project-helpers';
import { useBiohubApi } from 'hooks/useBioHubApi';
import { UPDATE_GET_SURVEY_ENTITIES } from 'interfaces/useSurveyApi.interface';

jest.mock('../../../../hooks/useBioHubApi');
const mockUseBiohubApi = {
survey: {
getSurveyForUpdate: jest.fn(),
updateSurvey: jest.fn()
}
};

const mockBiohubApi = ((useBiohubApi as unknown) as jest.Mock<typeof mockUseBiohubApi>).mockReturnValue(
mockUseBiohubApi
);

const mockRefresh = jest.fn();

Expand All @@ -19,9 +33,234 @@ const renderContainer = () => {
};

describe('SurveyProprietaryData', () => {
beforeEach(() => {
// clear mocks before each test
mockBiohubApi().survey.getSurveyForUpdate.mockClear();
mockBiohubApi().survey.updateSurvey.mockClear();
mockRefresh.mockClear();
});

afterEach(() => {
cleanup();
});

it('renders correctly', () => {
const { asFragment } = renderContainer();

expect(asFragment()).toMatchSnapshot();
});

it('editing the survey proprietary data works in the dialog (not proprietary data)', async () => {
mockBiohubApi().survey.getSurveyForUpdate.mockResolvedValue({
survey_proprietor: {
id: 23,
revision_count: 1
}
});

const { getByText, queryByText } = renderContainer();

await waitFor(() => {
expect(getByText('Proprietary Data')).toBeVisible();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(mockBiohubApi().survey.getSurveyForUpdate).toBeCalledWith(1, getSurveyForViewResponse.survey_details.id, [
UPDATE_GET_SURVEY_ENTITIES.survey_proprietor
]);
});

await waitFor(() => {
expect(getByText('Edit Survey Proprietor')).toBeVisible();
});

fireEvent.click(getByText('Cancel'));

await waitFor(() => {
expect(queryByText('Edit Survey Proprietor')).not.toBeInTheDocument();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(getByText('Edit Survey Proprietor')).toBeVisible();
});

fireEvent.click(getByText('Save Changes'));

await waitFor(() => {
expect(mockBiohubApi().survey.updateSurvey).toBeCalledWith(1, getSurveyForViewResponse.survey_details.id, {
survey_proprietor: {
id: 23,
revision_count: 1,
category_rationale: '',
data_sharing_agreement_required: 'false',
first_nations_id: 0,
proprietary_data_category: 0,
proprietor_name: '',
survey_data_proprietary: 'false'
}
});

expect(mockRefresh).toBeCalledTimes(1);
});
});

it('editing the survey proprietary data works in the dialog (proprietary data)', async () => {
mockBiohubApi().survey.getSurveyForUpdate.mockResolvedValue({
survey_proprietor: {
id: 23,
revision_count: 1,
proprietary_data_category: 1,
first_nations_id: 0,
category_rationale: 'rationale',
proprietor_name: 'prop name',
data_sharing_agreement_required: 'true',
survey_data_proprietary: 'true'
}
});

const { getByText, queryByText } = renderContainer();

await waitFor(() => {
expect(getByText('Proprietary Data')).toBeVisible();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(mockBiohubApi().survey.getSurveyForUpdate).toBeCalledWith(1, getSurveyForViewResponse.survey_details.id, [
UPDATE_GET_SURVEY_ENTITIES.survey_proprietor
]);
});

await waitFor(() => {
expect(getByText('Edit Survey Proprietor')).toBeVisible();
});

fireEvent.click(getByText('Cancel'));

await waitFor(() => {
expect(queryByText('Edit Survey Proprietor')).not.toBeInTheDocument();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(getByText('Edit Survey Proprietor')).toBeVisible();
});

fireEvent.click(getByText('Save Changes'));

await waitFor(() => {
expect(mockBiohubApi().survey.updateSurvey).toBeCalledWith(1, getSurveyForViewResponse.survey_details.id, {
survey_proprietor: {
id: 23,
revision_count: 1,
proprietary_data_category: 1,
first_nations_id: 0,
category_rationale: 'rationale',
proprietor_name: 'prop name',
data_sharing_agreement_required: 'true',
survey_data_proprietary: 'true'
}
});

expect(mockRefresh).toBeCalledTimes(1);
});
});

it('displays an error dialog when fetching the update data fails', async () => {
mockBiohubApi().survey.getSurveyForUpdate.mockResolvedValue(null);

const { getByText, queryByText } = renderContainer();

await waitFor(() => {
expect(getByText('Proprietary Data')).toBeVisible();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(getByText('Error Editing Survey Proprietor')).toBeVisible();
});

fireEvent.click(getByText('Ok'));

await waitFor(() => {
expect(queryByText('Error Editing Survey Proprietor')).not.toBeInTheDocument();
});
});

it('shows error dialog with API error message when getting survey proprietor data for update fails', async () => {
mockBiohubApi().survey.getSurveyForUpdate = jest.fn(() => Promise.reject(new Error('API Error is Here')));

const { getByText, queryByText, getAllByRole } = renderContainer();

await waitFor(() => {
expect(getByText('Proprietary Data')).toBeVisible();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(queryByText('API Error is Here')).toBeInTheDocument();
});

// Get the backdrop, then get the firstChild because this is where the event listener is attached
//@ts-ignore
fireEvent.click(getAllByRole('presentation')[0].firstChild);

await waitFor(() => {
expect(queryByText('API Error is Here')).toBeNull();
});
});

it('shows error dialog with API error message when updating survey proprietor data fails', async () => {
mockBiohubApi().survey.getSurveyForUpdate.mockResolvedValue({
survey_proprietor: {
id: 23,
revision_count: 1,
proprietary_data_category: 1,
first_nations_id: 0,
category_rationale: 'rationale',
proprietor_name: 'prop name',
data_sharing_agreement_required: 'true',
survey_data_proprietary: 'true'
}
});
mockBiohubApi().survey.updateSurvey = jest.fn(() => Promise.reject(new Error('API Error is Here')));

const { getByText, queryByText } = renderContainer();

await waitFor(() => {
expect(getByText('Proprietary Data')).toBeVisible();
});

fireEvent.click(getByText('Edit'));

await waitFor(() => {
expect(mockBiohubApi().survey.getSurveyForUpdate).toBeCalledWith(1, getSurveyForViewResponse.survey_details.id, [
UPDATE_GET_SURVEY_ENTITIES.survey_proprietor
]);
});

await waitFor(() => {
expect(getByText('Edit Survey Proprietor')).toBeVisible();
});

fireEvent.click(getByText('Save Changes'));

await waitFor(() => {
expect(queryByText('API Error is Here')).toBeInTheDocument();
});

fireEvent.click(getByText('Ok'));

await waitFor(() => {
expect(queryByText('API Error is Here')).toBeNull();
});
});
});