Skip to content

Commit

Permalink
fix: update credit card form validation (#11306)
Browse files Browse the repository at this point in the history
* fix: update credit card form validation

* add test

(cherry picked from commit e2c4d6d)
  • Loading branch information
MrSltun authored and MounirDhahri committed Dec 18, 2024
1 parent 4bf23dc commit 465b008
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/app/Components/Bidding/Screens/CreditCardForm.tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe("CreditCardForm", () => {
await waitFor(() => expect(addButton.props.accessibilityState.disabled).toEqual(true))
})

it("is enabled while the form is valid", () => {
it("is enabled while the form is valid", async () => {
const onSubmitMock = jest.fn()

;(createToken as jest.Mock).mockReturnValueOnce(stripeToken)
Expand All @@ -187,7 +187,33 @@ describe("CreditCardForm", () => {

const addButton = screen.getByTestId("credit-card-form-button")

expect(addButton.props.accessibilityState.disabled).toEqual(false)
await waitFor(() => expect(addButton.props.accessibilityState.disabled).toEqual(false))
})

it("is disabled when the form is invalid", async () => {
renderWithWrappers(
<CreditCardForm
onSubmit={onSubmitMock}
navigator={{ pop: () => null } as any}
billingAddress={{
fullName: "mockName",
addressLine1: "mockAddress1",
addressLine2: "mockAddress2",
city: "mockCity",
state: "mockState",
postalCode: "mockPostalCode",
phoneNumber: "mockPhone",
country: { shortName: "US", longName: "United States" },
}}
/>
)

const creditCardField = screen.getByTestId("credit-card-field")
fireEvent.changeText(creditCardField, "4242") // incomplete number

const addButton = screen.getByTestId("credit-card-form-button")

await waitFor(() => expect(addButton.props.accessibilityState.disabled).toEqual(true))
})

it("shows an error when stripe's API returns an error", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as Yup from "yup"

export const creditCardFormValidationSchema = Yup.object().shape({
creditCard: Yup.object().shape({
valid: Yup.boolean().required("Credit card is required"),
valid: Yup.boolean()
.oneOf([true], "Credit card is required")
.required("Credit card is required"),
}),
fullName: Yup.string().required("Name is required"),
addressLine1: Yup.string().required("Address is required"),
Expand Down

0 comments on commit 465b008

Please sign in to comment.