|
| 1 | +import { renderHook, act } from "@testing-library/react"; |
| 2 | +import { useConfirmationDialog } from "./confirmation-dialog.hook"; |
| 3 | +import { createEmptyLookup } from "#common/models"; |
| 4 | + |
| 5 | +describe("useConfirmationDialog", () => { |
| 6 | + it("should initialize with default values", () => { |
| 7 | + // Arrange & Act |
| 8 | + const { result } = renderHook(() => useConfirmationDialog()); |
| 9 | + |
| 10 | + // Assert |
| 11 | + expect(result.current.isOpen).toBe(false); |
| 12 | + expect(result.current.itemToDelete).toEqual(createEmptyLookup()); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should update state when onOpenDialog is called", () => { |
| 16 | + // Arrange |
| 17 | + const { result } = renderHook(() => useConfirmationDialog()); |
| 18 | + const mockItem = { id: "1", name: "Test Item" }; |
| 19 | + |
| 20 | + // Act |
| 21 | + act(() => { |
| 22 | + result.current.onOpenDialog(mockItem); |
| 23 | + }); |
| 24 | + |
| 25 | + // Assert |
| 26 | + expect(result.current.isOpen).toBe(true); |
| 27 | + expect(result.current.itemToDelete).toEqual(mockItem); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should reset itemToDelete when onAccept is called", () => { |
| 31 | + // Arrange |
| 32 | + const { result } = renderHook(() => useConfirmationDialog()); |
| 33 | + const mockItem = { id: "1", name: "Test Item" }; |
| 34 | + |
| 35 | + // Act |
| 36 | + act(() => { |
| 37 | + result.current.onOpenDialog(mockItem); |
| 38 | + }); |
| 39 | + act(() => { |
| 40 | + result.current.onAccept(); |
| 41 | + }); |
| 42 | + |
| 43 | + // Assert |
| 44 | + expect(result.current.itemToDelete).toEqual(createEmptyLookup()); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should close dialog when onClose is called", () => { |
| 48 | + // Arrange |
| 49 | + const { result } = renderHook(() => useConfirmationDialog()); |
| 50 | + const mockItem = { id: "1", name: "Test Item" }; |
| 51 | + |
| 52 | + // Act |
| 53 | + act(() => { |
| 54 | + result.current.onOpenDialog(mockItem); |
| 55 | + }); |
| 56 | + act(() => { |
| 57 | + result.current.onClose(); |
| 58 | + }); |
| 59 | + |
| 60 | + // Assert |
| 61 | + expect(result.current.isOpen).toBe(false); |
| 62 | + expect(result.current.itemToDelete).toEqual(mockItem); // itemToDelete remains unchanged |
| 63 | + }); |
| 64 | + |
| 65 | + it("should maintain state consistency through multiple operations", () => { |
| 66 | + // Arrange |
| 67 | + const { result } = renderHook(() => useConfirmationDialog()); |
| 68 | + const mockItem1 = { id: "1", name: "Test Item 1" }; |
| 69 | + const mockItem2 = { id: "2", name: "Test Item 2" }; |
| 70 | + |
| 71 | + // Act & Assert - First operation |
| 72 | + act(() => { |
| 73 | + result.current.onOpenDialog(mockItem1); |
| 74 | + }); |
| 75 | + expect(result.current.isOpen).toBe(true); |
| 76 | + expect(result.current.itemToDelete).toEqual(mockItem1); |
| 77 | + |
| 78 | + // Act & Assert - Close dialog |
| 79 | + act(() => { |
| 80 | + result.current.onClose(); |
| 81 | + }); |
| 82 | + expect(result.current.isOpen).toBe(false); |
| 83 | + expect(result.current.itemToDelete).toEqual(mockItem1); |
| 84 | + |
| 85 | + // Act & Assert - Open with different item |
| 86 | + act(() => { |
| 87 | + result.current.onOpenDialog(mockItem2); |
| 88 | + }); |
| 89 | + expect(result.current.isOpen).toBe(true); |
| 90 | + expect(result.current.itemToDelete).toEqual(mockItem2); |
| 91 | + }); |
| 92 | +}); |
0 commit comments