Skip to content

Commit

Permalink
fix(ts): fix ts errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirojose committed Apr 28, 2024
1 parent 221d30e commit 6d65bd7
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
import { withConditional } from "../fields";
import { SelectFieldProps } from "./selects/SelectField";

export interface MultiSelectCheckboxesField extends SelectFieldProps {
export interface MultiSelectCheckboxesFieldProps extends SelectFieldProps {
type: "multi_select_checkbox";
}

export const MultiSelectCheckboxes =
withConditional<MultiSelectCheckboxesField>(({ form, field }) => (
withConditional<MultiSelectCheckboxesFieldProps>(({ form, field }) => (
<FormField
control={form.control}
name={field.name}
Expand Down
10 changes: 7 additions & 3 deletions tests/components/FormBuilder/DatePickerInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { vi, describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { DatePickerInput } from "#/components";
import { DatePickerInput, DatePickerInputProps } from "#/components";

describe("DatePickerInput", () => {
const mockDate = new Date(2022, 0, 1);
const field = {
const field: DatePickerInputProps = {
type: "date",
name: "test",
defaultValue: "",
value: "",
};

it("renders a date picker input", () => {
Expand All @@ -20,7 +21,10 @@ describe("DatePickerInput", () => {

it("displays the selected date in the correct format", () => {
vi.setSystemTime(mockDate);
renderFormField(DatePickerInput, { ...field, defaultValue: mockDate });
renderFormField(DatePickerInput, {
...field,
defaultValue: mockDate.toDateString(),
});
const buttonElement = screen.getByRole("button");
expect(buttonElement).toHaveTextContent("Jan 1, 2022");
});
Expand Down
18 changes: 14 additions & 4 deletions tests/components/FormBuilder/FieldArray.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { FieldArray } from "#/components";
import { FieldArray, FieldArrayFieldProps } from "#/components";

describe("FieldArray", () => {
it("renders an 'Add' button", () => {
const field = {
const field: FieldArrayFieldProps = {
type: "field_array",
name: "test",
value: "",
defaultValues: { name: "" },
fields: [],
hasSequence: false,
};
Expand All @@ -18,12 +20,20 @@ describe("FieldArray", () => {
});

it("adds a new field when the 'Add' button is clicked", () => {
const field = {
const field: FieldArrayFieldProps = {
type: "field_array",
name: "test",
value: "",
label: "Test",
defaultValues: { name: "" },
fields: [{ type: "input", name: "name" }],
fields: [
{
type: "input",
name: "name",
label: "Name",
value: "",
},
],
hasSequence: false,
};
const { form } = renderFormField(FieldArray, field);
Expand Down
9 changes: 7 additions & 2 deletions tests/components/FormBuilder/FileUploadField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { describe, it, expect } from "vitest";
import { screen } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { FileUploadField } from "#/components";
import { FileUploadField, FileUploadFieldProps } from "#/components";

describe("FileUploadField", () => {
it("renders an upload button", () => {
const field = { type: "file", name: "test", mode: "file" };
const field: FileUploadFieldProps = {
type: "file",
name: "test",
mode: "file",
value: "",
};

renderFormField(FileUploadField, field);

Expand Down
19 changes: 8 additions & 11 deletions tests/components/FormBuilder/HiddenField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { describe, it, expect } from "vitest";
import { screen } from "@testing-library/react";
import { HiddenField } from "#/components";
import { BaseField, HiddenField } from "#/components";
import { renderFormField } from "../../../tests/helpers/renderFormField";

describe("HiddenField", () => {
const field: BaseField = {
type: "hidden",
name: "test",
value: "hiddenValue",
};
it("renders a hidden input field", () => {
const { form } = renderFormField(HiddenField, {
type: "hidden",
name: "test",
value: "hiddenValue",
});
const { form } = renderFormField(HiddenField, field);
const hiddenInput = form.getValues("test");
expect(hiddenInput).toBe("hiddenValue");
});

it("does not render any visible elements", () => {
renderFormField(HiddenField, {
type: "hidden",
name: "test",
value: "hiddenValue",
});
renderFormField(HiddenField, field);
const hiddenInput = screen.queryByRole("textbox");
expect(hiddenInput).not.toBeInTheDocument();
});
Expand Down
9 changes: 7 additions & 2 deletions tests/components/FormBuilder/InputField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { InputField } from "#/components";
import { InputField, InputFieldProps } from "#/components";

describe("InputField", () => {
const field = { type: "input", name: "test", value: "" };
const field: InputFieldProps = {
type: "input",
name: "test",
value: "",
mode: "text",
};
it("renders an input element", () => {
renderFormField(InputField, field);
const inputElement = screen.getByRole("textbox");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { MultiSelectCheckboxes } from "#/components";
import {
MultiSelectCheckboxes,
MultiSelectCheckboxesFieldProps,
} from "#/components";

describe("MultiSelectCheckboxes", () => {
const field = {
const field: MultiSelectCheckboxesFieldProps = {
type: "multi_select_checkbox",
name: "test",
value: "",
options: [
{ value: "option1", label: "Option 1" },
{ value: "option2", label: "Option 2" },
Expand Down
17 changes: 12 additions & 5 deletions tests/components/FormBuilder/RadioGroupField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { RadioGroupField } from "#/components";
import { RadioGroupField, RadioGroupFieldProps } from "#/components";

describe("RadioGroupField", () => {
const field = {
const field: RadioGroupFieldProps = {
type: "radio_group",
name: "test",
options: [
{ value: "option1", label: "Option 1" },
{ value: "option2", label: "Option 2" },
value: "",
sections: [
{
label: "Test",
description: "Test description",
options: [
{ value: "option1", label: "Option 1" },
{ value: "option2", label: "Option 2" },
],
},
],
};

Expand Down
8 changes: 6 additions & 2 deletions tests/components/FormBuilder/RichTextEditorField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { describe, it, expect, vi } from "vitest";
import { screen, act } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { RichTextEditorField } from "#/components";
import { RichTextEditorField, RichTextEditorFieldProps } from "#/components";

describe("RichTextEditorField", () => {
const field = { type: "wysiwyg", name: "test" };
const field: RichTextEditorFieldProps = {
type: "wysiwyg",
name: "test",
value: "",
};

it("renders a rich text editor", async () => {
const { container } = renderFormField(RichTextEditorField, field);
Expand Down
4 changes: 2 additions & 2 deletions tests/components/FormBuilder/SwitchField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { SwitchField } from "#/components";
import { SwitchField, SwitchFieldProps } from "#/components";

describe("SwitchField", () => {
const field = { type: "switch", name: "test" };
const field: SwitchFieldProps = { type: "switch", name: "test", value: "" };

it("renders a switch element", () => {
renderFormField(SwitchField, field);
Expand Down
8 changes: 6 additions & 2 deletions tests/components/FormBuilder/TextAreaField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { describe, it, expect } from "vitest";
import { screen, fireEvent } from "@testing-library/react";
import { renderFormField } from "tests/helpers/renderFormField";
import { TextAreaField } from "#/components";
import { TextAreaField, TextAreaFieldProps } from "#/components";

describe("TextAreaField", () => {
const field = { type: "textarea", name: "test" };
const field: TextAreaFieldProps = {
type: "textarea",
name: "test",
value: "",
};

it("renders a textarea element", () => {
renderFormField(TextAreaField, field);
Expand Down
12 changes: 8 additions & 4 deletions tests/helpers/renderFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { useForm, FormProvider, UseFormProps } from "react-hook-form";
import { CommonFieldProps, FormFieldProps } from "#/components";

export const renderFormField = (
FieldComponent: React.ComponentType<CommonFieldProps<FormFieldProps>>,
field: FormFieldProps,
formProps: UseFormProps
FieldComponent: unknown,
field: CommonFieldProps<FormFieldProps>["field"],
formProps?: UseFormProps
): {
form: CommonFieldProps<FormFieldProps>["form"];
} & RenderResult => {
const { result } = renderHook(() => useForm(formProps));
const form = result.current;

const Component = FieldComponent as React.ElementType<
CommonFieldProps<FormFieldProps>
>;

return {
form,
...render(
<FormProvider {...form}>
<FieldComponent form={form} field={field} />
<Component form={form} field={field} />
</FormProvider>
),
};
Expand Down

0 comments on commit 6d65bd7

Please sign in to comment.