Fixed typescript errors

This commit is contained in:
vutukuri15 2025-03-05 12:47:51 -08:00
parent b7238d11d4
commit 868d9aec54
3 changed files with 72 additions and 42 deletions

View File

@ -78,7 +78,7 @@ const IndexScreen: React.FC = () => {
const loadedState = await loadState(slot); const loadedState = await loadState(slot);
if (loadedState) { if (loadedState) {
setPlayerCount(loadedState.playerCount); setPlayerCount(loadedState.playerCount);
setBuyInAmount(loadedState.buyInAmount); setBuyInAmount(loadedState.buyInAmount ?? 20);
setNumberOfChips(loadedState.numberOfChips); setNumberOfChips(loadedState.numberOfChips);
setTotalChipsCount(loadedState.totalChipsCount); setTotalChipsCount(loadedState.totalChipsCount);
setSelectedCurrency(loadedState.selectedCurrency || "$"); setSelectedCurrency(loadedState.selectedCurrency || "$");

View File

@ -2,6 +2,7 @@ import React from "react";
import { fireEvent, render } from "@testing-library/react-native"; import { fireEvent, render } from "@testing-library/react-native";
import BuyInSelector from "@/components/BuyInSelector"; import BuyInSelector from "@/components/BuyInSelector";
// Mocking vector icons to prevent errors
jest.mock("@expo/vector-icons", () => { jest.mock("@expo/vector-icons", () => {
const { Text } = require("react-native"); const { Text } = require("react-native");
return { return {
@ -10,63 +11,73 @@ jest.mock("@expo/vector-icons", () => {
}); });
describe("BuyInSelector Component", () => { describe("BuyInSelector Component", () => {
let setBuyInAmount; let setBuyInAmount: jest.Mock;
let getByText;
let getByPlaceholderText;
let queryByText;
// Render the component with the necessary props // Render the component and return query methods
const renderComponent = (selectedCurrency = "$") => { const renderComponent = (selectedCurrency = "$") => {
const result = render( const utils = render(
<BuyInSelector <BuyInSelector
setBuyInAmount={setBuyInAmount} setBuyInAmount={setBuyInAmount}
selectedCurrency={selectedCurrency} selectedCurrency={selectedCurrency}
/> />
); );
getByText = result.getByText; return {
getByPlaceholderText = result.getByPlaceholderText; ...utils,
queryByText = result.queryByText; getByText: utils.getByText,
getByPlaceholderText: utils.getByPlaceholderText,
queryByText: utils.queryByText,
};
}; };
beforeEach(() => { beforeEach(() => {
setBuyInAmount = jest.fn(); setBuyInAmount = jest.fn();
renderComponent();
}); });
it("renders the buy-in options and input correctly", () => { it("renders the buy-in options and input correctly", () => {
const { getByText, getByPlaceholderText, queryByText } = renderComponent();
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(queryByText(/Selected Buy-in:.*None/i)).toBeTruthy();
// Check default selection with a more flexible approach
expect(queryByText(/Selected Buy-in.*None/)).toBeTruthy();
}); });
it("sets a predefined buy-in amount correctly", () => { it("sets a predefined buy-in amount correctly", () => {
const { getByText } = renderComponent();
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 { getByPlaceholderText } = renderComponent();
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 { getByPlaceholderText } = renderComponent();
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "-10"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "-10");
expect(setBuyInAmount).toHaveBeenCalledWith(25); expect(setBuyInAmount).toHaveBeenCalledWith(25); // Default reset
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("clears the custom amount when selecting a predefined option", () => { it("clears the custom amount when selecting a predefined option", () => {
const { getByPlaceholderText, getByText } = renderComponent();
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 { getByPlaceholderText } = renderComponent();
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "75"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "75");
expect(setBuyInAmount).toHaveBeenCalledWith(75); expect(setBuyInAmount).toHaveBeenCalledWith(75);
@ -78,33 +89,40 @@ describe("BuyInSelector Component", () => {
}); });
it("triggers state update every time a buy-in option is clicked, even if it's the same", () => { it("triggers state update every time a buy-in option is clicked, even if it's the same", () => {
const { getByText } = renderComponent();
fireEvent.press(getByText("$ 25")); fireEvent.press(getByText("$ 25"));
fireEvent.press(getByText("$ 25")); fireEvent.press(getByText("$ 25"));
expect(setBuyInAmount).toHaveBeenCalledTimes(2); expect(setBuyInAmount).toHaveBeenCalledTimes(2);
}); });
it("resets to default buy-in when custom input is cleared", () => { it("resets to default buy-in when custom input is cleared", () => {
const { getByPlaceholderText } = renderComponent();
const input = getByPlaceholderText("Enter custom buy-in"); const input = getByPlaceholderText("Enter custom buy-in");
fireEvent.changeText(input, "75"); fireEvent.changeText(input, "75");
expect(setBuyInAmount).toHaveBeenCalledWith(75); expect(setBuyInAmount).toHaveBeenCalledWith(75);
fireEvent.changeText(input, ""); fireEvent.changeText(input, "");
expect(setBuyInAmount).toHaveBeenCalledWith(25); expect(setBuyInAmount).toHaveBeenCalledWith(25);
}); });
it("updates state correctly when selecting predefined buy-in after entering a custom amount", () => { it("updates state correctly when selecting predefined buy-in after entering a custom amount", () => {
const { getByPlaceholderText, getByText } = renderComponent();
fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "200"); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "200");
expect(setBuyInAmount).toHaveBeenCalledWith(200); expect(setBuyInAmount).toHaveBeenCalledWith(200);
fireEvent.press(getByText("$ 10")); fireEvent.press(getByText("$ 10"));
expect(setBuyInAmount).toHaveBeenCalledWith(10); expect(setBuyInAmount).toHaveBeenCalledWith(10);
}); });
it("displays selected currency correctly", () => { it("displays selected currency correctly", () => {
renderComponent("€"); const { getByText, queryByText } = renderComponent("€");
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(queryByText(/Selected Buy-in:.*None/i)).toBeTruthy();
// Check default selection text with a flexible regex
expect(queryByText(/Selected Buy-in.*None/)).toBeTruthy();
}); });
}); });

View File

@ -17,11 +17,10 @@ jest.mock("expo-image-picker", () => ({
describe("ChipDetection", () => { describe("ChipDetection", () => {
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
global.fetch = jest.fn(() => jest.spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve({ Promise.resolve(
ok: true, new Response(
json: () => JSON.stringify({
Promise.resolve({
choices: [ choices: [
{ {
message: { message: {
@ -34,10 +33,16 @@ describe("ChipDetection", () => {
}, },
], ],
}), }),
}) { status: 200, headers: { "Content-Type": "application/json" } }
)
)
); );
}); });
afterEach(() => {
jest.restoreAllMocks(); // Reset all mocks to prevent test contamination
});
it("renders correctly", () => { it("renders correctly", () => {
const { getByText } = render( const { getByText } = render(
<ChipDetection updateChipCount={mockUpdateChipCount} /> <ChipDetection updateChipCount={mockUpdateChipCount} />
@ -48,7 +53,7 @@ describe("ChipDetection", () => {
}); });
it("picks an image from the library", async () => { it("picks an image from the library", async () => {
ImagePicker.launchImageLibraryAsync.mockResolvedValueOnce({ (ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValueOnce({
canceled: false, canceled: false,
assets: [{ uri: "test-uri", base64: "test-base64" }], assets: [{ uri: "test-uri", base64: "test-base64" }],
}); });
@ -62,10 +67,12 @@ describe("ChipDetection", () => {
}); });
it("takes a photo with the camera", async () => { it("takes a photo with the camera", async () => {
ImagePicker.requestCameraPermissionsAsync.mockResolvedValueOnce({ (
ImagePicker.requestCameraPermissionsAsync as jest.Mock
).mockResolvedValueOnce({
granted: true, granted: true,
}); });
ImagePicker.launchCameraAsync.mockResolvedValueOnce({ (ImagePicker.launchCameraAsync as jest.Mock).mockResolvedValueOnce({
canceled: false, canceled: false,
assets: [{ uri: "test-camera-uri", base64: "test-camera-base64" }], assets: [{ uri: "test-camera-uri", base64: "test-camera-base64" }],
}); });
@ -81,7 +88,9 @@ describe("ChipDetection", () => {
}); });
it("handles camera permission denied", async () => { it("handles camera permission denied", async () => {
ImagePicker.requestCameraPermissionsAsync.mockResolvedValueOnce({ (
ImagePicker.requestCameraPermissionsAsync as jest.Mock
).mockResolvedValueOnce({
granted: false, granted: false,
}); });
@ -98,16 +107,18 @@ describe("ChipDetection", () => {
}); });
it("displays error message on image processing failure", async () => { it("displays error message on image processing failure", async () => {
ImagePicker.launchImageLibraryAsync.mockResolvedValueOnce({ (ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValueOnce({
canceled: false, canceled: false,
assets: [{ uri: "test-uri", base64: "test-base64" }], assets: [{ uri: "test-uri", base64: "test-base64" }],
}); });
global.fetch.mockImplementationOnce(() => jest.spyOn(global, "fetch").mockImplementationOnce(() =>
Promise.resolve({ Promise.resolve(
ok: false, new Response(JSON.stringify({ choices: [] }), {
json: () => Promise.resolve({ choices: [] }), status: 400,
}) headers: { "Content-Type": "application/json" },
})
)
); );
const { getByText } = render( const { getByText } = render(
@ -121,16 +132,15 @@ describe("ChipDetection", () => {
}); });
it("handles valid API response correctly", async () => { it("handles valid API response correctly", async () => {
ImagePicker.launchImageLibraryAsync.mockResolvedValueOnce({ (ImagePicker.launchImageLibraryAsync as jest.Mock).mockResolvedValueOnce({
canceled: false, canceled: false,
assets: [{ uri: "test-uri", base64: "test-base64" }], assets: [{ uri: "test-uri", base64: "test-base64" }],
}); });
global.fetch.mockImplementationOnce(() => jest.spyOn(global, "fetch").mockImplementationOnce(() =>
Promise.resolve({ Promise.resolve(
ok: true, new Response(
json: () => JSON.stringify({
Promise.resolve({
choices: [ choices: [
{ {
message: { message: {
@ -139,7 +149,9 @@ describe("ChipDetection", () => {
}, },
], ],
}), }),
}) { status: 200, headers: { "Content-Type": "application/json" } }
)
)
); );
const { getByText } = render( const { getByText } = render(