import { renderHook } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { useWoTableRowHandlers } from "@/app/(protected)/workorders/_components/list/table/use-wo-table-row-handlers"; import { applySchedulePatch } from "@/domain/work-orders/use-cases/use-work-order-table-mutations"; const { rescheduleNotify, weekMoveNotify } = vi.hoisted(() => ({ rescheduleNotify: vi.fn(), weekMoveNotify: vi.fn(), })); vi.mock("@/app/(protected)/workorders/_components/list/table/reschedule-notify", () => ({ rescheduleNotify, weekMoveNotify, })); function buildTableData(revealScheduledRow = vi.fn()) { return { dragState: { current: { dragId: null, overId: null } }, setDragViz: vi.fn(), reorderRows: vi.fn(), endDrag: vi.fn(), revealScheduledRow, setSelectedId: vi.fn(), } as never; } describe("useWoTableRowHandlers schedule navigation (SH-240)", () => { it("selects the scheduled weekday in the same week so the row stays visible", () => { const onNavigateToDay = vi.fn(); const onPatch = vi.fn(); const revealScheduledRow = vi.fn(); const { result } = renderHook(() => useWoTableRowHandlers({ tableData: buildTableData(revealScheduledRow), weekMonday: "2026-05-18", onNavigateToDay, onOpenSlideOver: vi.fn(), onOpenMedia: vi.fn(), onPatch, onScheduleHighlight: vi.fn(), }), ); result.current.onScheduleDate("wo-1", "2026-05-21"); expect(onPatch).toHaveBeenCalledWith("wo-1", applySchedulePatch("2026-05-21", false)); expect(onNavigateToDay).toHaveBeenCalledWith("2026-05-21"); expect(revealScheduledRow).toHaveBeenCalledWith("wo-1"); expect(rescheduleNotify).not.toHaveBeenCalled(); }); it("sends cross-week view-it navigation to the target day, not Monday", () => { const onNavigateToDay = vi.fn(); const revealScheduledRow = vi.fn(); const { result } = renderHook(() => useWoTableRowHandlers({ tableData: buildTableData(revealScheduledRow), weekMonday: "2026-05-18", onNavigateToDay, onOpenSlideOver: vi.fn(), onOpenMedia: vi.fn(), onPatch: vi.fn(), onScheduleHighlight: vi.fn(), }), ); result.current.onScheduleDate("wo-1", "2026-05-27"); expect(onNavigateToDay).not.toHaveBeenCalled(); expect(rescheduleNotify).toHaveBeenCalledWith( "2026-05-27", expect.objectContaining({ onNavigate: expect.any(Function) }), ); const { onNavigate } = rescheduleNotify.mock.calls[0][1] as { onNavigate: () => void }; onNavigate(); expect(onNavigateToDay).toHaveBeenCalledWith("2026-05-27"); expect(revealScheduledRow).toHaveBeenCalledWith("wo-1"); }); it("opens Advanced Filters for week-only moves instead of the removed Unscheduled section", () => { const onNavigateToDay = vi.fn(); const onOpenAdvancedFilters = vi.fn(); const onPatch = vi.fn(); const { result } = renderHook(() => useWoTableRowHandlers({ tableData: buildTableData(), weekMonday: "2026-05-18", onNavigateToDay, onOpenAdvancedFilters, onOpenSlideOver: vi.fn(), onOpenMedia: vi.fn(), onPatch, onScheduleHighlight: vi.fn(), }), ); result.current.onScheduleWeek("wo-1", "2026-05-25"); expect(onPatch).toHaveBeenCalledWith( "wo-1", expect.objectContaining({ scheduledOn: "", scheduleWeekOnly: true, targetWeek: "2026-05-25", }), ); expect(weekMoveNotify).toHaveBeenCalled(); const { onNavigate } = weekMoveNotify.mock.calls[0][1] as { onNavigate: () => void }; onNavigate(); expect(onNavigateToDay).toHaveBeenCalledWith("2026-05-25"); expect(onOpenAdvancedFilters).toHaveBeenCalled(); }); });