|
| 1 | +import { act, renderHook } from "@testing-library/react"; |
| 2 | +import React from "react"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { createStore, useStore } from "zustand"; |
| 5 | +import type { BookingOutput } from "../types"; |
| 6 | + |
| 7 | +type BookingDetailsSheetStore = { |
| 8 | + selectedBookingUid: string | null; |
| 9 | + bookings: BookingOutput[]; |
| 10 | + setSelectedBookingUid: (uid: string | null) => void; |
| 11 | + setBookings: (bookings: BookingOutput[]) => void; |
| 12 | + getSelectedBooking: () => BookingOutput | null; |
| 13 | +}; |
| 14 | + |
| 15 | +function createTestStore(initialBookings: BookingOutput[] = []) { |
| 16 | + return createStore<BookingDetailsSheetStore>((set, get) => ({ |
| 17 | + selectedBookingUid: null, |
| 18 | + bookings: initialBookings, |
| 19 | + setSelectedBookingUid: (uid) => set({ selectedBookingUid: uid }), |
| 20 | + setBookings: (bookings) => set({ bookings }), |
| 21 | + getSelectedBooking: () => { |
| 22 | + const state = get(); |
| 23 | + if (!state.selectedBookingUid) return null; |
| 24 | + return state.bookings.find((booking) => booking.uid === state.selectedBookingUid) ?? null; |
| 25 | + }, |
| 26 | + })); |
| 27 | +} |
| 28 | + |
| 29 | +const makeBooking = (uid: string, title: string): BookingOutput => |
| 30 | + ({ |
| 31 | + uid, |
| 32 | + id: Number(uid), |
| 33 | + title, |
| 34 | + startTime: new Date().toISOString(), |
| 35 | + endTime: new Date().toISOString(), |
| 36 | + status: "ACCEPTED", |
| 37 | + attendees: [], |
| 38 | + metadata: null, |
| 39 | + location: null, |
| 40 | + rescheduled: false, |
| 41 | + recurringEventId: null, |
| 42 | + fromReschedule: null, |
| 43 | + responses: {}, |
| 44 | + payment: [], |
| 45 | + eventType: null, |
| 46 | + user: null, |
| 47 | + assignmentReasonSortedByCreatedAt: [], |
| 48 | + }) as unknown as BookingOutput; |
| 49 | + |
| 50 | +describe("BookingDetailsSheet transition behavior", () => { |
| 51 | + const bookingA = makeBooking("uid-a", "Booking A"); |
| 52 | + const bookingB = makeBooking("uid-b", "Booking B"); |
| 53 | + |
| 54 | + it("getSelectedBooking returns null when no uid is selected", () => { |
| 55 | + const store = createTestStore([bookingA, bookingB]); |
| 56 | + expect(store.getState().getSelectedBooking()).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("getSelectedBooking returns the correct booking when uid is set", () => { |
| 60 | + const store = createTestStore([bookingA, bookingB]); |
| 61 | + store.getState().setSelectedBookingUid("uid-a"); |
| 62 | + expect(store.getState().getSelectedBooking()?.uid).toBe("uid-a"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("getSelectedBooking returns null when uid does not match any booking", () => { |
| 66 | + const store = createTestStore([bookingA, bookingB]); |
| 67 | + store.getState().setSelectedBookingUid("uid-nonexistent"); |
| 68 | + expect(store.getState().getSelectedBooking()).toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("switching between bookings updates getSelectedBooking immediately", () => { |
| 72 | + const store = createTestStore([bookingA, bookingB]); |
| 73 | + store.getState().setSelectedBookingUid("uid-a"); |
| 74 | + expect(store.getState().getSelectedBooking()?.title).toBe("Booking A"); |
| 75 | + |
| 76 | + store.getState().setSelectedBookingUid("uid-b"); |
| 77 | + expect(store.getState().getSelectedBooking()?.title).toBe("Booking B"); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("lastBookingRef fallback logic", () => { |
| 81 | + it("preserves last booking when getSelectedBooking returns null during transition", () => { |
| 82 | + let lastBooking: BookingOutput | null = null; |
| 83 | + let selectedBookingUid: string | null = null; |
| 84 | + |
| 85 | + const store = createTestStore([bookingA, bookingB]); |
| 86 | + store.getState().setSelectedBookingUid("uid-a"); |
| 87 | + |
| 88 | + const booking = store.getState().getSelectedBooking(); |
| 89 | + if (booking) { |
| 90 | + lastBooking = booking; |
| 91 | + } |
| 92 | + |
| 93 | + store.getState().setSelectedBookingUid("uid-nonexistent"); |
| 94 | + selectedBookingUid = store.getState().selectedBookingUid; |
| 95 | + |
| 96 | + const currentBooking = store.getState().getSelectedBooking(); |
| 97 | + const displayBooking = currentBooking ?? lastBooking; |
| 98 | + |
| 99 | + expect(currentBooking).toBeNull(); |
| 100 | + expect(selectedBookingUid).toBe("uid-nonexistent"); |
| 101 | + expect(displayBooking?.uid).toBe("uid-a"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("clears lastBooking when selectedBookingUid is explicitly null", () => { |
| 105 | + let lastBooking: BookingOutput | null = null; |
| 106 | + |
| 107 | + const store = createTestStore([bookingA]); |
| 108 | + store.getState().setSelectedBookingUid("uid-a"); |
| 109 | + |
| 110 | + const booking = store.getState().getSelectedBooking(); |
| 111 | + if (booking) { |
| 112 | + lastBooking = booking; |
| 113 | + } |
| 114 | + |
| 115 | + store.getState().setSelectedBookingUid(null); |
| 116 | + const selectedBookingUid = store.getState().selectedBookingUid; |
| 117 | + |
| 118 | + if (!selectedBookingUid) { |
| 119 | + lastBooking = null; |
| 120 | + } |
| 121 | + |
| 122 | + const currentBooking = store.getState().getSelectedBooking(); |
| 123 | + const displayBooking = currentBooking ?? lastBooking; |
| 124 | + |
| 125 | + expect(displayBooking).toBeNull(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("updates lastBooking when new booking is found", () => { |
| 129 | + let lastBooking: BookingOutput | null = null; |
| 130 | + |
| 131 | + const store = createTestStore([bookingA, bookingB]); |
| 132 | + |
| 133 | + store.getState().setSelectedBookingUid("uid-a"); |
| 134 | + const bookingAResult = store.getState().getSelectedBooking(); |
| 135 | + if (bookingAResult) lastBooking = bookingAResult; |
| 136 | + expect(lastBooking?.uid).toBe("uid-a"); |
| 137 | + |
| 138 | + store.getState().setSelectedBookingUid("uid-b"); |
| 139 | + const bookingBResult = store.getState().getSelectedBooking(); |
| 140 | + if (bookingBResult) lastBooking = bookingBResult; |
| 141 | + expect(lastBooking?.uid).toBe("uid-b"); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments