|
| 1 | +import {type DocumentHandle, mediaLibrarySource} from '@sanity/sdk' |
| 2 | +import {renderHook} from '@testing-library/react' |
| 3 | +import {beforeEach, describe, expect, it, vi} from 'vitest' |
| 4 | + |
| 5 | +import {type DocumentHandleWithSource} from './types' |
| 6 | +import {useDispatchIntent} from './useDispatchIntent' |
| 7 | + |
| 8 | +// Mock the useWindowConnection hook |
| 9 | +const mockSendMessage = vi.fn() |
| 10 | +vi.mock('../comlink/useWindowConnection', () => ({ |
| 11 | + useWindowConnection: vi.fn(() => ({ |
| 12 | + sendMessage: mockSendMessage, |
| 13 | + })), |
| 14 | +})) |
| 15 | + |
| 16 | +describe('useDispatchIntent', () => { |
| 17 | + const mockDocumentHandle: DocumentHandle = { |
| 18 | + documentId: 'test-document-id', |
| 19 | + documentType: 'test-document-type', |
| 20 | + projectId: 'test-project-id', |
| 21 | + dataset: 'test-dataset', |
| 22 | + } |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks() |
| 26 | + // Reset mock implementation to default behavior |
| 27 | + mockSendMessage.mockImplementation(() => {}) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should return dispatchIntent function', () => { |
| 31 | + const {result} = renderHook(() => |
| 32 | + useDispatchIntent({action: 'edit', documentHandle: mockDocumentHandle}), |
| 33 | + ) |
| 34 | + |
| 35 | + expect(result.current).toEqual({ |
| 36 | + dispatchIntent: expect.any(Function), |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should throw error when neither action nor intentId is provided', () => { |
| 41 | + const {result} = renderHook(() => |
| 42 | + // @ts-expect-error - Testing runtime error when neither is provided |
| 43 | + useDispatchIntent({documentHandle: mockDocumentHandle}), |
| 44 | + ) |
| 45 | + |
| 46 | + expect(() => result.current.dispatchIntent()).toThrow( |
| 47 | + 'useDispatchIntent: Either `action` or `intentId` must be provided.', |
| 48 | + ) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should handle errors gracefully', () => { |
| 52 | + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 53 | + mockSendMessage.mockImplementation(() => { |
| 54 | + throw new Error('Test error') |
| 55 | + }) |
| 56 | + |
| 57 | + const {result} = renderHook(() => |
| 58 | + useDispatchIntent({action: 'edit', documentHandle: mockDocumentHandle}), |
| 59 | + ) |
| 60 | + |
| 61 | + expect(() => result.current.dispatchIntent()).toThrow('Test error') |
| 62 | + expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to dispatch intent:', expect.any(Error)) |
| 63 | + |
| 64 | + consoleErrorSpy.mockRestore() |
| 65 | + }) |
| 66 | + |
| 67 | + it('should use memoized dispatchIntent function', () => { |
| 68 | + const {result, rerender} = renderHook(({params}) => useDispatchIntent(params), { |
| 69 | + initialProps: {params: {action: 'edit' as const, documentHandle: mockDocumentHandle}}, |
| 70 | + }) |
| 71 | + |
| 72 | + const firstDispatchIntent = result.current.dispatchIntent |
| 73 | + |
| 74 | + // Rerender with the same params |
| 75 | + rerender({params: {action: 'edit' as const, documentHandle: mockDocumentHandle}}) |
| 76 | + |
| 77 | + expect(result.current.dispatchIntent).toBe(firstDispatchIntent) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should create new dispatchIntent function when documentHandle changes', () => { |
| 81 | + const {result, rerender} = renderHook(({params}) => useDispatchIntent(params), { |
| 82 | + initialProps: {params: {action: 'edit' as const, documentHandle: mockDocumentHandle}}, |
| 83 | + }) |
| 84 | + |
| 85 | + const firstDispatchIntent = result.current.dispatchIntent |
| 86 | + |
| 87 | + const newDocumentHandle: DocumentHandle = { |
| 88 | + documentId: 'new-document-id', |
| 89 | + documentType: 'new-document-type', |
| 90 | + projectId: 'new-project-id', |
| 91 | + dataset: 'new-dataset', |
| 92 | + } |
| 93 | + |
| 94 | + rerender({params: {action: 'edit' as const, documentHandle: newDocumentHandle}}) |
| 95 | + |
| 96 | + expect(result.current.dispatchIntent).not.toBe(firstDispatchIntent) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should warn if both action and intentId are provided', () => { |
| 100 | + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 101 | + const {result} = renderHook(() => |
| 102 | + useDispatchIntent({ |
| 103 | + action: 'edit' as const, |
| 104 | + intentId: 'custom-intent' as never, // test runtime error when both are provided |
| 105 | + documentHandle: mockDocumentHandle, |
| 106 | + }), |
| 107 | + ) |
| 108 | + result.current.dispatchIntent() |
| 109 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 110 | + 'useDispatchIntent: Both `action` and `intentId` were provided. Using `intentId` and ignoring `action`.', |
| 111 | + ) |
| 112 | + consoleWarnSpy.mockRestore() |
| 113 | + }) |
| 114 | + |
| 115 | + it('should send intent message with action and params when both are provided', () => { |
| 116 | + const intentParams = {view: 'editor', tab: 'content'} |
| 117 | + const {result} = renderHook(() => |
| 118 | + useDispatchIntent({ |
| 119 | + action: 'edit', |
| 120 | + documentHandle: mockDocumentHandle, |
| 121 | + parameters: intentParams, |
| 122 | + }), |
| 123 | + ) |
| 124 | + |
| 125 | + result.current.dispatchIntent() |
| 126 | + |
| 127 | + expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', { |
| 128 | + action: 'edit', |
| 129 | + document: { |
| 130 | + id: 'test-document-id', |
| 131 | + type: 'test-document-type', |
| 132 | + }, |
| 133 | + resource: { |
| 134 | + id: 'test-project-id.test-dataset', |
| 135 | + }, |
| 136 | + parameters: intentParams, |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + it('should send intent message with intentId and params when both are provided', () => { |
| 141 | + const intentParams = {view: 'editor', tab: 'content'} |
| 142 | + const {result} = renderHook(() => |
| 143 | + useDispatchIntent({ |
| 144 | + intentId: 'custom-intent', |
| 145 | + documentHandle: mockDocumentHandle, |
| 146 | + parameters: intentParams, |
| 147 | + }), |
| 148 | + ) |
| 149 | + |
| 150 | + result.current.dispatchIntent() |
| 151 | + |
| 152 | + expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', { |
| 153 | + intentId: 'custom-intent', |
| 154 | + document: { |
| 155 | + id: 'test-document-id', |
| 156 | + type: 'test-document-type', |
| 157 | + }, |
| 158 | + resource: { |
| 159 | + id: 'test-project-id.test-dataset', |
| 160 | + }, |
| 161 | + parameters: intentParams, |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + it('should send intent message with media library source', () => { |
| 166 | + const mockMediaLibraryHandle: DocumentHandleWithSource = { |
| 167 | + documentId: 'test-asset-id', |
| 168 | + documentType: 'sanity.asset', |
| 169 | + source: mediaLibrarySource('mlPGY7BEqt52'), |
| 170 | + } |
| 171 | + |
| 172 | + const {result} = renderHook(() => |
| 173 | + useDispatchIntent({ |
| 174 | + action: 'edit', |
| 175 | + documentHandle: mockMediaLibraryHandle, |
| 176 | + }), |
| 177 | + ) |
| 178 | + |
| 179 | + result.current.dispatchIntent() |
| 180 | + |
| 181 | + expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', { |
| 182 | + action: 'edit', |
| 183 | + document: { |
| 184 | + id: 'test-asset-id', |
| 185 | + type: 'sanity.asset', |
| 186 | + }, |
| 187 | + resource: { |
| 188 | + id: 'mlPGY7BEqt52', |
| 189 | + type: 'mediaLibrary', |
| 190 | + }, |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + describe('error handling', () => { |
| 195 | + it('should throw error when neither source nor projectId/dataset is provided', () => { |
| 196 | + const invalidHandle = { |
| 197 | + documentId: 'test-document-id', |
| 198 | + documentType: 'test-document-type', |
| 199 | + } |
| 200 | + |
| 201 | + const {result} = renderHook(() => |
| 202 | + useDispatchIntent({ |
| 203 | + action: 'edit', |
| 204 | + documentHandle: invalidHandle as unknown as DocumentHandleWithSource, |
| 205 | + }), |
| 206 | + ) |
| 207 | + |
| 208 | + expect(() => result.current.dispatchIntent()).toThrow( |
| 209 | + 'useDispatchIntent: Either `source` or both `projectId` and `dataset` must be provided in documentHandle.', |
| 210 | + ) |
| 211 | + }) |
| 212 | + }) |
| 213 | +}) |
0 commit comments