From 381896b05627d91006aac062e77e20921e971925 Mon Sep 17 00:00:00 2001 From: Mirjam Aulbach Date: Thu, 27 Oct 2022 18:24:29 +0200 Subject: [PATCH] feat: Add tests for Form based on aiven-core. --- coral/src/app/components/Form.test.tsx | 154 ++++++++++++++++++ .../__snapshots__/Form.test.tsx.snap | 43 +++++ 2 files changed, 197 insertions(+) create mode 100644 coral/src/app/components/Form.test.tsx create mode 100644 coral/src/app/components/__snapshots__/Form.test.tsx.snap diff --git a/coral/src/app/components/Form.test.tsx b/coral/src/app/components/Form.test.tsx new file mode 100644 index 0000000000..0fb97f503c --- /dev/null +++ b/coral/src/app/components/Form.test.tsx @@ -0,0 +1,154 @@ +// ❗️ This implementation mirrors the Aiven core and is kept up to date with it. +// The Aiven core solution will be its own open source package soon. +// we're mirroring the implementation as it is, using only the components we actively use in coral. +// ❗️The Aiven core code base is the source of truth for this component. +import { Button } from "@aivenio/design-system"; +import type { RenderResult } from "@testing-library/react"; +import { cleanup, render, screen, waitFor } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import React from "react"; +import type { DeepPartial, FieldValues } from "react-hook-form"; +import { + Form, + SubmitErrorHandler, + SubmitHandler, + TextInput, + useForm, +} from "src/app/components/Form"; +import { z } from "zod"; + +type WrapperProps = { + schema: any; + defaultValues?: DeepPartial; + onSubmit: SubmitHandler; + onError: SubmitErrorHandler; +}; + +const Wrapper = ({ + schema, + defaultValues, + onSubmit, + onError, + children, +}: React.PropsWithChildren>): React.ReactElement => { + const form = useForm({ schema, defaultValues }); + return ( +
+ {children} +
+ ); +}; + +describe("Form", () => { + const onSubmit = jest.fn(); + const onError = jest.fn(); + let results: RenderResult; + let user: ReturnType; + + beforeEach(() => { + user = userEvent.setup(); + }); + + afterEach(() => { + cleanup(); + onSubmit.mockClear(); + onError.mockClear(); + }); + + const renderForm = ( + children: React.ReactNode, + { schema, defaultValues }: { schema: any; defaultValues?: DeepPartial } + ) => { + return render( + + schema={schema} + defaultValues={defaultValues} + onSubmit={onSubmit} + onError={onError} + > + {children} +