Skip to content

Commit

Permalink
Boban/6115 prevent correct result from deleted facility (#6220)
Browse files Browse the repository at this point in the history
* fix isDeleted field resolver to use keyword 'get'

* add isDeleted to `GetFacilityResultsMultiplexWithCount` query

* add error message and disable button for invalid facility deleted cases

* Add bottom text for facilities that are deleted

* update copy

* remove unused variable

* replace logical or with nullis coalescing

* change type of Boolean to boolean for isFacilityDeleted

* change boolean type again
  • Loading branch information
BobanL authored Jul 28, 2023
1 parent ca44471 commit 527dddb
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 83 deletions.
254 changes: 175 additions & 79 deletions frontend/src/app/testResults/TestResultCorrectionModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { act, render, screen, waitFor, within } from "@testing-library/react";
import "./TestResultCorrectionModal.scss";
import userEvent from "@testing-library/user-event";
import configureStore from "redux-mock-store";
import { MockedResponse } from "@apollo/client/testing/core";

import {
TestCorrectionReasons,
Expand Down Expand Up @@ -60,6 +61,26 @@ jest.mock("react-router-dom", () => {
useNavigate: () => mockedNavigate,
};
});
const renderModal = (
isFacilityDeleted: boolean,
mocks: ReadonlyArray<MockedResponse>,
initialEntries?: any[] | undefined
) => {
render(
<Provider store={store}>
<MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter initialEntries={initialEntries}>
<DetachedTestResultCorrectionModal
data={testResult}
testResultId={internalId}
closeModal={() => {}}
isFacilityDeleted={isFacilityDeleted}
/>
</MemoryRouter>
</MockedProvider>
</Provider>
);
};

describe("TestResultCorrectionModal", () => {
let component: any;
Expand All @@ -71,19 +92,7 @@ describe("TestResultCorrectionModal", () => {
});

it("renders the correction reason dropdown menu", async () => {
render(
<Provider store={store}>
<MockedProvider mocks={[]} addTypename={false}>
<MemoryRouter>
<DetachedTestResultCorrectionModal
data={testResult}
testResultId={internalId}
closeModal={() => {}}
/>
</MemoryRouter>
</MockedProvider>
</Provider>
);
renderModal(false, []);

const expectedCorrectionReasons = Object.values(TestCorrectionReasons);

Expand All @@ -107,6 +116,7 @@ describe("TestResultCorrectionModal", () => {
data={testResult}
testResultId={internalId}
closeModal={() => {}}
isFacilityDeleted={false}
/>
</MemoryRouter>
</MockedProvider>
Expand Down Expand Up @@ -138,33 +148,23 @@ describe("TestResultCorrectionModal", () => {
},
},
];

beforeEach(() => {
render(
<Provider store={store}>
<MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter>
<DetachedTestResultCorrectionModal
data={testResult}
testResultId={internalId}
closeModal={() => {}}
/>
</MemoryRouter>
</MockedProvider>
</Provider>
);
markAsErrorMockDidComplete = false;
});
it.each([true, false])(
"sends a GraphQL request to perform the removal",
async (isFacilityDeleted) => {
renderModal(isFacilityDeleted, mocks);
await act(
async () =>
await userEvent.click(await screen.findByText("Yes, I'm sure"))
);

it("sends a GraphQL request to perform the removal", async () => {
await act(
async () =>
await userEvent.click(await screen.findByText("Yes, I'm sure"))
);

await waitFor(() => {
expect(markAsErrorMockDidComplete).toBe(true);
});
});
await waitFor(() => {
expect(markAsErrorMockDidComplete).toBe(true);
});
}
);
});

describe("when correcting for incorrect result", () => {
Expand All @@ -191,24 +191,11 @@ describe("TestResultCorrectionModal", () => {
];

beforeEach(() => {
render(
<Provider store={store}>
<MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter
initialEntries={[`/results/1?facility=${mockFacilityID}`]}
>
<DetachedTestResultCorrectionModal
data={testResult}
testResultId={internalId}
closeModal={() => {}}
/>
</MemoryRouter>
</MockedProvider>
</Provider>
);
markAsCorrectMockDidComplete = false;
});

it("sends a GraphQL request on submit to initiate correction", async () => {
renderModal(false, mocks, [`/results/1?facility=${mockFacilityID}`]);
const dropdown = await screen.findByLabelText(
"Please select a reason for correcting this test result."
);
Expand All @@ -231,6 +218,24 @@ describe("TestResultCorrectionModal", () => {
);
});
});
it("prevents submission when facility is deleted", async () => {
renderModal(true, mocks, [`/results/1?facility=${mockFacilityID}`]);
const dropdown = await screen.findByLabelText(
"Please select a reason for correcting this test result."
);
await act(
async () =>
await userEvent.selectOptions(
dropdown,
TestCorrectionReasons.INCORRECT_RESULT
)
);

expect(await screen.findByText("Yes, I'm sure")).toBeDisabled();
expect(
screen.getByText("Can't update test result for deleted facility")
).toBeInTheDocument();
});
});

describe("when correcting for a reason not listed", () => {
Expand Down Expand Up @@ -274,21 +279,7 @@ describe("TestResultCorrectionModal", () => {
},
},
];
beforeEach(async () => {
render(
<Provider store={store}>
<MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter>
<DetachedTestResultCorrectionModal
data={testResult}
testResultId={internalId}
closeModal={() => {}}
/>
</MemoryRouter>
</MockedProvider>
</Provider>
);

const selectOther = async () => {
const dropdown = await screen.findByLabelText(
"Please select a reason for correcting this test result."
);
Expand All @@ -300,9 +291,15 @@ describe("TestResultCorrectionModal", () => {
expect(
await screen.findByText("Additional information", { exact: false })
).toBeInTheDocument();
};
beforeEach(async () => {
markAsErrorMockDidComplete = false;
markAsCorrectionMockDidComplete = false;
});

it("renders sub-form", async () => {
renderModal(false, mocks);
await selectOther();
const additionalDetails = await screen.findByTestId(
"additionalInformation"
);
Expand All @@ -324,6 +321,8 @@ describe("TestResultCorrectionModal", () => {
});

it("prevents submission if additional details not populated", async () => {
renderModal(false, mocks);
await selectOther();
const correctionActionOption = screen.getByLabelText(
TestCorrectionActions.CORRECT_RESULT
);
Expand All @@ -334,6 +333,8 @@ describe("TestResultCorrectionModal", () => {
});

it("prevents submission if additional details does not meet minimum character requirement", async () => {
renderModal(false, mocks);
await selectOther();
const additionalDetails = await screen.findByTestId(
"additionalInformation"
);
Expand All @@ -349,6 +350,8 @@ describe("TestResultCorrectionModal", () => {
});

it("prevents submission if correction action not selected", async () => {
renderModal(false, mocks);
await selectOther();
const additionalDetails = await screen.findByTestId(
"additionalInformation"
);
Expand All @@ -361,28 +364,57 @@ describe("TestResultCorrectionModal", () => {
});

describe("correction actions", () => {
it("mark as error sends GraphQL request to remove test", async () => {
it.each([true, false])(
"mark as error sends GraphQL request to remove test",
async (isFacilityDeleted) => {
renderModal(isFacilityDeleted, mocks);
await selectOther();
const additionalDetails = await screen.findByTestId(
"additionalInformation"
);
await act(
async () =>
await userEvent.type(additionalDetails, "Some good reason")
);
const correctionActionOption = screen.getByLabelText(
TestCorrectionActions.MARK_AS_ERROR,
{ exact: false }
);
await act(async () => await userEvent.click(correctionActionOption));

const submitButton = await screen.findByText("Yes, I'm sure");
await act(async () => await userEvent.click(submitButton));
await waitFor(() => {
expect(markAsErrorMockDidComplete).toBe(true);
});
}
);

it("mark as incorrect results sends GraphQL request to correct test", async () => {
renderModal(false, mocks);
await selectOther();
const additionalDetails = await screen.findByTestId(
"additionalInformation"
);
await act(
async () =>
await userEvent.type(additionalDetails, "Some good reason")
);
const correctionActionOption = screen.getByLabelText(
TestCorrectionActions.MARK_AS_ERROR,

const correctionActionOption = screen.getAllByLabelText(
TestCorrectionActions.CORRECT_RESULT,
{ exact: false }
);
await act(async () => await userEvent.click(correctionActionOption));
)[0];

await act(async () => await userEvent.click(correctionActionOption));
const submitButton = await screen.findByText("Yes, I'm sure");
await act(async () => await userEvent.click(submitButton));
await waitFor(() => {
expect(markAsErrorMockDidComplete).toBe(true);
});
await waitFor(() => expect(markAsCorrectionMockDidComplete).toBe(true));
});

it("mark as incorrect results sends GraphQL request to correct test", async () => {
it("mark as incorrect results is blocked when facility is deleted", async () => {
renderModal(true, mocks);
await selectOther();
const additionalDetails = await screen.findByTestId(
"additionalInformation"
);
Expand All @@ -397,10 +429,74 @@ describe("TestResultCorrectionModal", () => {
)[0];

await act(async () => await userEvent.click(correctionActionOption));
const submitButton = await screen.findByText("Yes, I'm sure");
await act(async () => await userEvent.click(submitButton));
await waitFor(() => expect(markAsCorrectionMockDidComplete).toBe(true));
expect(await screen.findByText("Yes, I'm sure")).toBeDisabled();
expect(
screen.getByText("Can't update test result for deleted facility")
).toBeInTheDocument();
});
});
});

describe("when correcting for incorrect test date", () => {
let markAsCorrectMockDidComplete = false;
const mocks = [
{
request: {
query: MARK_TEST_AS_CORRECTION,
variables: {
id: internalId,
reason: TestCorrectionReason.INCORRECT_TEST_DATE,
},
},
result: () => {
markAsCorrectMockDidComplete = true;

return {
data: {
correctTestMarkAsCorrection: { internalId },
},
};
},
},
];
beforeEach(() => {
markAsCorrectMockDidComplete = false;
});
it("sends a GraphQL request to edit results", async () => {
renderModal(false, mocks);
const dropdown = await screen.findByLabelText(
"Please select a reason for correcting this test result."
);
await act(
async () =>
await userEvent.selectOptions(
dropdown,
TestCorrectionReasons.INCORRECT_TEST_DATE
)
);
const submitButton = await screen.findByText("Yes, I'm sure");
await act(async () => await userEvent.click(submitButton));
await waitFor(() => {
expect(markAsCorrectMockDidComplete).toBe(true);
});
});
it("should block submission for incorrect test date when facility is deleted", async () => {
renderModal(true, mocks);
const dropdown = await screen.findByLabelText(
"Please select a reason for correcting this test result."
);
await act(
async () =>
await userEvent.selectOptions(
dropdown,
TestCorrectionReasons.INCORRECT_TEST_DATE
)
);

expect(await screen.findByText("Yes, I'm sure")).toBeDisabled();
expect(
screen.getByText("Can't update test date for deleted facility")
).toBeInTheDocument();
});
});
});
Loading

0 comments on commit 527dddb

Please sign in to comment.