Skip to content

Commit

Permalink
🧪 test(form-actions): improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Feb 16, 2024
1 parent e6051a4 commit 8bc3507
Show file tree
Hide file tree
Showing 24 changed files with 262 additions and 225 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/_/api-handlers/form-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export const formActionsApiHandlers = [
}),
rest.post(BASE_TEST_URL("/api/form-actions"), async (req, res, ctx) => {
const newFormAction = await req.json();
FORM_ACTIONS.push(newFormAction);
FORM_ACTIONS.push({ id: "id", ...newFormAction });
if (
JSON.stringify(newFormAction) ===
'{"configuration":{"channel":"{ CONSTANTS.SLACK_CHANNEL }}","message":"Hello how are youHello how are you","shouldNotify":true},"entity":"test-entity","trigger":"create","integration":"slack","action":"SEND_MESSAGE"}'
'{"configuration":{"channel":"{ CONSTANTS.SLACK_CHANNEL }}","message":"Hello how are you","shouldNotify":true},"entity":"test-entity","trigger":"create","integration":"slack","action":"SEND_MESSAGE"}'
) {
return res(ctx.status(204));
}
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/_/api-handlers/integrations-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ export const integrationsListApiHandlers = [
),
rest.get(
BASE_TEST_URL("/api/integrations/actions/:integration/implementations"),
async (_, res, ctx) => {
async (req, res, ctx) => {
const requestedIntegration = req.params.integration as string;
return res(
ctx.json([
{
key: "SEND_MESSAGE",
label: "Send Message",
label: `Send Message - ${requestedIntegration}`,
configurationSchema: {
channel: {
type: "text",
Expand All @@ -104,7 +105,7 @@ export const integrationsListApiHandlers = [
},
{
key: "SEND_MAIL",
label: "Send Mail",
label: `Send Mail - ${requestedIntegration}`,
configurationSchema: {
from: {
type: "text",
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/_/utils/closeAllToasts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { waitFor, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

export const closeAllToasts = async () => {
const toast = screen.getByRole("button", {
name: "Close Toast",
});

await userEvent.click(toast);

await waitFor(
() => {
expect(toast).not.toBeInTheDocument();
},
{ timeout: 20000 }
);
};
File renamed without changes.
5 changes: 3 additions & 2 deletions src/__tests__/account/password.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import userEvent from "@testing-library/user-event";
import AccountPassword from "pages/account/password";

import { setupApiHandlers } from "__tests__/_/setupApihandlers";
import { closeAllToasts } from "__tests__/_/utils/closeAllToasts";

setupApiHandlers();

Expand Down Expand Up @@ -45,6 +46,8 @@ describe("pages/account/password", () => {
expect(await screen.findByRole("status")).toHaveTextContent(
"Password Updated Successfully"
);

await closeAllToasts();
});

it("should show different error message on demo", async () => {
Expand All @@ -56,8 +59,6 @@ describe("pages/account/password", () => {
</ApplicationRoot>
);

await userEvent.click(screen.getByRole("button", { name: "Close Toast" }));

await userEvent.type(
await screen.findByLabelText("Old Password"),
"Old Password"
Expand Down
183 changes: 131 additions & 52 deletions src/__tests__/admin/[entity]/config/actions.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint-disable testing-library/no-node-access */
/* eslint-disable testing-library/no-container */
import React from "react";
import { render, screen, within } from "@testing-library/react";
import { ApplicationRoot } from "frontend/components/ApplicationRoot";
import userEvent from "@testing-library/user-event";
import EntityFormActionsSettings from "pages/admin/[entity]/config/actions";

import { setupApiHandlers } from "__tests__/_/setupApihandlers";
import { getTableRows } from "__tests__/_/utiis/getTableRows";
import { getTableRows } from "__tests__/_/utils/getTableRows";
import { closeAllToasts } from "__tests__/_/utils/closeAllToasts";

setupApiHandlers();

Expand All @@ -23,7 +22,7 @@ describe("pages/admin/[entity]/config/actions", () => {
}));
});

it.skip("should list entity form actions", async () => {
it("should list entity form actions", async () => {
render(
<ApplicationRoot>
<EntityFormActionsSettings />x
Expand Down Expand Up @@ -89,11 +88,6 @@ describe("pages/admin/[entity]/config/actions", () => {
"Hello how are you"
);

await userEvent.type(
within(dialog).getByLabelText("Slack: Message"),
"Hello how are you"
);

await userEvent.click(screen.getByLabelText("Slack: Should Notify"));

await userEvent.click(
Expand Down Expand Up @@ -124,100 +118,185 @@ describe("pages/admin/[entity]/config/actions", () => {
"SlackCreateSend Message",
]
`);

await closeAllToasts();
});

it("should delete form action successfully", async () => {
it("should show the correct form values", async () => {
render(
<ApplicationRoot>
<EntityFormActionsSettings />
</ApplicationRoot>
);

await userEvent.click(screen.getByRole("button", { name: "Close Toast" }));

expect(await screen.findByRole("table")).toBeInTheDocument();

const tableRows = await screen.findAllByRole("row");

expect(tableRows).toHaveLength(5);
await userEvent.click(
within(tableRows[4]).getByRole("button", {
name: "Edit Form Action",
})
);

const dialog = screen.getByRole("dialog");
//
expect(
within(dialog).getByTestId("react-select__trigger")
).toHaveTextContent("On Create");

expect(
within(dialog).getByRole("option", { selected: true })
).toHaveTextContent("Slack");

expect(
within(dialog).getByTestId("react-select__action")
).toHaveTextContent("Send Message - slack");

expect(await within(dialog).findByLabelText("Slack: Channel")).toHaveValue(
"{ CONSTANTS.SLACK_CHANNEL }}"
);
expect(within(dialog).getByLabelText("Slack: Message")).toHaveValue(
"Hello how are you"
);
expect(within(dialog).getByLabelText("Slack: Should Notify")).toBeChecked();
});

it("should update the actions form", async () => {
render(
<ApplicationRoot>
<EntityFormActionsSettings />
</ApplicationRoot>
);

expect(await screen.findByRole("table")).toBeInTheDocument();

const tableRows = await screen.findAllByRole("row");

await userEvent.click(
within(tableRows[1]).getByRole("button", {
name: "Delete Form Action",
within(tableRows[4]).getByRole("button", {
name: "Edit Form Action",
})
);

const confirmBox = await screen.findByRole("alertdialog", {
name: "Confirm Delete",
});
const dialog = screen.getByRole("dialog");

await userEvent.type(within(dialog).getByLabelText("Trigger"), "On Delete");
await userEvent.keyboard("{Enter}");

await userEvent.click(within(dialog).getByRole("option", { name: "SMTP" }));

await userEvent.type(within(dialog).getByLabelText("Action"), "Send Mail");
await userEvent.keyboard("{Enter}");

await userEvent.type(
await within(dialog).findByLabelText("SMTP: From"),
"{{ CONSTANTS.MAIL_FROM }}"
);

await userEvent.type(
within(dialog).getByLabelText("SMTP: To"),
"[email protected]"
);

await userEvent.click(
await within(confirmBox).findByRole("button", { name: "Confirm" })
within(dialog).getByRole("button", { name: "Update Form Action" })
);

expect(await screen.findByRole("status")).toHaveTextContent(
"Form Action Deleted Successfully"
"Form Action Updated Successfully"
);

expect(await screen.findAllByRole("row")).toHaveLength(4);
await closeAllToasts();

expect(await getTableRows(screen.getByRole("table")))
.toMatchInlineSnapshot(`
[
"Integration
Trigger
Action
Action",
"HttpCreatePost",
"SmtpUpdateSend Mail",
"SlackDeleteSend Message",
"SmtpDeleteSend Mail",
]
`);
});

it.skip("should show the correct value on the update form", async () => {
const { container } = render(
it("should show the correct form values", async () => {
render(
<ApplicationRoot>
<EntityFormActionsSettings />
</ApplicationRoot>
);

await userEvent.click(screen.getByRole("button", { name: "Close Toast" }));

expect(await screen.findByRole("table")).toBeInTheDocument();

const tableRows = await screen.findAllByRole("row");

await userEvent.click(
within(tableRows[1]).getByRole("button", {
within(tableRows[4]).getByRole("button", {
name: "Edit Form Action",
})
);

const dialog = screen.getByRole("dialog");
//
expect(
within(dialog).getByTestId("react-select__trigger")
).toHaveTextContent("On Delete");

const dialog = screen.getByRole("dialog");
expect(
within(dialog).getByRole("option", { selected: true })
).toHaveTextContent("SMTP");

expect(
within(dialog).getByTestId("react-select__action")
).toHaveTextContent("Send Mail - smtp");

expect(await within(dialog).findByLabelText("SMTP: From")).toHaveValue(
"[email protected]"
"{ CONSTANTS.MAIL_FROM }}"
);
expect(within(dialog).getByLabelText("SMTP: To")).toHaveValue(
"[email protected]"
);
});

expect(container.querySelector(`input[name="trigger"]`)).toHaveValue(
"update"
it("should delete form action successfully", async () => {
render(
<ApplicationRoot>
<EntityFormActionsSettings />
</ApplicationRoot>
);

expect(
within(dialog).getByRole("option", { selected: true })
).toHaveTextContent("SMTP");
expect(await screen.findByRole("table")).toBeInTheDocument();

expect(
within(dialog).getByRole("option", { selected: true })
).toHaveTextContent("Send Mail");
});
const tableRows = await screen.findAllByRole("row");

expect(tableRows).toHaveLength(5);

await userEvent.click(
within(tableRows[1]).getByRole("button", {
name: "Delete Form Action",
})
);

const confirmBox = await screen.findByRole("alertdialog", {
name: "Confirm Delete",
});

// it("should display updated diction values", async () => {
// render(
// <ApplicationRoot>
// <EntityDictionSettings />
// </ApplicationRoot>
// );
// await waitFor(() => {
// expect(screen.getByLabelText("Plural")).toHaveValue(
// "Plural entity-1Updated"
// );
// });
// expect(screen.getByLabelText("Singular")).toHaveValue(
// "Singular entity-1Updated"
// );
// });
await userEvent.click(
await within(confirmBox).findByRole("button", { name: "Confirm" })
);

expect(await screen.findByRole("status")).toHaveTextContent(
"Form Action Deleted Successfully"
);

expect(await screen.findAllByRole("row")).toHaveLength(4);
});
});
13 changes: 5 additions & 8 deletions src/__tests__/admin/[entity]/config/form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import userEvent from "@testing-library/user-event";
import { setupApiHandlers } from "__tests__/_/setupApihandlers";

import EntityFormExtensionSettings from "pages/admin/[entity]/config/form";
import { closeAllToasts } from "__tests__/_/utils/closeAllToasts";

setupApiHandlers();

Expand Down Expand Up @@ -73,6 +74,8 @@ describe("pages/admin/[entity]/config/form", () => {
expect(await screen.findByRole("status")).toHaveTextContent(
"Form Scripts Saved Successfully"
);

await closeAllToasts();
});

it("should display updated value", async () => {
Expand Down Expand Up @@ -107,17 +110,15 @@ describe("pages/admin/[entity]/config/form", () => {
"Updated"
);

await userEvent.click(
screen.getByRole("button", { name: "Close Toast" })
);

await userEvent.click(
within(currentTab).getByRole("button", { name: "Save Form Scripts" })
);

expect(await screen.findByRole("status")).toHaveTextContent(
"Expression: •JS-Error: SyntaxError: Unexpected identifier"
);

await closeAllToasts();
});

it("should display previous section value", async () => {
Expand Down Expand Up @@ -149,10 +150,6 @@ describe("pages/admin/[entity]/config/form", () => {

await userEvent.clear(within(currentTab).getByLabelText("Script"));

await userEvent.click(
screen.getByRole("button", { name: "Close Toast" })
);

await userEvent.click(
within(currentTab).getByRole("button", { name: "Save Form Scripts" })
);
Expand Down
Loading

0 comments on commit 8bc3507

Please sign in to comment.