From 802bc5595b97257929f470d377fa4f04cd6f64fb Mon Sep 17 00:00:00 2001 From: KryptXBSA Date: Mon, 2 Dec 2024 03:22:50 +0300 Subject: [PATCH] tests not finished --- apps/web/src/app/test/page.tsx | 2 +- packages/core/src/codegen/generate-code.ts | 2 +- packages/core/test/codegen.test.ts | 41 ++- packages/core/test/forms/form1.ts | 0 packages/core/test/mock.ts | 326 --------------------- 5 files changed, 35 insertions(+), 336 deletions(-) create mode 100644 packages/core/test/forms/form1.ts delete mode 100644 packages/core/test/mock.ts diff --git a/apps/web/src/app/test/page.tsx b/apps/web/src/app/test/page.tsx index 5a92aa5..1f9e703 100644 --- a/apps/web/src/app/test/page.tsx +++ b/apps/web/src/app/test/page.tsx @@ -52,7 +52,7 @@ const language = [ { label: "Arabic", value: "ar" }, { label: "Kurdish", value: "ku" }, ] -export function MyForm() { +export default function MyForm() { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { diff --git a/packages/core/src/codegen/generate-code.ts b/packages/core/src/codegen/generate-code.ts index f74c789..97a5e81 100644 --- a/packages/core/src/codegen/generate-code.ts +++ b/packages/core/src/codegen/generate-code.ts @@ -1,4 +1,4 @@ -import type { FormField, FormSchema } from "@/schema"; +import type { FormSchema } from "@/schema"; import Handlebars from "handlebars"; import { generateImports } from "./imports/index"; diff --git a/packages/core/test/codegen.test.ts b/packages/core/test/codegen.test.ts index f0b0499..17d7d11 100644 --- a/packages/core/test/codegen.test.ts +++ b/packages/core/test/codegen.test.ts @@ -1,14 +1,39 @@ import { expect, test } from "bun:test"; import { generateCode } from "../src/codegen/generate-code"; import { mockFields } from "../src/mock/mockFields"; -import { toBe } from "./mock"; +import { + mainTemplate, + stringInputTemplate, + numberInputTemplate, + booleanInputTemplate, + dateInputTemplate, + selectInputTemplate, + radioInputTemplate, + comboboxInputTemplate, + textareaInputTemplate, + } from "../src/codegen/templates"; +import type { FormSchema } from "@/schema"; test("test generate code", () => { - const code = generateCode({ - name: "test", - fields: mockFields, - }); - expect(code).toBeDefined(); - Bun.write("./apps/web/src/app/test/page.tsx", code); - // expect(code).toBe(toBe); + const form: FormSchema = { + name: "TestForm", + fields: [ + { + id: "1", + label: "Name", + key: "name", + kind: "string", + required: true, + }, + { + id: "2", + label: "Age", + key: "age", + kind: "number", + required: false, + }, + ], + }; + const generatedCode = generateCode(form); + expect(generatedCode).toBe(); }); diff --git a/packages/core/test/forms/form1.ts b/packages/core/test/forms/form1.ts new file mode 100644 index 0000000..e69de29 diff --git a/packages/core/test/mock.ts b/packages/core/test/mock.ts deleted file mode 100644 index 56e4951..0000000 --- a/packages/core/test/mock.ts +++ /dev/null @@ -1,326 +0,0 @@ -export const toBe = ` -'use client' -import { zodResolver } from "@hookform/resolvers/zod" -import * as z from "zod" -import { useForm } from "react-hook-form" -import { Button } from "@/components/ui/button" -import { - Form, - FormControl, - FormDescription, - FormField, - FormItem, - FormLabel, - FormMessage, -} from "@/components/ui/form" -import { cn } from "@/lib/utils" -import { Input } from "@/components/ui/input" - -import { format } from "date-fns" -import { CalendarIcon } from "lucide-react" -import { Calendar } from "@/components/ui/calendar" - -import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" - -import { Check, ChevronsUpDown } from "lucide-react" -import { - Command, - CommandEmpty, - CommandGroup, - CommandInput, - CommandItem, -} from "@/components/ui/command" -import { - Popover, - PopoverContent, - PopoverTrigger, -} from "@/components/ui/popover" - -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue, -} from "@/components/ui/select" -const formSchema = z.object({"username":z.string().min(1).max(255),"myNumber":z.coerce.number().gte(1).lte(9999),"email":z.string().email().min(1).max(255),"securityEmails":z.boolean(),"dateOfBirth":z.date(),"notify":z.string(),"language":z.string(),"languageSelect":z.string()}) - -const language = [ - { label: "English", value: "en" }, - { label: "Arabic", value: "ar" }, - { label: "Kurdish", value: "ku" }, -] -export function MyForm() { - const form = useForm>({ - resolver: zodResolver(formSchema), - defaultValues: { -username: "", -myNumber: 1, -email: "", -}, - }) - - function onSubmit(values: z.infer) { - console.log(values) - } - - return ( -
- - - ( - - Username - - - - - - - - - )} - /> - - ( - - Number - - - - - - - - - )} - /> - - ( - - Email - - - - - - - - - )} - /> - - ( - -
- Security emails - - Receive emails about your account security. - -
- - - -
- )} - /> - - ( - - Date of birth - - - - - - - - - // date > new Date() || date < new Date("1900-01-01") - // } - initialFocus - /> - - - - Your date of birth is used to calculate your age. - - - - )} - /> - - ( - - Notify me about - - - - - - - - All new messages - - - - - - - - Direct messages and mentions - - - - - - - - Nothing - - - - - - - - - - )} - /> - - ( - - Language - - - - - - - - - - No language found. - - { language.map((item) => ( - { - form.setValue("language", item.value) - }} - > - - {item.label} - - ))} - - - - - - This is the language that will be used in the dashboard. - - - - )} - /> - - ( - - Language - - - Select a language - - - - )} - /> - - - ) -} - -`;