|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { useDestructiveAction } from './useDestructiveAction'; |
| 5 | +import { useDestructiveActionStore } from './useDestructiveActionStore'; |
| 6 | + |
| 7 | +import type { DestructiveAction, DestructiveActionOptions, DestructiveActionParams } from './useDestructiveActionStore'; |
| 8 | + |
| 9 | +vi.mock('./useDestructiveActionStore', () => ({ |
| 10 | + useDestructiveActionStore: vi.fn() |
| 11 | +})); |
| 12 | + |
| 13 | +describe('useDestructiveAction', () => { |
| 14 | + const mockAddPendingDestructiveAction = vi.fn(); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + (useDestructiveActionStore as any).mockImplementation((selector: any) => { |
| 19 | + const store = { |
| 20 | + addPendingDestructiveAction: mockAddPendingDestructiveAction |
| 21 | + }; |
| 22 | + return selector(store); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('useDestructiveAction()', () => { |
| 27 | + it('should return a function that accepts action and options', () => { |
| 28 | + const { result } = renderHook(() => useDestructiveAction()); |
| 29 | + expect(typeof result.current).toBe('function'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should call addPendingDestructiveAction with action only', () => { |
| 33 | + const { result } = renderHook(() => useDestructiveAction()); |
| 34 | + const testAction: DestructiveAction = vi.fn(); |
| 35 | + |
| 36 | + act(() => { |
| 37 | + result.current(testAction); |
| 38 | + }); |
| 39 | + |
| 40 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(testAction, undefined); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should call addPendingDestructiveAction with action and options', () => { |
| 44 | + const { result } = renderHook(() => useDestructiveAction()); |
| 45 | + const testAction: DestructiveAction = vi.fn(); |
| 46 | + const options: DestructiveActionOptions = { |
| 47 | + description: 'This is a test', |
| 48 | + title: 'Test Action' |
| 49 | + }; |
| 50 | + |
| 51 | + act(() => { |
| 52 | + result.current(testAction, options); |
| 53 | + }); |
| 54 | + |
| 55 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(testAction, options); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('useDestructiveAction(action)', () => { |
| 60 | + it('should return a function that calls the action with provided args', () => { |
| 61 | + const testAction = vi.fn(); |
| 62 | + const { result } = renderHook(() => useDestructiveAction(testAction)); |
| 63 | + |
| 64 | + const arg1 = 'test'; |
| 65 | + const arg2 = 123; |
| 66 | + |
| 67 | + act(() => { |
| 68 | + result.current(arg1, arg2); |
| 69 | + }); |
| 70 | + |
| 71 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(expect.any(Function), {}); |
| 72 | + |
| 73 | + // Test that the wrapped function calls the original action with args |
| 74 | + const wrappedAction = mockAddPendingDestructiveAction.mock.calls[0]![0]; |
| 75 | + wrappedAction(); |
| 76 | + expect(testAction).toHaveBeenCalledWith(arg1, arg2); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should work with no arguments', () => { |
| 80 | + const testAction = vi.fn(); |
| 81 | + const { result } = renderHook(() => useDestructiveAction(testAction)); |
| 82 | + |
| 83 | + act(() => { |
| 84 | + result.current(); |
| 85 | + }); |
| 86 | + |
| 87 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(expect.any(Function), {}); |
| 88 | + |
| 89 | + const wrappedAction = mockAddPendingDestructiveAction.mock.calls[0]![0]; |
| 90 | + wrappedAction(); |
| 91 | + expect(testAction).toHaveBeenCalledWith(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('useDestructiveAction(params)', () => { |
| 96 | + it('should return a function that calls the action with provided args and passes options', () => { |
| 97 | + const testAction = vi.fn(); |
| 98 | + const params: DestructiveActionParams<[string, number]> = { |
| 99 | + action: testAction, |
| 100 | + description: 'This is a test', |
| 101 | + title: 'Test Action' |
| 102 | + }; |
| 103 | + const { result } = renderHook(() => useDestructiveAction(params)); |
| 104 | + |
| 105 | + const arg1 = 'test'; |
| 106 | + const arg2 = 123; |
| 107 | + |
| 108 | + act(() => { |
| 109 | + result.current(arg1, arg2); |
| 110 | + }); |
| 111 | + |
| 112 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(expect.any(Function), { |
| 113 | + description: 'This is a test', |
| 114 | + title: 'Test Action' |
| 115 | + }); |
| 116 | + |
| 117 | + // test that the wrapped function calls the original action with args |
| 118 | + const wrappedAction = mockAddPendingDestructiveAction.mock.calls[0]![0]; |
| 119 | + wrappedAction(); |
| 120 | + expect(testAction).toHaveBeenCalledWith(arg1, arg2); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should work with only action in params', () => { |
| 124 | + const testAction = vi.fn(); |
| 125 | + const params: DestructiveActionParams<[]> = { |
| 126 | + action: testAction |
| 127 | + }; |
| 128 | + const { result } = renderHook(() => useDestructiveAction(params)); |
| 129 | + |
| 130 | + act(() => { |
| 131 | + result.current(); |
| 132 | + }); |
| 133 | + |
| 134 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(expect.any(Function), {}); |
| 135 | + |
| 136 | + const wrappedAction = mockAddPendingDestructiveAction.mock.calls[0]![0]; |
| 137 | + wrappedAction(); |
| 138 | + expect(testAction).toHaveBeenCalledWith(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should work with partial options in params', () => { |
| 142 | + const testAction = vi.fn(); |
| 143 | + const params: DestructiveActionParams<[string]> = { |
| 144 | + action: testAction, |
| 145 | + title: 'Only Title' |
| 146 | + }; |
| 147 | + const { result } = renderHook(() => useDestructiveAction(params)); |
| 148 | + |
| 149 | + act(() => { |
| 150 | + result.current('test'); |
| 151 | + }); |
| 152 | + |
| 153 | + expect(mockAddPendingDestructiveAction).toHaveBeenCalledWith(expect.any(Function), { |
| 154 | + title: 'Only Title' |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('callback stability', () => { |
| 160 | + it('should return the same callback function when dependencies do not change', () => { |
| 161 | + const testAction = vi.fn(); |
| 162 | + const { rerender, result } = renderHook(() => useDestructiveAction(testAction)); |
| 163 | + |
| 164 | + const firstCallback = result.current; |
| 165 | + rerender(); |
| 166 | + const secondCallback = result.current; |
| 167 | + |
| 168 | + expect(firstCallback).toBe(secondCallback); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should return a new callback function when action changes', () => { |
| 172 | + const testAction1 = vi.fn(); |
| 173 | + const testAction2 = vi.fn(); |
| 174 | + let action = testAction1; |
| 175 | + |
| 176 | + const { rerender, result } = renderHook(() => useDestructiveAction(action)); |
| 177 | + |
| 178 | + const firstCallback = result.current; |
| 179 | + |
| 180 | + action = testAction2; |
| 181 | + rerender(); |
| 182 | + |
| 183 | + const secondCallback = result.current; |
| 184 | + |
| 185 | + expect(firstCallback).not.toBe(secondCallback); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should return a new callback function when params change', () => { |
| 189 | + const testAction = vi.fn(); |
| 190 | + let params: DestructiveActionParams<[]> = { action: testAction, title: 'First' }; |
| 191 | + |
| 192 | + const { rerender, result } = renderHook(() => useDestructiveAction(params)); |
| 193 | + |
| 194 | + const firstCallback = result.current; |
| 195 | + |
| 196 | + params = { action: testAction, title: 'Second' }; |
| 197 | + rerender(); |
| 198 | + |
| 199 | + const secondCallback = result.current; |
| 200 | + |
| 201 | + expect(firstCallback).not.toBe(secondCallback); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + describe('store integration', () => { |
| 206 | + it('should call useDestructiveActionStore with correct selector', () => { |
| 207 | + renderHook(() => useDestructiveAction()); |
| 208 | + |
| 209 | + expect(useDestructiveActionStore).toHaveBeenCalledWith(expect.any(Function)); |
| 210 | + |
| 211 | + // Test the selector function |
| 212 | + const selector = (useDestructiveActionStore as any).mock.calls[0][0]; |
| 213 | + const mockStore = { |
| 214 | + addPendingDestructiveAction: mockAddPendingDestructiveAction, |
| 215 | + otherProperty: 'should not be selected' |
| 216 | + }; |
| 217 | + |
| 218 | + expect(selector(mockStore)).toBe(mockAddPendingDestructiveAction); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments