-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from degica/task/test-cases-2
Test cases
- Loading branch information
Showing
8 changed files
with
422 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import React, { ReactNode } from "react"; | ||
import { render, fireEvent } from "@testing-library/react-native"; | ||
|
||
import { formatCreditCardNumber, formatExpiry } from "../util/helpers"; | ||
import { Actions, DispatchContext, StateContext } from "../state"; | ||
import CardInputGroup from "../components/CardInputGroup"; | ||
import { isCardNumberValid, validateCardExpiry } from "../util/validator"; | ||
|
||
// Mock helper functions | ||
jest.mock("../util/helpers", () => ({ | ||
formatCreditCardNumber: jest.fn(), | ||
formatExpiry: jest.fn(), | ||
})); | ||
|
||
// Mock validate function | ||
|
||
jest.mock("../util/validator", () => ({ | ||
isCardNumberValid: jest.fn(), | ||
validateCardExpiry: jest.fn(), | ||
})); | ||
|
||
// Mocked context values | ||
const mockState = { | ||
cardCVV: "", | ||
cardNumber: "", | ||
cardExpiredDate: "", | ||
}; | ||
|
||
const mockDispatch = jest.fn(); | ||
|
||
describe("CardInputGroup Component", () => { | ||
const renderWithContext = (component: ReactNode) => { | ||
return render( | ||
<StateContext.Provider value={mockState}> | ||
<DispatchContext.Provider value={mockDispatch}> | ||
{component} | ||
</DispatchContext.Provider> | ||
</StateContext.Provider> | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
// Clear all mock call history before each test | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("renders correctly with initial state", () => { | ||
const { getByTestId } = renderWithContext(<CardInputGroup />); | ||
|
||
// Check if the inputs render with the initial values from context | ||
expect(getByTestId("cardNumberInput").props.value).toBe( | ||
mockState.cardNumber | ||
); | ||
expect(getByTestId("cardExpiryInput").props.value).toBe( | ||
mockState.cardExpiredDate | ||
); | ||
expect(getByTestId("cardCVVInput").props.value).toBe(mockState.cardCVV); | ||
}); | ||
|
||
it("updates card number when valid and dispatches action", () => { | ||
isCardNumberValid.mockReturnValue(true); | ||
formatCreditCardNumber.mockReturnValue("1234 1234 1234 1234"); | ||
|
||
const { getByTestId } = renderWithContext(<CardInputGroup />); | ||
|
||
const cardNumberInput = getByTestId("cardNumberInput"); | ||
|
||
// Simulate typing a valid card number | ||
fireEvent.changeText(cardNumberInput, "1234123412341234"); | ||
|
||
// Check if the validation function was called | ||
expect(isCardNumberValid).toHaveBeenCalledWith("1234123412341234"); | ||
|
||
// Check if the dispatch was called with the correct action | ||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: Actions.SET_CARD_NUMBER, | ||
payload: "1234 1234 1234 1234", | ||
}); | ||
}); | ||
|
||
it("does not update card number when invalid", () => { | ||
isCardNumberValid.mockReturnValue(false); | ||
|
||
const { getByTestId } = renderWithContext(<CardInputGroup />); | ||
|
||
const cardNumberInput = getByTestId("cardNumberInput"); | ||
|
||
// Simulate typing an invalid card number | ||
fireEvent.changeText(cardNumberInput, "1234"); | ||
|
||
// Check if the validation function was called | ||
expect(isCardNumberValid).toHaveBeenCalledWith("1234"); | ||
|
||
// Check that dispatch was not called | ||
expect(mockDispatch).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("updates card expiry date and dispatches action", () => { | ||
validateCardExpiry.mockReturnValue(true); | ||
formatExpiry.mockReturnValue("12 / 25"); | ||
|
||
const { getByTestId } = renderWithContext(<CardInputGroup />); | ||
|
||
const cardExpiryInput = getByTestId("cardExpiryInput"); | ||
|
||
// Simulate typing a valid expiry date | ||
fireEvent.changeText(cardExpiryInput, "12/25"); | ||
|
||
// Check if the validation function was called | ||
expect(validateCardExpiry).toHaveBeenCalledWith("12/25"); | ||
|
||
// Check if the dispatch was called with the correct action | ||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: Actions.SET_CARD_EXPIRED_DATE, | ||
payload: "12 / 25", | ||
}); | ||
}); | ||
|
||
it("updates card CVV and dispatches action", () => { | ||
const { getByTestId } = renderWithContext(<CardInputGroup />); | ||
|
||
const cardCVVInput = getByTestId("cardCVVInput"); | ||
|
||
// Simulate typing a CVV | ||
fireEvent.changeText(cardCVVInput, "123"); | ||
|
||
// Check if the dispatch was called with the correct action | ||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: Actions.SET_CARD_CVV, | ||
payload: "123", | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,9 +1,74 @@ | ||
import React from "react"; | ||
import { render } from "@testing-library/react-native"; | ||
import React, { ReactNode } from "react"; | ||
import { fireEvent, render, waitFor } from "@testing-library/react-native"; | ||
|
||
import CardSection from "../components/sections/CardSection"; | ||
|
||
test("Cardholder name should be initially rendered", () => { | ||
const { getByText } = render(<CardSection />); | ||
expect(getByText("Cardholder name")).toBeTruthy(); | ||
import StateProvider from "../components/paymentState/stateProvider"; | ||
import { DispatchContext, StateContext } from "../state"; | ||
import { PaymentType } from "../util/types"; | ||
|
||
export const mockState = { | ||
sessionPay: jest.fn(), | ||
cardholderName: "John Doe", | ||
cardCVV: "", | ||
cardNumber: "", | ||
cardExpiredDate: "", | ||
amount: 1000, | ||
currency: "USD", | ||
}; | ||
|
||
export const mockDispatch = jest.fn(); | ||
|
||
export const renderWithContext = (component: ReactNode) => { | ||
return render( | ||
<StateContext.Provider value={mockState}> | ||
<DispatchContext.Provider value={mockDispatch}> | ||
{component} | ||
</DispatchContext.Provider> | ||
</StateContext.Provider> | ||
); | ||
}; | ||
|
||
describe("Card Section Test cases", () => { | ||
test("Cardholder name should be initially rendered", async () => { | ||
const { getByText, getByTestId } = render( | ||
<StateProvider> | ||
<CardSection /> | ||
</StateProvider> | ||
); | ||
const cardHolderText = getByText("Cardholder name"); | ||
expect(cardHolderText).toBeTruthy(); | ||
}); | ||
|
||
test("When enter card holders name state context should capture the card holder name", async () => { | ||
const { getByText, getByTestId } = render( | ||
<StateProvider> | ||
<CardSection /> | ||
</StateProvider> | ||
); | ||
fireEvent.changeText(getByText("Cardholder name"), "Kasun Prabath"); | ||
await waitFor(() => | ||
expect(getByTestId("cardHolderName").props.value).toBe("Kasun Prabath") | ||
); | ||
}); | ||
|
||
it("calls sessionPay with correct parameters on button press", () => { | ||
const { getByTestId } = renderWithContext(<CardSection />); | ||
|
||
const payButton = getByTestId("PayCTA"); | ||
|
||
// Simulate pressing the Pay button | ||
fireEvent.press(payButton); | ||
|
||
// Check if sessionPay was called with the correct arguments | ||
expect(mockState.sessionPay).toHaveBeenCalledWith({ | ||
paymentType: PaymentType.CREDIT, | ||
cardDetails: { | ||
cardholderName: mockState.cardholderName, | ||
cardCVV: mockState.cardCVV, | ||
cardNumber: mockState.cardNumber, | ||
cardExpiredDate: mockState.cardExpiredDate, | ||
}, | ||
}); | ||
}); | ||
}); |
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,89 @@ | ||
import payForSession from "../services/paymentService"; | ||
import { PaymentType } from "../util/types"; | ||
|
||
// Mock the fetch function | ||
global.fetch = jest.fn(); | ||
|
||
const paymentDetails = { | ||
publicKey: "samplepublickKey", | ||
sessionId: "sessionID123", | ||
paymentType: PaymentType.CREDIT, | ||
cardDetails: { | ||
cardholderName: "John Doe", | ||
cardNumber: "1234 5678 9012 3456", | ||
cardExpiredDate: "12/25", | ||
cardCVV: "123", | ||
}, | ||
}; | ||
|
||
describe("payForSession", () => { | ||
test("should successfully process credit card payment", async () => { | ||
// Mock successful response from the server | ||
|
||
const successPaymentResponse = { | ||
redirect_url: null, | ||
status: "completed", | ||
payment: { | ||
id: "15xvkc1r4we8g4xjgcrj2ltzd", | ||
resource: "payment", | ||
status: "captured", | ||
amount: 1000, | ||
tax: 0, | ||
customer: null, | ||
payment_deadline: "2024-06-08T14:59:59Z", | ||
payment_details: { | ||
type: "credit_card", | ||
email: null, | ||
brand: "visa", | ||
last_four_digits: "0100", | ||
month: 8, | ||
year: 2025, | ||
}, | ||
payment_method_fee: 0, | ||
total: 1000, | ||
currency: "JPY", | ||
description: null, | ||
captured_at: "2024-06-06T12:56:14Z", | ||
external_order_num: null, | ||
metadata: {}, | ||
created_at: "2024-06-06T12:56:14Z", | ||
amount_refunded: 0, | ||
locale: "ja", | ||
session: "6u1jrun2y7fmr8wwu7qskwgov", | ||
customer_family_name: null, | ||
customer_given_name: null, | ||
mcc: null, | ||
statement_descriptor: null, | ||
refunds: [], | ||
refund_requests: [], | ||
}, | ||
}; | ||
|
||
fetch.mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce(successPaymentResponse), | ||
}); | ||
|
||
const paymentResponse = await payForSession(paymentDetails); | ||
expect(paymentResponse).toBe(successPaymentResponse); | ||
}); | ||
|
||
test("should handle network error gracefully", async () => { | ||
const errorResponse = { | ||
error: { | ||
code: "unprocessable_entity", | ||
message: | ||
"Validation failed: Payment is invalid, Payment source is invalid, Payment source number is not a valid credit card number", | ||
param: null, | ||
details: {}, | ||
}, | ||
}; | ||
// Mock network error response from the server | ||
fetch.mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce(errorResponse), | ||
}); | ||
const paymentResponse = await payForSession(paymentDetails); | ||
expect(paymentResponse).toBe(errorResponse); | ||
}); | ||
|
||
// Add more test cases for different scenarios | ||
}); |
Oops, something went wrong.