From 50b27b7cb141df45543f0bff0f328bbd5cf0e0be Mon Sep 17 00:00:00 2001 From: Karmveer Singh Date: Wed, 24 Jul 2024 22:37:48 +1200 Subject: [PATCH] Passed tests by adding labels and for --- web/__test__/components/FormInput.test.tsx | 2 +- .../components/UserInformationForm.test.tsx | 22 ++-- web/src/components/FormInput.tsx | 52 +++++--- web/src/components/UserInformationForm.tsx | 116 +++++------------- 4 files changed, 74 insertions(+), 118 deletions(-) diff --git a/web/__test__/components/FormInput.test.tsx b/web/__test__/components/FormInput.test.tsx index 2f711eaa..900c8a2c 100644 --- a/web/__test__/components/FormInput.test.tsx +++ b/web/__test__/components/FormInput.test.tsx @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { render, screen, fireEvent } from "@testing-library/react"; import FormInput from "../../src/components/FormInput"; import React from "react"; diff --git a/web/__test__/components/UserInformationForm.test.tsx b/web/__test__/components/UserInformationForm.test.tsx index a9c98ac8..53571ac5 100644 --- a/web/__test__/components/UserInformationForm.test.tsx +++ b/web/__test__/components/UserInformationForm.test.tsx @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { render, screen, fireEvent } from "@testing-library/react"; import UserInformationForm from "../../src/components/UserInformationForm"; import React from "react"; @@ -6,25 +6,19 @@ import React from "react"; describe("UserInformationForm Component", () => { it("renders the form", () => { render(); - expect(screen.getByText(/Thanks for Joining/i)).toBeInTheDocument(); + expect(screen.getByText(/Thanks for joining/i)).toBeInTheDocument(); }); - it("validates form fields", () => { + it("validates form fields", async () => { render(); const submitButton = screen.getByText(/Purchase membership/i); fireEvent.click(submitButton); - expect(screen.getByText(/First Name is Required/i)).toBeInTheDocument(); - expect(screen.getByText(/Last Name is Required/i)).toBeInTheDocument(); - expect( - screen.getByText(/Your University is Required/i) - ).toBeInTheDocument(); - expect( - screen.getByText(/Your Graduation Year is Required/i) - ).toBeInTheDocument(); - expect( - screen.getByText(/Membership Type is Required/i) - ).toBeInTheDocument(); + expect(await screen.findByText(/First Name is Required/i)).toBeInTheDocument(); + expect(await screen.findByText(/Last Name is Required/i)).toBeInTheDocument(); + expect(await screen.findByText(/Your University is Required/i)).toBeInTheDocument(); + expect(await screen.findByText(/Your Graduation Year is Required/i)).toBeInTheDocument(); + expect(await screen.findByText(/Membership Type is Required/i)).toBeInTheDocument(); }); }); diff --git a/web/src/components/FormInput.tsx b/web/src/components/FormInput.tsx index 38dd5688..212461ec 100644 --- a/web/src/components/FormInput.tsx +++ b/web/src/components/FormInput.tsx @@ -25,24 +25,40 @@ const FormInput = forwardRef( }, ref ) => { - return ( -
- {label && ( - - )} - - {errorMessage && {errorMessage}} -
- ); + if (type === 'radio' && options.length > 0) { + return ( +
+
+ {options.map((option, index) => ( + + ))} +
+ {errorMessage && {errorMessage}} +
+ ); + } else { + return ( +
+ {label && ( + + )} + + {errorMessage && {errorMessage}} +
+ ); + } } ); diff --git a/web/src/components/UserInformationForm.tsx b/web/src/components/UserInformationForm.tsx index fb9083e9..4b06d01e 100644 --- a/web/src/components/UserInformationForm.tsx +++ b/web/src/components/UserInformationForm.tsx @@ -1,24 +1,19 @@ -import React from "react"; -import { useForm, Controller } from "react-hook-form"; -import axios from "axios"; -import FormInput from "../components/FormInput"; +import { useForm, Controller } from 'react-hook-form'; +import axios from 'axios'; +import FormInput from '../components/FormInput'; function UserInformationForm() { - const { - handleSubmit, - control, - formState: { errors }, - } = useForm({ + const { handleSubmit, control, formState: { errors } } = useForm({ defaultValues: { - firstName: "", - lastName: "", - university: "", - graduationYear: "", - UPI: "", - studentID: "", - studyOption: "", - residency: "", - duration: "", + firstName: '', + lastName: '', + university: '', + graduationYear: '', + UPI: '', + studentID: '', + studyOption: '', + residency: '', + duration: '', }, }); @@ -54,10 +49,7 @@ function UserInformationForm() { We just need a bit more info about your membershipβ€”it'll be quick! -
+
@@ -65,13 +57,9 @@ function UserInformationForm() { name="firstName" control={control} rules={{ required: "First Name is Required" }} - render={({ field }) => ( - - )} + render={({ field }) => } /> - {errors.firstName && ( - {errors.firstName.message} - )} + {errors.firstName && {errors.firstName.message}}
@@ -79,13 +67,9 @@ function UserInformationForm() { name="lastName" control={control} rules={{ required: "Last Name is Required" }} - render={({ field }) => ( - - )} + render={({ field }) => } /> - {errors.lastName && ( - {errors.lastName.message} - )} + {errors.lastName && {errors.lastName.message}}
@@ -96,50 +80,29 @@ function UserInformationForm() { name="university" control={control} rules={{ required: "Your University is Required" }} - render={({ field }) => ( - - )} + render={({ field }) => } /> - {errors.university && ( - - {errors.university.message} - - )} + {errors.university && {errors.university.message}}
- + ( - - )} + render={({ field }) => } /> - {errors.graduationYear && ( - - {errors.graduationYear.message} - - )} + {errors.graduationYear && {errors.graduationYear.message}}
- + ( - - )} + render={({ field }) => } />
@@ -147,9 +110,7 @@ function UserInformationForm() { ( - - )} + render={({ field }) => } />
@@ -160,28 +121,18 @@ function UserInformationForm() { ( - - )} + render={({ field }) => } />
- + (
{residencyOptions.map((option, index) => ( -