From 6f5d9dc41ff2fd8353659afeb34806e0174c8231 Mon Sep 17 00:00:00 2001 From: "GitStart-Cal.com" <121884634+gitstart-calcom@users.noreply.github.com> Date: Fri, 25 Aug 2023 20:18:53 +0800 Subject: [PATCH] test: Create unit tests for react components in packages/ui/components/form/select (#10459) Co-authored-by: gitstart-calcom Co-authored-by: Keith Williams --- .../embed-core/playwright/lib/testUtils.ts | 1 + packages/ui/components/form/select/Select.tsx | 2 +- .../ui/components/form/select/select.test.tsx | 308 ++++++++++++++++++ packages/ui/components/test-setup.ts | 8 + 4 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 packages/ui/components/form/select/select.test.tsx diff --git a/packages/embeds/embed-core/playwright/lib/testUtils.ts b/packages/embeds/embed-core/playwright/lib/testUtils.ts index e1afa3aa49..5f237ed99a 100644 --- a/packages/embeds/embed-core/playwright/lib/testUtils.ts +++ b/packages/embeds/embed-core/playwright/lib/testUtils.ts @@ -1,6 +1,7 @@ import type { Page, Frame } from "@playwright/test"; import { test, expect } from "@playwright/test"; +// eslint-disable-next-line no-restricted-imports import prisma from "@calcom/prisma"; export function todo(title: string) { diff --git a/packages/ui/components/form/select/Select.tsx b/packages/ui/components/form/select/Select.tsx index e854f0cc9b..3a3cbe84f1 100644 --- a/packages/ui/components/form/select/Select.tsx +++ b/packages/ui/components/form/select/Select.tsx @@ -141,7 +141,7 @@ export function SelectWithValidation< }: SelectProps & { required?: boolean }) { const [hiddenInputValue, _setHiddenInputValue] = React.useState(() => { if (value instanceof Array || !value) { - return; + return ""; } return value.value || ""; }); diff --git a/packages/ui/components/form/select/select.test.tsx b/packages/ui/components/form/select/select.test.tsx new file mode 100644 index 0000000000..85008c8014 --- /dev/null +++ b/packages/ui/components/form/select/select.test.tsx @@ -0,0 +1,308 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +/* eslint-disable playwright/missing-playwright-await */ +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; +import { vi } from "vitest"; + +import { Select, SelectField, SelectWithValidation } from "./Select"; + +const options = [ + { label: "Option 1", value: "option1" }, + { label: "Option 2", value: "option2" }, + { label: "Option 3", value: "option3" }, +]; + +const onChangeMock = vi.fn(); + +const props: any = { + name: "test", + options: options, + defaultValue: { label: "Option 3", value: "option3" }, + onChange: onChangeMock, +}; + +const classNames = { + singleValue: () => "w-1", + valueContainer: () => "w-2", + control: () => "w-3", + input: () => "w-4", + option: () => "w-5", + menuList: () => "w-6", + menu: () => "w-7", + multiValue: () => "w-8", +}; + +const renderSelectWithForm = (newProps?: any) => { + render( +
+ +