Skip to content

Commit

Permalink
add tests for Checkbox component
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer6497 committed Dec 20, 2024
1 parent b591049 commit 8545071
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/components/form/__test__/ValidatedFlowForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { withZod } from "@remix-validated-form/with-zod";
import { fireEvent, render, waitFor } from "@testing-library/react";
import { RouterProvider, createMemoryRouter } from "react-router-dom";
import { z } from "zod";
import { getStrapiCheckboxComponent } from "tests/factories/cmsModels/strapiCheckboxComponent";
import { getStrapiDropdownComponent } from "tests/factories/cmsModels/strapiDropdownComponent";
import {
getStrapiInputComponent,
Expand All @@ -12,6 +13,7 @@ import { getStrapiTextareaComponent } from "tests/factories/cmsModels/strapiText
import ValidatedFlowForm from "~/components/form/ValidatedFlowForm";
import type { StrapiFormComponent } from "~/services/cms/models/StrapiFormComponent";
import * as buildStepValidator from "~/services/validation/buildStepValidator";
import { checkedRequired } from "~/services/validation/checkedCheckbox";
import { createDateSchema } from "~/services/validation/date";
import { integerSchema } from "~/services/validation/integer";
import { stringRequiredSchema } from "~/services/validation/stringRequired";
Expand Down Expand Up @@ -322,6 +324,45 @@ describe("ValidatedFlowForm", () => {
});
});
});

describe("Checkbox Component", () => {
beforeAll(() => {
fieldNameValidatorSpy.mockImplementation(() =>
withZod(
z.object({
myCheckbox: checkedRequired,
}),
),
);
});
const { component, expectCheckboxErrorToExist } =
getStrapiCheckboxComponent({
code: "required",
text: "Selection required.",
});

it("should display an error if the user doesn't select the checkbox", async () => {
const { getByText, getByLabelText } = renderValidatedFlowForm([
component,
]);

const nextButton = getByText("NEXT");
expect(nextButton).toBeInTheDocument();
fireEvent.blur(getByLabelText("Checkbox"));
await expectCheckboxErrorToExist();
});

it("should not display an error if the user has selected the checkbox", async () => {
const { getByText, queryByTestId, getByLabelText } =
renderValidatedFlowForm([component]);
fireEvent.click(getByLabelText("Checkbox"));
fireEvent.click(getByText("NEXT"));
await waitFor(() => {
expect(queryByTestId("inputError")).not.toBeInTheDocument();
expect(queryByTestId("ErrorOutlineIcon")).not.toBeInTheDocument();
});
});
});
});

function renderValidatedFlowForm(formElements: Partial<StrapiFormComponent>[]) {
Expand Down
31 changes: 31 additions & 0 deletions tests/factories/cmsModels/strapiCheckboxComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { waitFor, screen } from "@testing-library/react";
import type { z } from "zod";
import type { StrapiCheckboxComponentSchema } from "~/services/cms/components/StrapiCheckbox";
import type { StrapiFieldErrorSchema } from "~/services/cms/models/StrapiFieldError";

export function getStrapiCheckboxComponent(
errorCode: z.infer<typeof StrapiFieldErrorSchema>,
): {
component: Partial<z.infer<typeof StrapiCheckboxComponentSchema>>;
expectCheckboxErrorToExist: () => Promise<void>;
} {
return {
component: {
__component: "form-elements.checkbox",
name: "myCheckbox",
label: "Checkbox",
isRequiredError: {
name: "",
id: 0,
errorCodes: [errorCode],
},
},
expectCheckboxErrorToExist: async function () {
await waitFor(() => {
expect(screen.getByText(errorCode.text)).toBeInTheDocument();
expect(screen.getByTestId("inputError")).toBeInTheDocument();
expect(screen.getByTestId("ErrorOutlineIcon")).toBeInTheDocument();
});
},
};
}

0 comments on commit 8545071

Please sign in to comment.