-
Notifications
You must be signed in to change notification settings - Fork 8
π§ͺ Add state management tests for useHotelStore #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chuanman2707
wants to merge
3
commits into
main
Choose a base branch
from
testing-use-hotel-store-13810137760556023977
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { describe, expect, it, beforeEach } from "vitest"; | ||
| import { useHotelStore } from "@/stores/useHotelStore"; | ||
| import { invoke, clearMockResponses, setMockResponse } from "@test-mocks/tauri-core"; | ||
| import { createAllRooms, createStats } from "../helpers/mock-data"; | ||
|
|
||
| describe("useHotelStore", () => { | ||
| beforeEach(() => { | ||
| clearMockResponses(); | ||
| invoke.mockClear(); | ||
| }); | ||
|
|
||
| it("has expected initial state", () => { | ||
| const state = useHotelStore.getInitialState(); | ||
| expect(state.rooms).toEqual([]); | ||
| expect(state.stats).toBeNull(); | ||
| expect(state.activeTab).toBe("dashboard"); | ||
| expect(state.loading).toBe(false); | ||
| expect(state.isCheckinOpen).toBe(false); | ||
| expect(state.checkinRoomId).toBeNull(); | ||
| expect(state.isGroupCheckinOpen).toBe(false); | ||
| expect(state.groups).toEqual([]); | ||
| expect(state.housekeepingTasks).toEqual([]); | ||
| expect(state.roomDetail).toBeNull(); | ||
| }); | ||
|
|
||
| it("fetchRooms updates the rooms state", async () => { | ||
| const mockRooms = createAllRooms(); | ||
| setMockResponse("get_rooms", () => mockRooms); | ||
|
|
||
| await useHotelStore.getState().fetchRooms(); | ||
|
|
||
| expect(invoke).toHaveBeenCalledWith("get_rooms"); | ||
| expect(useHotelStore.getState().rooms).toEqual(mockRooms); | ||
| }); | ||
|
|
||
| it("fetchStats updates the stats state", async () => { | ||
| const mockStats = createStats(); | ||
| setMockResponse("get_dashboard_stats", () => mockStats); | ||
|
|
||
| await useHotelStore.getState().fetchStats(); | ||
|
|
||
| expect(invoke).toHaveBeenCalledWith("get_dashboard_stats"); | ||
| expect(useHotelStore.getState().stats).toEqual(mockStats); | ||
| }); | ||
|
|
||
| it("checkIn executes successfully and updates data", async () => { | ||
| // We expect checkIn to call `check_in`, `get_rooms`, and `get_dashboard_stats` | ||
| setMockResponse("check_in", () => undefined); | ||
| setMockResponse("get_rooms", () => createAllRooms()); | ||
| setMockResponse("get_dashboard_stats", () => createStats()); | ||
|
|
||
| const guests = [{ full_name: "John Doe", doc_number: "123456" }]; | ||
| await useHotelStore.getState().checkIn("1A", guests, 2, 500000, "walk-in", "test note"); | ||
|
|
||
| expect(invoke).toHaveBeenCalledWith("check_in", { | ||
| req: { | ||
| room_id: "1A", | ||
| guests, | ||
| nights: 2, | ||
| source: "walk-in", | ||
| notes: "test note", | ||
| paid_amount: 500000, | ||
| }, | ||
| }); | ||
| expect(invoke).toHaveBeenCalledWith("get_rooms"); | ||
| expect(invoke).toHaveBeenCalledWith("get_dashboard_stats"); | ||
| expect(useHotelStore.getState().loading).toBe(false); | ||
| expect(useHotelStore.getState().activeTab).toBe("dashboard"); | ||
| }); | ||
|
|
||
| it("checkIn handles errors correctly", async () => { | ||
| const mockError = new Error("Check-in failed"); | ||
| setMockResponse("check_in", () => { | ||
| throw mockError; | ||
| }); | ||
|
|
||
| const guests = [{ full_name: "Jane Doe", doc_number: "654321" }]; | ||
|
|
||
| await expect( | ||
| useHotelStore.getState().checkIn("1A", guests, 1) | ||
| ).rejects.toThrow("Check-in failed"); | ||
|
|
||
| expect(useHotelStore.getState().loading).toBe(false); | ||
| }); | ||
|
|
||
| it("checkOut executes successfully and updates data", async () => { | ||
| setMockResponse("check_out", () => undefined); | ||
| setMockResponse("get_rooms", () => createAllRooms()); | ||
| setMockResponse("get_dashboard_stats", () => createStats()); | ||
|
|
||
| await useHotelStore.getState().checkOut("booking-1", 100000); | ||
|
|
||
| expect(invoke).toHaveBeenCalledWith("check_out", { | ||
| req: { | ||
| booking_id: "booking-1", | ||
| final_paid: 100000, | ||
| }, | ||
| }); | ||
| expect(invoke).toHaveBeenCalledWith("get_rooms"); | ||
| expect(invoke).toHaveBeenCalledWith("get_dashboard_stats"); | ||
| expect(useHotelStore.getState().loading).toBe(false); | ||
| expect(useHotelStore.getState().activeTab).toBe("dashboard"); | ||
| }); | ||
|
|
||
| it("checkOut handles errors correctly", async () => { | ||
| const mockError = new Error("Check-out failed"); | ||
| setMockResponse("check_out", () => { | ||
| throw mockError; | ||
| }); | ||
|
|
||
| await expect( | ||
| useHotelStore.getState().checkOut("booking-1") | ||
| ).rejects.toThrow("Check-out failed"); | ||
|
|
||
| expect(useHotelStore.getState().loading).toBe(false); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useHotelStoreis a singleton Zustand store, but thisbeforeEachonly clears mocked invoke handlers and never restores store state. Because tests likefetchRooms,checkIn, andcheckOutmutate global store fields, later tests can inherit prior state and either fail intermittently or pass while missing regressions (for example,activeTabassertions can stay green simply because the previous/default value is already"dashboard"). Add auseHotelStore.setState(...)reset to the initial state inbeforeEachto keep tests isolated.Useful? React with πΒ / π.