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

CRDCDH-2416 Submission Request – Cancel/Restore Justification #646

Draft
wants to merge 6 commits into
base: CRDCDH-2458
Choose a base branch
from
Draft
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
208 changes: 206 additions & 2 deletions src/components/CancelApplicationButton/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
import type { Meta, StoryObj } from "@storybook/react";
import { fn, expect } from "@storybook/test";
import { MockedResponse } from "@apollo/client/testing";
import { screen, userEvent, waitFor, within } from "@storybook/testing-library";
import { Context as AuthContext, ContextState as AuthCtxState } from "../Contexts/AuthContext";
import Button from "./index";
import {
CANCEL_APP,
CancelAppInput,
CancelAppResp,
RESTORE_APP,
RestoreAppInput,
RestoreAppResp,
} from "../../graphql";

const mockCancelApp: MockedResponse<CancelAppResp, CancelAppInput> = {
request: {
query: CANCEL_APP,
},
variableMatcher: () => true,
result: {
data: {
cancelApplication: {
_id: "some id",
},
},
},
};

const mockRestoreApp: MockedResponse<RestoreAppResp, RestoreAppInput> = {
request: {
query: RESTORE_APP,
},
variableMatcher: () => true,
result: {
data: {
restoreApplication: {
_id: "some id",
},
},
},
};

const meta: Meta<typeof Button> = {
title: "Submission Requests / Cancel & Restore Button",
component: Button,
tags: ["autodocs"],
parameters: {
apolloClient: {
mocks: [mockCancelApp, mockRestoreApp],
},
},
decorators: [
(Story) => (
<AuthContext.Provider
Expand All @@ -30,10 +74,12 @@ export default meta;
type Story = StoryObj<typeof meta>;

/**
* The button that allows the user to cancel an application.
* The button that allows the user to restore an application.
*/
export const Restore: Story = {
export const RestoreCanceled: Story = {
name: "Restore (From Cancelled)",
args: {
onCancel: fn(),
application: {
_id: "mock-id",
status: "Canceled",
Expand Down Expand Up @@ -68,11 +114,111 @@ export const Restore: Story = {
},
};

/**
* The button that allows the user to restore an application.
*/
export const RestoreDeleted: Story = {
name: "Restore (From Deleted)",
args: {
onCancel: fn(),
application: {
_id: "mock-id",
status: "Deleted",
createdAt: "",
updatedAt: "",
submittedDate: "",
history: [],
ORCID: "",
applicant: {
applicantID: "applicant-123",
applicantName: "",
applicantEmail: "",
},
PI: "",
controlledAccess: false,
openAccess: false,
studyAbbreviation: "Mock Study that is deleted",
conditional: false,
pendingConditions: [],
programName: "",
programAbbreviation: "",
programDescription: "",
version: "",
},
},
argTypes: {
application: {
control: {
disable: true,
},
},
},
};

export const RestoreDialog: Story = {
name: "Restore Confirmation Dialog",
args: {
onCancel: fn(),
application: {
_id: "mock-id",
status: "Canceled",
createdAt: "",
updatedAt: "",
submittedDate: "",
history: [],
ORCID: "",
applicant: {
applicantID: "applicant-123",
applicantName: "",
applicantEmail: "",
},
PI: "",
controlledAccess: false,
openAccess: false,
studyAbbreviation: "Mock Study that is canceled",
conditional: false,
pendingConditions: [],
programName: "",
programAbbreviation: "",
programDescription: "",
version: "",
},
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

const button = canvas.getByRole("button", { name: /restore/i });
await userEvent.click(button);

await waitFor(() => {
expect(screen.getByRole("dialog")).toBeInTheDocument();
});

const reasonInput = within(screen.getByRole("dialog")).queryByTestId(
"cancel-restore-application-reason"
);

await userEvent.type(reasonInput, "lorem ipsum dol excel ".repeat(10));

await waitFor(() => {
expect(screen.getByRole("button", { name: /confirm/i })).toBeEnabled();
});
},
argTypes: {
application: {
control: {
disable: true,
},
},
},
};

/**
* The button that's visible when the application is in a state that allows restoration.
*/
export const Cancel: Story = {
args: {
onCancel: fn(),
application: {
_id: "mock-id",
status: "In Progress",
Expand Down Expand Up @@ -106,3 +252,61 @@ export const Cancel: Story = {
},
},
};

export const CancelConfirmDialog: Story = {
name: "Cancel Confirmation Dialog",
args: {
onCancel: fn(),
application: {
_id: "mock-id",
status: "In Progress",
createdAt: "",
updatedAt: "",
submittedDate: "",
history: [],
ORCID: "",
applicant: {
applicantID: "applicant-123",
applicantName: "",
applicantEmail: "",
},
PI: "",
controlledAccess: false,
openAccess: false,
studyAbbreviation: "Mock Study that is in progress",
conditional: false,
pendingConditions: [],
programName: "",
programAbbreviation: "",
programDescription: "",
version: "",
},
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

const button = canvas.getByRole("button", { name: /cancel/i });
await userEvent.click(button);

await waitFor(() => {
expect(screen.getByRole("dialog")).toBeInTheDocument();
});

const reasonInput = within(screen.getByRole("dialog")).queryByTestId(
"cancel-restore-application-reason"
);

await userEvent.type(reasonInput, "lorem ipsum dol excel ".repeat(10));

await waitFor(() => {
expect(screen.getByRole("button", { name: /confirm/i })).toBeEnabled();
});
},
argTypes: {
application: {
control: {
disable: true,
},
},
},
};
93 changes: 93 additions & 0 deletions src/components/CancelApplicationButton/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -283,6 +287,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -337,6 +345,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -393,6 +405,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -438,6 +454,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -486,6 +506,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -540,6 +564,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -596,6 +624,10 @@ describe("Basic Functionality", () => {
// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

// Enter reason for action
const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "mock reason");

// Click dialog confirm button
const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });
userEvent.click(button);
Expand Down Expand Up @@ -938,4 +970,65 @@ describe("Implementation Requirements", () => {

expect(getByTestId("delete-dialog-description")).toHaveTextContent("Study: NA");
});

it("should require a reason for canceling", async () => {
const mockMatcher = jest.fn().mockImplementation(() => true);
const mocks: MockedResponse<CancelAppResp, CancelAppInput>[] = [
{
request: {
query: CANCEL_APP,
},
variableMatcher: mockMatcher,
result: {
data: {
cancelApplication: {
_id: "some id",
},
},
},
},
];

const { getByRole, getByTestId } = render(
<Button
application={{
...baseApp,
_id: "mock-id-cancel-reason",
status: "New",
applicant: { ...baseApp.applicant, applicantID: "owner" },
}}
/>,
{
wrapper: ({ children }) => (
<TestParent
mocks={mocks}
user={{ ...baseUser, _id: "owner", permissions: ["submission_request:cancel"] }}
>
{children}
</TestParent>
),
}
);

// Open confirmation dialog
userEvent.click(getByTestId("cancel-restore-application-button"));

const button = await within(getByRole("dialog")).findByRole("button", { name: /confirm/i });

expect(button).toBeDisabled();

const input = await within(getByRole("dialog")).findByRole("textbox");
userEvent.type(input, "this is a mock reason xyz 123");

expect(button).toBeEnabled();

userEvent.click(button);

await waitFor(() => {
expect(mockMatcher).toHaveBeenCalledWith({
_id: "mock-id-cancel-reason",
reviewComments: "this is a mock reason xyz 123",
});
});
});
});
Loading