Skip to content

Commit

Permalink
add tests for TileGroup component
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer6497 committed Dec 20, 2024
1 parent 8545071 commit 142ad0b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/components/form/__test__/ValidatedFlowForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "tests/factories/cmsModels/strapiInputComponent";
import { getStrapiSelectComponent } from "tests/factories/cmsModels/strapiSelectComponent";
import { getStrapiTextareaComponent } from "tests/factories/cmsModels/strapiTextareaComponent";
import { getStrapiTileGroupComponent } from "tests/factories/cmsModels/strapiTileGroupComponent";
import ValidatedFlowForm from "~/components/form/ValidatedFlowForm";
import type { StrapiFormComponent } from "~/services/cms/models/StrapiFormComponent";
import * as buildStepValidator from "~/services/validation/buildStepValidator";
Expand Down Expand Up @@ -363,6 +364,42 @@ describe("ValidatedFlowForm", () => {
});
});
});

describe("TileGroup Component", () => {
beforeAll(() => {
fieldNameValidatorSpy.mockImplementation(() =>
withZod(
z.object({
myTileGroup: z.enum(["tile1"], customRequiredErrorMessage),
}),
),
);
});
const { component, expectTileGroupErrorToExist } =
getStrapiTileGroupComponent({
code: "required",
text: "Selection required.",
});

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

const nextButton = getByText("NEXT");
expect(nextButton).toBeInTheDocument();
fireEvent.click(nextButton);
await expectTileGroupErrorToExist();
});

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

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

export function getStrapiTileGroupComponent(
errorCode: z.infer<typeof StrapiFieldErrorSchema>,
): {
component: Partial<z.infer<typeof StrapiTileGroupComponentSchema>>;
expectTileGroupErrorToExist: () => Promise<void>;
} {
return {
component: {
__component: "form-elements.tile-group",
name: "myTileGroup",
options: [
{
title: "Tile 1",
value: "tile1",
description: null,
tagDescription: null,
},
],
errors: [
{
name: "",
id: 0,
errorCodes: [errorCode],
},
],
},
expectTileGroupErrorToExist: async function () {
await waitFor(() => {
expect(screen.getByText(errorCode.text)).toBeInTheDocument();
expect(screen.getByTestId("inputError")).toBeInTheDocument();
expect(screen.getByTestId("ErrorOutlineIcon")).toBeInTheDocument();
});
},
};
}

0 comments on commit 142ad0b

Please sign in to comment.