|
| 1 | +import {type Status} from '@sanity/comlink' |
| 2 | +import {type DocumentHandle} from '@sanity/sdk' |
| 3 | +import {act, renderHook} from '@testing-library/react' |
| 4 | +import {beforeEach, describe, expect, it, vi} from 'vitest' |
| 5 | + |
| 6 | +import {useNavigateToStudioDocument} from './useNavigateToStudioDocument' |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +const mockSendMessage = vi.fn() |
| 10 | +const mockFetch = vi.fn() |
| 11 | +let mockWorkspacesByResourceId = {} |
| 12 | +let mockWorkspacesIsConnected = true |
| 13 | +let mockStatusCallback: ((status: Status) => void) | null = null |
| 14 | + |
| 15 | +vi.mock('../comlink/useWindowConnection', () => { |
| 16 | + return { |
| 17 | + useWindowConnection: ({onStatus}: {onStatus?: (status: Status) => void}) => { |
| 18 | + mockStatusCallback = onStatus || null |
| 19 | + return { |
| 20 | + sendMessage: mockSendMessage, |
| 21 | + fetch: mockFetch, |
| 22 | + } |
| 23 | + }, |
| 24 | + } |
| 25 | +}) |
| 26 | + |
| 27 | +vi.mock('./useStudioWorkspacesByResourceId', () => { |
| 28 | + return { |
| 29 | + useStudioWorkspacesByResourceId: () => ({ |
| 30 | + workspacesByResourceId: mockWorkspacesByResourceId, |
| 31 | + error: null, |
| 32 | + isConnected: mockWorkspacesIsConnected, |
| 33 | + }), |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +describe('useNavigateToStudioDocument', () => { |
| 38 | + const mockDocumentHandle: DocumentHandle = { |
| 39 | + _id: 'doc123', |
| 40 | + _type: 'article', |
| 41 | + resourceId: 'document:project1.dataset1:doc123', |
| 42 | + } |
| 43 | + |
| 44 | + const mockWorkspace = { |
| 45 | + name: 'workspace1', |
| 46 | + title: 'Workspace 1', |
| 47 | + basePath: '/workspace1', |
| 48 | + dataset: 'dataset1', |
| 49 | + userApplicationId: 'user1', |
| 50 | + url: 'https://test.sanity.studio', |
| 51 | + _ref: 'workspace123', |
| 52 | + } |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + vi.resetAllMocks() |
| 56 | + mockWorkspacesByResourceId = { |
| 57 | + 'project1:dataset1': [mockWorkspace], |
| 58 | + } |
| 59 | + mockWorkspacesIsConnected = true |
| 60 | + mockStatusCallback = null |
| 61 | + }) |
| 62 | + |
| 63 | + it('returns a function and connection status', () => { |
| 64 | + const {result} = renderHook(() => useNavigateToStudioDocument(mockDocumentHandle)) |
| 65 | + |
| 66 | + // Initially not connected |
| 67 | + expect(result.current.isConnected).toBe(false) |
| 68 | + |
| 69 | + // Simulate connection |
| 70 | + act(() => { |
| 71 | + mockStatusCallback?.('connected') |
| 72 | + }) |
| 73 | + |
| 74 | + expect(result.current).toEqual({ |
| 75 | + navigateToStudioDocument: expect.any(Function), |
| 76 | + isConnected: true, |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('sends correct navigation message when called', () => { |
| 81 | + const {result} = renderHook(() => useNavigateToStudioDocument(mockDocumentHandle)) |
| 82 | + |
| 83 | + // Simulate connection |
| 84 | + act(() => { |
| 85 | + mockStatusCallback?.('connected') |
| 86 | + }) |
| 87 | + |
| 88 | + result.current.navigateToStudioDocument() |
| 89 | + |
| 90 | + expect(mockSendMessage).toHaveBeenCalledWith('core/v1/bridge/navigate-to-resource', { |
| 91 | + resourceId: 'workspace123', |
| 92 | + resourceType: 'studio', |
| 93 | + path: '/intent/edit/id=doc123;type=article', |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + it('does not send message when not connected', () => { |
| 98 | + mockWorkspacesByResourceId = {} |
| 99 | + mockWorkspacesIsConnected = false |
| 100 | + |
| 101 | + const {result} = renderHook(() => useNavigateToStudioDocument(mockDocumentHandle)) |
| 102 | + |
| 103 | + // Simulate connection |
| 104 | + act(() => { |
| 105 | + mockStatusCallback?.('connected') |
| 106 | + }) |
| 107 | + |
| 108 | + result.current.navigateToStudioDocument() |
| 109 | + |
| 110 | + expect(mockSendMessage).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | + |
| 113 | + it('does not send message when no workspace is found', () => { |
| 114 | + mockWorkspacesByResourceId = {} |
| 115 | + mockWorkspacesIsConnected = true |
| 116 | + |
| 117 | + const {result} = renderHook(() => useNavigateToStudioDocument(mockDocumentHandle)) |
| 118 | + |
| 119 | + // Simulate connection |
| 120 | + act(() => { |
| 121 | + mockStatusCallback?.('connected') |
| 122 | + }) |
| 123 | + |
| 124 | + result.current.navigateToStudioDocument() |
| 125 | + |
| 126 | + expect(mockSendMessage).not.toHaveBeenCalled() |
| 127 | + }) |
| 128 | + |
| 129 | + it('handles invalid resourceId format', () => { |
| 130 | + const invalidDocHandle: DocumentHandle = { |
| 131 | + _id: 'doc123', |
| 132 | + _type: 'article', |
| 133 | + resourceId: 'document:project1.invalid:doc123' as `document:${string}.${string}:${string}`, |
| 134 | + } |
| 135 | + |
| 136 | + const {result} = renderHook(() => useNavigateToStudioDocument(invalidDocHandle)) |
| 137 | + |
| 138 | + // Simulate connection |
| 139 | + act(() => { |
| 140 | + mockStatusCallback?.('connected') |
| 141 | + }) |
| 142 | + |
| 143 | + result.current.navigateToStudioDocument() |
| 144 | + |
| 145 | + expect(mockSendMessage).not.toHaveBeenCalled() |
| 146 | + }) |
| 147 | + |
| 148 | + it('warns when multiple workspaces are found', () => { |
| 149 | + const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 150 | + const mockWorkspace2 = {...mockWorkspace, _ref: 'workspace2'} |
| 151 | + |
| 152 | + mockWorkspacesByResourceId = { |
| 153 | + 'project1:dataset1': [mockWorkspace, mockWorkspace2], |
| 154 | + } |
| 155 | + |
| 156 | + const {result} = renderHook(() => useNavigateToStudioDocument(mockDocumentHandle)) |
| 157 | + |
| 158 | + // Simulate connection |
| 159 | + act(() => { |
| 160 | + mockStatusCallback?.('connected') |
| 161 | + }) |
| 162 | + |
| 163 | + result.current.navigateToStudioDocument() |
| 164 | + |
| 165 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 166 | + 'Multiple workspaces found for document', |
| 167 | + mockDocumentHandle.resourceId, |
| 168 | + ) |
| 169 | + expect(mockSendMessage).toHaveBeenCalledWith( |
| 170 | + 'core/v1/bridge/navigate-to-resource', |
| 171 | + expect.objectContaining({ |
| 172 | + resourceId: mockWorkspace._ref, |
| 173 | + }), |
| 174 | + ) |
| 175 | + |
| 176 | + consoleSpy.mockRestore() |
| 177 | + }) |
| 178 | +}) |
0 commit comments