From d878e93eb480261251d25dd7d0f5e2e10d45017e Mon Sep 17 00:00:00 2001 From: MantashaNoyela Date: Tue, 25 Feb 2025 20:46:59 -0800 Subject: [PATCH] Restores the previous state when reopened --- app/index.tsx | 23 +++++++- components/PersistentState.tsx | 28 ++++++++++ components/__tests__/PersistentState.test.tsx | 53 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 components/PersistentState.tsx create mode 100644 components/__tests__/PersistentState.test.tsx diff --git a/app/index.tsx b/app/index.tsx index 1620b18..eab3b10 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -1,10 +1,11 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { ScrollView, Text, Alert, Button, View, StyleSheet } from "react-native"; import PlayerSelector from "@/components/PlayerSelector"; import BuyInSelector from "@/components/BuyInSelector"; import ChipsSelector from "@/components/ChipsSelector"; import ChipDistributionSummary from "@/components/ChipDistributionSummary"; import { saveState, loadState } from "../components/CalculatorState"; +import { savePersistentState, loadPersistentState } from "../components/PersistentState"; const IndexScreen = () => { const [playerCount, setPlayerCount] = useState(2); @@ -12,6 +13,24 @@ const IndexScreen = () => { const [numberOfChips, setNumberOfChips] = useState(5); const [totalChipsCount, setTotalChipsCount] = useState([]); + useEffect(() => { + const loadPersistentData = async () => { + const savedState = await loadPersistentState(); + if (savedState) { + setPlayerCount(savedState.playerCount); + setBuyInAmount(savedState.buyInAmount); + setNumberOfChips(savedState.numberOfChips); + setTotalChipsCount(savedState.totalChipsCount); + } + }; + loadPersistentData(); + }, []); + + useEffect(() => { + const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount }; + savePersistentState(state); + }, [playerCount, buyInAmount, numberOfChips, totalChipsCount]); + const handleSave = async (slot: "SLOT1" | "SLOT2") => { if (buyInAmount === null) { Alert.alert("Error", "Please select a valid buy-in amount"); @@ -73,4 +92,4 @@ const styles = StyleSheet.create({ }, }); -export default IndexScreen; +export default IndexScreen; \ No newline at end of file diff --git a/components/PersistentState.tsx b/components/PersistentState.tsx new file mode 100644 index 0000000..f235325 --- /dev/null +++ b/components/PersistentState.tsx @@ -0,0 +1,28 @@ +import AsyncStorage from "@react-native-async-storage/async-storage"; + +const STORAGE_KEY = "@poker_calculator_state"; + +export interface PokerState { + playerCount: number; + buyInAmount: number | null; + numberOfChips: number; + totalChipsCount: number[]; +} + +export const savePersistentState = async (state: PokerState) => { + try { + await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(state)); + return { success: true, message: "State saved successfully" }; + } catch (error) { + return { success: false, message: "Failed to save state" }; + } +}; + +export const loadPersistentState = async (): Promise => { + try { + const storedState = await AsyncStorage.getItem(STORAGE_KEY); + return storedState ? JSON.parse(storedState) : null; + } catch (error) { + return null; + } +}; diff --git a/components/__tests__/PersistentState.test.tsx b/components/__tests__/PersistentState.test.tsx new file mode 100644 index 0000000..1626194 --- /dev/null +++ b/components/__tests__/PersistentState.test.tsx @@ -0,0 +1,53 @@ +import AsyncStorage from "@react-native-async-storage/async-storage"; +import { saveState, loadState, PokerState } from "../CalculatorState"; + +jest.mock("@react-native-async-storage/async-storage", () => ({ + setItem: jest.fn(), + getItem: jest.fn(), +})); + +describe("CalculatorState.tsx", () => { + const mockState: PokerState = { + playerCount: 4, + buyInAmount: 50, + numberOfChips: 5, + totalChipsCount: [100, 200, 150, 50, 75], + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("should save state successfully", async () => { + (AsyncStorage.setItem as jest.Mock).mockResolvedValueOnce(undefined); + const result = await saveState("SLOT1", mockState); + expect(result.success).toBe(true); + expect(result.message).toBe("State saved to SLOT1"); + expect(AsyncStorage.setItem).toHaveBeenCalledWith("@poker_state_slot1", JSON.stringify(mockState)); + }); + + it("should fail to save state if an error occurs", async () => { + (AsyncStorage.setItem as jest.Mock).mockRejectedValueOnce(new Error("Failed to save")); + const result = await saveState("SLOT1", mockState); + expect(result.success).toBe(false); + expect(result.message).toBe("Failed to save state"); + }); + + it("should load state successfully", async () => { + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(mockState)); + const result = await loadState("SLOT1"); + expect(result).toEqual(mockState); + }); + + it("should return null if no state is found", async () => { + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(null); + const result = await loadState("SLOT1"); + expect(result).toBeNull(); + }); + + it("should return null if an error occurs while loading state", async () => { + (AsyncStorage.getItem as jest.Mock).mockRejectedValueOnce(new Error("Failed to load")); + const result = await loadState("SLOT1"); + expect(result).toBeNull(); + }); +});