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

PSP-7209 Fix research container race condition #3581

Merged
merged 2 commits into from
Nov 9, 2023
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 @@ -3,15 +3,17 @@ import MockAdapter from 'axios-mock-adapter';
import { createMemoryHistory } from 'history';

import { useMapStateMachine } from '@/components/common/mapFSM/MapStateMachineContext';
import { FileTypes } from '@/constants';
import { Claims } from '@/constants/claims';
import { mockAcquisitionFileResponse } from '@/mocks/acquisitionFiles.mock';
import { mockLookups } from '@/mocks/lookups.mock';
import { mapMachineBaseMock } from '@/mocks/mapFSM.mock';
import { getMockResearchFile } from '@/mocks/researchFile.mock';
import { Api_ResearchFile } from '@/models/api/ResearchFile';
import { lookupCodesSlice } from '@/store/slices/lookupCodes';
import { render, RenderOptions, waitForElementToBeRemoved } from '@/utils/test-utils';

import { SideBarContextProvider } from '../context/sidebarContext';
import { SideBarContextProvider, TypedFile } from '../context/sidebarContext';
import ResearchContainer, { IResearchContainerProps } from './ResearchContainer';

const history = createMemoryHistory();
Expand All @@ -35,9 +37,13 @@ const onClose = jest.fn();

describe('ResearchContainer component', () => {
// render component under test
const setup = (props: Partial<IResearchContainerProps>, renderOptions: RenderOptions = {}) => {
const setup = (
renderOptions: RenderOptions & { props?: Partial<IResearchContainerProps> } & {
context?: { file?: TypedFile };
} = {},
) => {
const utils = render(
<SideBarContextProvider>
<SideBarContextProvider {...renderOptions?.context}>
<ResearchContainer researchFileId={getMockResearchFile().id as number} onClose={onClose} />
</SideBarContextProvider>,
{
Expand All @@ -55,87 +61,86 @@ describe('ResearchContainer component', () => {
};
};

const mockResearchFile = getMockResearchFile();

beforeEach(() => {
mockAxios.resetHistory();
mockAxios.reset();
mockAxios.onGet(/users\/info.*?/).reply(200, {});

mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}`).reply(200, {
...mockResearchFile,
fileProperties: [{ id: 1 }],
} as Api_ResearchFile);
mockAxios
.onGet(`/researchFiles/${mockResearchFile?.id}/properties`)
.reply(200, [{ id: 1, property: { pid: '123-456-789' } }]);
});

afterEach(() => {
jest.clearAllMocks();
});

it('renders as expected', async () => {
const mockResearchFile = getMockResearchFile();
mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}`).reply(200, getMockResearchFile());
mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}/properties`).reply(200, []);
const { getByTestId } = setup({});

const { getByTestId } = setup();
await waitForElementToBeRemoved(getByTestId('filter-backdrop-loading'));
expect(document.body).toMatchSnapshot();
});

it('displays a properties pid if that is the only valid identifier', async () => {
const mockResearchFile = getMockResearchFile();
mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}`).reply(200, {
...mockResearchFile,
fileProperties: [{ id: 1 }],
} as Api_ResearchFile);
mockAxios
.onGet(`/researchFiles/${mockResearchFile?.id}/properties`)
.reply(200, [{ id: 1, property: { pid: '123-456-789' } }]);
const { getByTestId, findByText } = setup({});
it('renders a spinner while loading', async () => {
const { findByTestId } = setup();
const spinner = await findByTestId('filter-backdrop-loading');
expect(spinner).toBeVisible();
});

await waitForElementToBeRemoved(getByTestId('filter-backdrop-loading'));
it('renders research details when file is in context', async () => {
const typedFile: TypedFile = { ...getMockResearchFile(), fileType: FileTypes.Research };
const { findByText } = setup({ context: { file: typedFile } });
expect(await findByText('File Summary')).toBeVisible();
});

it(`doesn't render research details when wrong file type is in context`, async () => {
const typedFile: TypedFile = {
...mockAcquisitionFileResponse(),
fileType: FileTypes.Acquisition,
};
const { findByTestId } = setup({ context: { file: typedFile } });
expect(await findByTestId('filter-backdrop-loading')).toBeVisible();
});

it('displays a properties pid if that is the only valid identifier', async () => {
const { getByTestId, findByText } = setup();
await waitForElementToBeRemoved(getByTestId('filter-backdrop-loading'));
expect(await findByText('123-456-789')).toBeVisible();
});

it('displays a properties pin if that is the only valid identifier', async () => {
const mockResearchFile = getMockResearchFile();
mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}`).reply(200, {
...mockResearchFile,
fileProperties: [{ id: 1 }],
} as Api_ResearchFile);
mockAxios
.onGet(`/researchFiles/${mockResearchFile?.id}/properties`)
.reply(200, [{ id: 1, property: { pin: 123456 } }]);
const { getByTestId, findByText } = setup({});

const { getByTestId, findByText } = setup();
await waitForElementToBeRemoved(getByTestId('filter-backdrop-loading'));

expect(await findByText('123456')).toBeVisible();
});

it('displays a properties plan number if that is the only valid identifier', async () => {
const mockResearchFile = getMockResearchFile();
mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}`).reply(200, {
...mockResearchFile,
fileProperties: [{ id: 1 }],
} as Api_ResearchFile);
mockAxios
.onGet(`/researchFiles/${mockResearchFile?.id}/properties`)
.reply(200, [{ id: 1, property: { planNumber: 'EPP92028' } }]);
const { getByTestId, findByText } = setup({});

const { getByTestId, findByText } = setup();
await waitForElementToBeRemoved(getByTestId('filter-backdrop-loading'));

expect(await findByText('EPP92028')).toBeVisible();
});

it('displays a properties lat/lng if that is the only valid identifier', async () => {
const mockResearchFile = getMockResearchFile();
mockAxios.onGet(`/researchFiles/${mockResearchFile?.id}`).reply(200, {
...mockResearchFile,
fileProperties: [{ id: 1 }],
} as Api_ResearchFile);
mockAxios
.onGet(`/researchFiles/${mockResearchFile?.id}/properties`)
.reply(200, [{ id: 1, property: { latitude: 1, longitude: 2 } }]);
const { getByTestId, findByText } = setup({});

const { getByTestId, findByText } = setup();
await waitForElementToBeRemoved(getByTestId('filter-backdrop-loading'));

expect(await findByText('2.000000, 1.000000')).toBeVisible();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ export const ResearchContainer: React.FunctionComponent<
setIsShowingPropertySelector(true);
};

if (researchFile === undefined && (loadingResearchFile || loadingResearchFileProperties)) {
return (
<>
<LoadingBackdrop show={true} parentScreen={true}></LoadingBackdrop>
</>
);
if (
loadingResearchFile ||
(loadingResearchFileProperties && !isShowingPropertySelector) ||
researchFile?.fileType !== FileTypes.Research ||
researchFile?.id !== researchFileId
) {
return <LoadingBackdrop show={true} parentScreen={true}></LoadingBackdrop>;
}

if (isShowingPropertySelector && researchFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ exports[`ResearchContainer component renders as expected 1`] = `
</div>
<div
class="col"
/>
>
123-456-789
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ const ResearchSummaryView: React.FunctionComponent<IResearchSummaryViewProps> =
</StyledEditWrapper>
<Section header="Project">
<SectionField label="Ministry project">
{detail.researchFileProjects.map(formattedName => (
<div>{formattedName}</div>
{detail.researchFileProjects.map((formattedName, i) => (
<div key={`project-key-${i}`}>{formattedName}</div>
))}
</SectionField>
</Section>
Expand Down
Loading