Merge pull request #36 from djwesty/vutukuri15/HWtest

Update Unit Tests for BuyInSelector Component
This commit is contained in:
Vutukuri15 2025-02-27 14:47:23 -08:00 committed by GitHub
commit f7d97cc1c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,6 @@ import { fireEvent, render } from "@testing-library/react-native";
import BuyInSelector from "@/components/BuyInSelector"; import BuyInSelector from "@/components/BuyInSelector";
jest.mock("@expo/vector-icons", () => { jest.mock("@expo/vector-icons", () => {
const React = require("react");
const { Text } = require("react-native"); const { Text } = require("react-native");
return { return {
MaterialIcons: () => <Text>MaterialIcons</Text>, MaterialIcons: () => <Text>MaterialIcons</Text>,
@ -11,71 +10,54 @@ jest.mock("@expo/vector-icons", () => {
}); });
describe("BuyInSelector Component", () => { describe("BuyInSelector Component", () => {
it("renders the buy-in options and input correctly", () => { let setBuyInAmount;
const setBuyInAmount = jest.fn(); let getByText;
const { getByText, getByPlaceholderText } = render( let getByPlaceholderText;
<BuyInSelector setBuyInAmount={setBuyInAmount} />
);
const renderComponent = () => {
const result = render(<BuyInSelector setBuyInAmount={setBuyInAmount} />);
getByText = result.getByText;
getByPlaceholderText = result.getByPlaceholderText;
};
beforeEach(() => {
setBuyInAmount = jest.fn();
renderComponent();
});
it("renders the buy-in options and input correctly", () => {
expect(getByText("Select Buy-in Amount:")).toBeTruthy(); expect(getByText("Select Buy-in Amount:")).toBeTruthy();
expect(getByText("10")).toBeTruthy(); expect(getByText("10")).toBeTruthy();
expect(getByText("25")).toBeTruthy(); expect(getByText("25")).toBeTruthy();
expect(getByText("50")).toBeTruthy(); expect(getByText("50")).toBeTruthy();
expect(getByPlaceholderText("Enter custom buy-in")).toBeTruthy(); expect(getByPlaceholderText("Enter custom buy-in")).toBeTruthy();
expect(getByText("Selected Buy-in: None")).toBeTruthy(); // Check default selection
}); });
it("sets a predefined buy-in amount correctly", () => { it("sets a predefined buy-in amount correctly", () => {
const setBuyInAmount = jest.fn();
const { getByText } = render(
<BuyInSelector setBuyInAmount={setBuyInAmount} />
);
fireEvent.press(getByText("25")); fireEvent.press(getByText("25"));
expect(setBuyInAmount).toHaveBeenCalledWith(25); expect(setBuyInAmount).toHaveBeenCalledWith(25);
}); });
it("sets a custom buy-in amount correctly", () => { it("sets a custom buy-in amount correctly", () => {
const setBuyInAmount = jest.fn();
const { getByPlaceholderText } = render(
<BuyInSelector setBuyInAmount={setBuyInAmount} />
);
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "100"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "100");
expect(setBuyInAmount).toHaveBeenCalledWith(100); expect(setBuyInAmount).toHaveBeenCalledWith(100);
}); });
it("resets custom amount if invalid input is entered", () => { it("resets custom amount if invalid input is entered", () => {
const setBuyInAmount = jest.fn();
const { getByPlaceholderText } = render(
<BuyInSelector setBuyInAmount={setBuyInAmount} />
);
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "-10"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "-10");
expect(setBuyInAmount).toHaveBeenCalledWith(25); // Assuming 25 is the default
expect(setBuyInAmount).toHaveBeenCalledWith(25); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "abc");
expect(setBuyInAmount).toHaveBeenCalledWith(25); // Reset to default
}); });
it("clears the custom amount when selecting a predefined option", () => { it("clears the custom amount when selecting a predefined option", () => {
const setBuyInAmount = jest.fn();
const { getByText, getByPlaceholderText } = render(
<BuyInSelector setBuyInAmount={setBuyInAmount} />
);
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "100"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "100");
fireEvent.press(getByText("50")); fireEvent.press(getByText("50"));
expect(setBuyInAmount).toHaveBeenCalledWith(50); expect(setBuyInAmount).toHaveBeenCalledWith(50);
}); });
it("handles valid and invalid input for custom amount correctly", () => { it("handles valid and invalid input for custom amount correctly", () => {
const setBuyInAmount = jest.fn();
const { getByPlaceholderText } = render(
<BuyInSelector setBuyInAmount={setBuyInAmount} />
);
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "75"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "75");
expect(setBuyInAmount).toHaveBeenCalledWith(75); expect(setBuyInAmount).toHaveBeenCalledWith(75);
@ -85,4 +67,25 @@ describe("BuyInSelector Component", () => {
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "abc"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "abc");
expect(setBuyInAmount).toHaveBeenCalledWith(25); expect(setBuyInAmount).toHaveBeenCalledWith(25);
}); });
it("triggers state update every time a buy-in option is clicked, even if it's the same", () => {
fireEvent.press(getByText("25")); // First click
fireEvent.press(getByText("25")); // Clicking the same option again
expect(setBuyInAmount).toHaveBeenCalledTimes(2); // Expect it to be called twice
});
it("resets to default buy-in when custom input is cleared", () => {
const input = getByPlaceholderText("Enter custom buy-in");
fireEvent.changeText(input, "75");
expect(setBuyInAmount).toHaveBeenCalledWith(75);
fireEvent.changeText(input, "");
expect(setBuyInAmount).toHaveBeenCalledWith(25); // Assuming 25 is the default
});
it("updates state correctly when selecting predefined buy-in after entering a custom amount", () => {
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "200");
expect(setBuyInAmount).toHaveBeenCalledWith(200);
fireEvent.press(getByText("10"));
expect(setBuyInAmount).toHaveBeenCalledWith(10);
});
}); });