Created a BuyInAmount Selector component; Added index; Added tests
This commit is contained in:
parent
98b2b4020c
commit
83776bc651
@ -1,9 +1,17 @@
|
||||
import React, { useState } from "react";
|
||||
import { ScrollView, Text } from "react-native";
|
||||
import { ScrollView, Text, Alert, Button } from "react-native";
|
||||
import PlayerSelector from "@/components/PlayerSelector";
|
||||
import BuyInSelector from "@/components/BuyInSelector";
|
||||
|
||||
const IndexScreen = () => {
|
||||
const [playerCount, setPlayerCount] = useState(2);
|
||||
const [buyInAmount, setBuyInAmount] = useState<number | null>(null);
|
||||
|
||||
const handleSave = () => {
|
||||
if (buyInAmount === null) {
|
||||
Alert.alert("Error", "Please select a valid buy-in amount");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ScrollView contentContainerStyle={{ padding: 20, flexGrow: 1 }}>
|
||||
@ -15,7 +23,16 @@ const IndexScreen = () => {
|
||||
playerCount={playerCount}
|
||||
setPlayerCount={setPlayerCount}
|
||||
/>
|
||||
|
||||
<BuyInSelector setBuyInAmount={setBuyInAmount} />
|
||||
|
||||
<Button
|
||||
title="Save"
|
||||
onPress={handleSave}
|
||||
disabled={buyInAmount === null}
|
||||
/>
|
||||
</ScrollView>
|
||||
);
|
||||
};
|
||||
|
||||
export default IndexScreen;
|
||||
|
138
components/BuyInSelector.tsx
Normal file
138
components/BuyInSelector.tsx
Normal file
@ -0,0 +1,138 @@
|
||||
import React, { useState } from "react";
|
||||
import {
|
||||
View,
|
||||
Text,
|
||||
TextInput,
|
||||
TouchableOpacity,
|
||||
Image,
|
||||
StyleSheet,
|
||||
} from "react-native";
|
||||
|
||||
interface BuyInSelectorProps {
|
||||
setBuyInAmount: React.Dispatch<React.SetStateAction<number | null>>;
|
||||
}
|
||||
|
||||
const defaultBuyInOptions = [10, 25, 50]; // Default buy-in values
|
||||
|
||||
const BuyInSelector: React.FC<BuyInSelectorProps> = ({ setBuyInAmount }) => {
|
||||
const [customAmount, setCustomAmount] = useState("");
|
||||
const [buyInAmount, setBuyInAmountState] = useState<number | null>(null); // Store the selected amount locally
|
||||
|
||||
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
|
||||
} else {
|
||||
setCustomAmount("");
|
||||
setBuyInAmountState(null); // Reset if invalid
|
||||
setBuyInAmount(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleBuyInSelection = (amount: number) => {
|
||||
setBuyInAmountState(amount);
|
||||
setCustomAmount("");
|
||||
setBuyInAmount(amount); // Pass it back to parent component
|
||||
};
|
||||
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
{/* Title with Money Icon */}
|
||||
<View style={styles.header}>
|
||||
<Image
|
||||
source={{
|
||||
uri: "https://png.pngtree.com/png-vector/20191128/ourmid/pngtree-coin-money-icon-png-image_2049478.jpg",
|
||||
}}
|
||||
style={styles.icon}
|
||||
/>
|
||||
<Text style={styles.title}>Select Buy-in Amount:</Text>
|
||||
</View>
|
||||
|
||||
{/* Default Buy-In Options */}
|
||||
<View style={styles.optionsContainer}>
|
||||
{defaultBuyInOptions.map((amount) => (
|
||||
<TouchableOpacity
|
||||
key={amount}
|
||||
style={[
|
||||
styles.buyInButton,
|
||||
buyInAmount === amount ? styles.selectedButton : null,
|
||||
]}
|
||||
onPress={() => handleBuyInSelection(amount)}
|
||||
>
|
||||
<Text style={styles.buttonText}>{amount}</Text>
|
||||
</TouchableOpacity>
|
||||
))}
|
||||
</View>
|
||||
|
||||
{/* Custom Amount Input */}
|
||||
<Text style={styles.orText}>Or enter a custom amount:</Text>
|
||||
<TextInput
|
||||
style={styles.input}
|
||||
value={customAmount}
|
||||
onChangeText={handleCustomAmountChange}
|
||||
placeholder="Enter custom buy-in"
|
||||
keyboardType="numeric"
|
||||
/>
|
||||
|
||||
{/* Display the selected buy-in amount */}
|
||||
<Text style={styles.selectionText}>
|
||||
Selected Buy-in: {buyInAmount !== null ? buyInAmount : "None"}
|
||||
</Text>
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
padding: 20,
|
||||
},
|
||||
header: {
|
||||
flexDirection: "row",
|
||||
alignItems: "center",
|
||||
marginBottom: 10,
|
||||
},
|
||||
icon: {
|
||||
width: 50,
|
||||
height: 50,
|
||||
marginRight: 15,
|
||||
},
|
||||
title: {
|
||||
fontSize: 22,
|
||||
},
|
||||
optionsContainer: {
|
||||
flexDirection: "row",
|
||||
justifyContent: "space-around",
|
||||
marginVertical: 10,
|
||||
},
|
||||
buyInButton: {
|
||||
backgroundColor: "#ddd",
|
||||
padding: 10,
|
||||
borderRadius: 5,
|
||||
},
|
||||
selectedButton: {
|
||||
backgroundColor: "#4caf50",
|
||||
},
|
||||
buttonText: {
|
||||
fontSize: 16,
|
||||
},
|
||||
orText: {
|
||||
marginTop: 10,
|
||||
textAlign: "center",
|
||||
},
|
||||
input: {
|
||||
borderWidth: 1,
|
||||
borderColor: "#ccc",
|
||||
padding: 8,
|
||||
marginVertical: 10,
|
||||
borderRadius: 5,
|
||||
},
|
||||
selectionText: {
|
||||
marginTop: 15,
|
||||
fontSize: 16,
|
||||
fontWeight: "bold",
|
||||
},
|
||||
});
|
||||
|
||||
export default BuyInSelector;
|
59
components/__tests__/BuyInSelector.test.tsx
Normal file
59
components/__tests__/BuyInSelector.test.tsx
Normal file
@ -0,0 +1,59 @@
|
||||
import React from "react";
|
||||
import { fireEvent, render } from "@testing-library/react-native";
|
||||
import BuyInSelector from "@/components/BuyInSelector";
|
||||
|
||||
describe("BuyInSelector Component", () => {
|
||||
it("renders the buy-in options and input correctly", () => {
|
||||
const setBuyInAmount = jest.fn();
|
||||
const { getByText, getByPlaceholderText } = render(
|
||||
<BuyInSelector setBuyInAmount={setBuyInAmount} />
|
||||
);
|
||||
|
||||
expect(getByText("Select Buy-in Amount:")).toBeTruthy();
|
||||
expect(getByText("10")).toBeTruthy();
|
||||
expect(getByText("25")).toBeTruthy();
|
||||
expect(getByText("50")).toBeTruthy();
|
||||
expect(getByPlaceholderText("Enter custom buy-in")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("sets a predefined buy-in amount correctly", () => {
|
||||
const setBuyInAmount = jest.fn();
|
||||
const { getByText } = render(
|
||||
<BuyInSelector setBuyInAmount={setBuyInAmount} />
|
||||
);
|
||||
|
||||
fireEvent.press(getByText("25"));
|
||||
expect(setBuyInAmount).toHaveBeenCalledWith(25);
|
||||
});
|
||||
|
||||
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");
|
||||
expect(setBuyInAmount).toHaveBeenCalledWith(100);
|
||||
});
|
||||
|
||||
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");
|
||||
expect(setBuyInAmount).toHaveBeenCalledWith(null);
|
||||
});
|
||||
|
||||
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.press(getByText("50"));
|
||||
expect(setBuyInAmount).toHaveBeenCalledWith(50);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user