From 76342ed534e2bdec3e76dc9355d1aa9b2ea97003 Mon Sep 17 00:00:00 2001 From: vutukuri15 Date: Sun, 9 Feb 2025 21:21:33 -0800 Subject: [PATCH] Replaced external image icon with MaterialIcons for money image --- components/BuyInSelector.tsx | 31 ++++++--------------- components/__tests__/BuyInSelector.test.tsx | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/components/BuyInSelector.tsx b/components/BuyInSelector.tsx index 88b9e2c..48d56c1 100644 --- a/components/BuyInSelector.tsx +++ b/components/BuyInSelector.tsx @@ -4,29 +4,29 @@ import { Text, TextInput, TouchableOpacity, - Image, StyleSheet, } from "react-native"; +import { MaterialIcons } from "@expo/vector-icons"; interface BuyInSelectorProps { setBuyInAmount: React.Dispatch>; } -const defaultBuyInOptions = [10, 25, 50]; // Default buy-in values +const defaultBuyInOptions = [10, 25, 50]; const BuyInSelector: React.FC = ({ setBuyInAmount }) => { const [customAmount, setCustomAmount] = useState(""); - const [buyInAmount, setBuyInAmountState] = useState(null); // Store the selected amount locally + const [buyInAmount, setBuyInAmountState] = useState(null); const handleCustomAmountChange = (value: string) => { const numericValue = parseFloat(value); if (!isNaN(numericValue) && numericValue >= 0) { setCustomAmount(value); - setBuyInAmountState(numericValue); // Update buy-in amount - setBuyInAmount(numericValue); // Pass it back to parent component + setBuyInAmountState(numericValue); + setBuyInAmount(numericValue); } else { setCustomAmount(""); - setBuyInAmountState(null); // Reset if invalid + setBuyInAmountState(null); setBuyInAmount(null); } }; @@ -34,23 +34,16 @@ const BuyInSelector: React.FC = ({ setBuyInAmount }) => { const handleBuyInSelection = (amount: number) => { setBuyInAmountState(amount); setCustomAmount(""); - setBuyInAmount(amount); // Pass it back to parent component + setBuyInAmount(amount); }; return ( - {/* Title with Money Icon */} - + Select Buy-in Amount: - {/* Default Buy-In Options */} {defaultBuyInOptions.map((amount) => ( = ({ setBuyInAmount }) => { ))} - {/* Custom Amount Input */} Or enter a custom amount: = ({ setBuyInAmount }) => { keyboardType="numeric" /> - {/* Display the selected buy-in amount */} Selected Buy-in: {buyInAmount !== null ? buyInAmount : "None"} @@ -93,13 +84,9 @@ const styles = StyleSheet.create({ alignItems: "center", marginBottom: 10, }, - icon: { - width: 50, - height: 50, - marginRight: 15, - }, title: { fontSize: 22, + marginLeft: 10, }, optionsContainer: { flexDirection: "row", diff --git a/components/__tests__/BuyInSelector.test.tsx b/components/__tests__/BuyInSelector.test.tsx index 8a9af11..6f6706f 100644 --- a/components/__tests__/BuyInSelector.test.tsx +++ b/components/__tests__/BuyInSelector.test.tsx @@ -2,6 +2,14 @@ import React from "react"; import { fireEvent, render } from "@testing-library/react-native"; import BuyInSelector from "@/components/BuyInSelector"; +jest.mock("@expo/vector-icons", () => { + const React = require("react"); + const { Text } = require("react-native"); + return { + MaterialIcons: () => MaterialIcons, + }; +}); + describe("BuyInSelector Component", () => { it("renders the buy-in options and input correctly", () => { const setBuyInAmount = jest.fn(); @@ -23,6 +31,7 @@ describe("BuyInSelector Component", () => { ); fireEvent.press(getByText("25")); + expect(setBuyInAmount).toHaveBeenCalledWith(25); }); @@ -33,6 +42,7 @@ describe("BuyInSelector Component", () => { ); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "100"); + expect(setBuyInAmount).toHaveBeenCalledWith(100); }); @@ -43,6 +53,7 @@ describe("BuyInSelector Component", () => { ); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "-10"); + expect(setBuyInAmount).toHaveBeenCalledWith(null); }); @@ -53,7 +64,25 @@ describe("BuyInSelector Component", () => { ); fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "100"); + fireEvent.press(getByText("50")); + expect(setBuyInAmount).toHaveBeenCalledWith(50); }); + + it("handles valid and invalid input for custom amount correctly", () => { + const setBuyInAmount = jest.fn(); + const { getByPlaceholderText } = render( + + ); + + fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "75"); + expect(setBuyInAmount).toHaveBeenCalledWith(75); + + fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "-5"); + expect(setBuyInAmount).toHaveBeenCalledWith(null); + + fireEvent.changeText(getByPlaceholderText("Enter custom buy-in"), "abc"); + expect(setBuyInAmount).toHaveBeenCalledWith(null); + }); });