-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Unit Tests for FormBuilderField and BookingFields components #16162
Merged
CarinaWolli
merged 8 commits into
main
from
08-10-formbuilderfield_and_bookingfields_tests
Sep 3, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a7d8dce
Remove use of location from FormBuilder
hariombalhara 455fdd2
Add tests
hariombalhara 7901b2e
FormBuilderField and BookingFields tests
hariombalhara ba5d122
More tests
hariombalhara e8296eb
Remove always true if condition
hariombalhara 1fbbc0d
Merge remote-tracking branch 'origin/main' into 08-10-formbuilderfiel…
hariombalhara 2c23e6b
Merge branch 'main' into 08-10-formbuilderfield_and_bookingfields_tests
hariombalhara e69a9a3
Fix ui import mockig that got broken after the last merge
hariombalhara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
packages/features/bookings/Booker/components/BookEventForm/BookingFields.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { TooltipProvider } from "@radix-ui/react-tooltip"; | ||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import * as React from "react"; | ||
import type { UseFormReturn } from "react-hook-form"; | ||
import { FormProvider, useForm } from "react-hook-form"; | ||
import { expect } from "vitest"; | ||
|
||
import { getBookingFieldsWithSystemFields } from "../../../lib/getBookingFields"; | ||
import { BookingFields } from "./BookingFields"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type FormMethods = UseFormReturn<any>; | ||
|
||
const renderComponent = ({ | ||
props: props, | ||
formDefaultValues, | ||
}: { | ||
props: Parameters<typeof BookingFields>[0]; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
formDefaultValues?: any; | ||
}) => { | ||
let formMethods: UseFormReturn | undefined; | ||
const Wrapper = ({ children }: { children: React.ReactNode }) => { | ||
const form = useForm({ | ||
defaultValues: formDefaultValues, | ||
}); | ||
formMethods = form; | ||
return ( | ||
<TooltipProvider> | ||
<FormProvider {...form}>{children}</FormProvider> | ||
</TooltipProvider> | ||
); | ||
}; | ||
const result = render(<BookingFields {...props} />, { wrapper: Wrapper }); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return { result, formMethods: formMethods! }; | ||
}; | ||
|
||
describe("BookingFields", () => { | ||
it("should correctly render with location fields", () => { | ||
const AttendeePhoneNumberOption = { | ||
label: "attendee_phone_number", | ||
value: "phone", | ||
}; | ||
|
||
const OrganizerLinkOption = { | ||
label: "https://google.com", | ||
value: "link", | ||
}; | ||
|
||
const locations = [ | ||
{ | ||
type: AttendeePhoneNumberOption.value, | ||
}, | ||
{ | ||
link: "https://google.com", | ||
type: OrganizerLinkOption.value, | ||
displayLocationPublicly: true, | ||
}, | ||
]; | ||
const { formMethods } = renderComponent({ | ||
props: { | ||
fields: getBookingFieldsWithSystemFields({ | ||
disableGuests: false, | ||
bookingFields: [], | ||
metadata: null, | ||
workflows: [], | ||
customInputs: [], | ||
}), | ||
locations, | ||
isDynamicGroupBooking: false, | ||
bookingData: null, | ||
}, | ||
formDefaultValues: {}, | ||
}); | ||
|
||
component.fillName({ value: "John Doe" }); | ||
component.fillEmail({ value: "john.doe@example.com" }); | ||
component.fillNotes({ value: "This is a note" }); | ||
expectScenarios.expectNameToBe({ value: "John Doe", formMethods }); | ||
expectScenarios.expectEmailToBe({ value: "john.doe@example.com", formMethods }); | ||
expectScenarios.expectNotesToBe({ value: "This is a note", formMethods }); | ||
|
||
component.fillRadioInputLocation({ label: AttendeePhoneNumberOption.label, inputValue: "+1234567890" }); | ||
expectScenarios.expectLocationToBe({ | ||
formMethods, | ||
label: AttendeePhoneNumberOption.label, | ||
toMatch: { | ||
formattedValue: "+1 (234) 567-890", | ||
value: { optionValue: "+1234567890", value: AttendeePhoneNumberOption.value }, | ||
}, | ||
}); | ||
|
||
component.fillRadioInputLocation({ label: OrganizerLinkOption.label }); | ||
expectScenarios.expectLocationToBe({ | ||
formMethods, | ||
label: OrganizerLinkOption.label, | ||
toMatch: { | ||
formattedValue: "+1 (234) 567-890", | ||
value: { optionValue: "", value: OrganizerLinkOption.value }, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
const component = { | ||
getName: ({ label = "your_name" }: { label?: string } = {}) => | ||
screen.getByRole("textbox", { | ||
name: new RegExp(label), | ||
}) as HTMLInputElement, | ||
getEmail: () => screen.getByRole("textbox", { name: /email/i }) as HTMLInputElement, | ||
getLocationRadioOption: ({ label }: { label: string }) => | ||
screen.getByRole("radio", { name: new RegExp(label) }) as HTMLInputElement, | ||
getLocationRadioInput: ({ placeholder }: { placeholder: string }) => | ||
screen.getByPlaceholderText(placeholder) as HTMLInputElement, | ||
getNotes: () => screen.getByRole("textbox", { name: /additional_notes/i }) as HTMLInputElement, | ||
getGuests: () => screen.getByLabelText("guests"), | ||
fillName: ({ value }: { value: string }) => { | ||
fireEvent.change(component.getName(), { target: { value } }); | ||
}, | ||
fillEmail: ({ value }: { value: string }) => { | ||
fireEvent.change(component.getEmail(), { target: { value } }); | ||
}, | ||
fillRadioInputLocation: ({ label, inputValue }: { label: string; inputValue?: string }) => { | ||
fireEvent.click(component.getLocationRadioOption({ label })); | ||
|
||
if (inputValue) { | ||
let placeholder = label; | ||
if (label === "attendee_phone_number") { | ||
placeholder = "enter_phone_number"; | ||
} else { | ||
// radioInput doesn't have a label, so we need to identify by placeholder | ||
throw new Error("Tell me how to identify the placeholder for this location input"); | ||
} | ||
fireEvent.change(component.getLocationRadioInput({ placeholder }), { | ||
target: { value: inputValue }, | ||
}); | ||
} | ||
}, | ||
fillNotes: ({ value }: { value: string }) => { | ||
fireEvent.change(component.getNotes(), { target: { value } }); | ||
}, | ||
}; | ||
|
||
const expectScenarios = { | ||
expectNameToBe: ({ value, formMethods }: { value: string; formMethods: FormMethods }) => { | ||
expect(component.getName().value).toEqual(value); | ||
expect(formMethods.getValues("responses.name")).toEqual(value); | ||
}, | ||
expectEmailToBe: ({ value, formMethods }: { value: string; formMethods: FormMethods }) => { | ||
expect(component.getEmail().value).toEqual(value); | ||
expect(formMethods.getValues("responses.email")).toEqual(value); | ||
}, | ||
expectLocationToBe: ({ | ||
formMethods, | ||
label, | ||
toMatch: { formattedValue, value }, | ||
}: { | ||
label: string; | ||
toMatch: { | ||
formattedValue?: string; | ||
value: { | ||
optionValue: string; | ||
value: string; | ||
}; | ||
}; | ||
formMethods: FormMethods; | ||
}) => { | ||
expect(component.getLocationRadioOption({ label }).checked).toBe(true); | ||
if (value.optionValue) { | ||
expect(component.getLocationRadioInput({ placeholder: "enter_phone_number" }).value).toEqual( | ||
formattedValue | ||
); | ||
} | ||
expect(formMethods.getValues("responses.location")).toEqual(value); | ||
}, | ||
expectNotesToBe: ({ value, formMethods }: { value: string; formMethods: FormMethods }) => { | ||
expect(component.getNotes().value).toEqual(value); | ||
expect(formMethods.getValues("responses.notes")).toEqual(value); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,16 +97,16 @@ type Component = | |
export const Components: Record<FieldType, Component> = { | ||
text: { | ||
propsType: propsTypes.text, | ||
factory: (props) => <Widgets.TextWidget noLabel={true} {...props} />, | ||
factory: (props) => <Widgets.TextWidget id={props.name} noLabel={true} {...props} />, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify id for each element so that it can be connected to the corresponding label using |
||
}, | ||
textarea: { | ||
propsType: propsTypes.textarea, | ||
// TODO: Make rows configurable in the form builder | ||
factory: (props) => <Widgets.TextAreaWidget rows={3} {...props} />, | ||
factory: (props) => <Widgets.TextAreaWidget id={props.name} rows={3} {...props} />, | ||
}, | ||
number: { | ||
propsType: propsTypes.number, | ||
factory: (props) => <Widgets.NumberWidget noLabel={true} {...props} />, | ||
factory: (props) => <Widgets.NumberWidget id={props.name} noLabel={true} {...props} />, | ||
}, | ||
name: { | ||
propsType: propsTypes.name, | ||
|
@@ -211,14 +211,15 @@ export const Components: Record<FieldType, Component> = { | |
if (!props) { | ||
return <div />; | ||
} | ||
return <Widgets.TextWidget type="email" noLabel={true} {...props} />; | ||
return <Widgets.TextWidget type="email" id={props.name} noLabel={true} {...props} />; | ||
}, | ||
}, | ||
address: { | ||
propsType: propsTypes.address, | ||
factory: (props) => { | ||
return ( | ||
<AddressInput | ||
id={props.name} | ||
onChange={(val) => { | ||
props.setValue(val); | ||
}} | ||
|
@@ -248,6 +249,7 @@ export const Components: Record<FieldType, Component> = { | |
{value.map((field, index) => ( | ||
<li key={index}> | ||
<EmailField | ||
id={`${props.name}.${index}`} | ||
disabled={readOnly} | ||
value={value[index]} | ||
className={inputClassName} | ||
|
@@ -319,7 +321,7 @@ export const Components: Record<FieldType, Component> = { | |
...props, | ||
listValues: props.options.map((o) => ({ title: o.label, value: o.value })), | ||
}; | ||
return <Widgets.MultiSelectWidget {...newProps} />; | ||
return <Widgets.MultiSelectWidget id={props.name} {...newProps} />; | ||
}, | ||
}, | ||
select: { | ||
|
@@ -329,7 +331,7 @@ export const Components: Record<FieldType, Component> = { | |
...props, | ||
listValues: props.options.map((o) => ({ title: o.label, value: o.value })), | ||
}; | ||
return <Widgets.SelectWidget {...newProps} />; | ||
return <Widgets.SelectWidget id={props.name} {...newProps} />; | ||
}, | ||
}, | ||
checkbox: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of hiding it from the normal user only(and still showing up for screen reader), hide the label completely.
This is because screen reader then sees duplicate labels. This was identified by getByRole selector in react-testing-library tests.