|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 6 | +import { createRoot, type Root } from 'react-dom/client' |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +const { linkPrefetch } = vi.hoisted(() => ({ |
| 10 | + linkPrefetch: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('next/link', () => ({ |
| 14 | + default: ({ |
| 15 | + href, |
| 16 | + children, |
| 17 | + prefetch, |
| 18 | + ...props |
| 19 | + }: { |
| 20 | + href: string |
| 21 | + children: React.ReactNode |
| 22 | + prefetch?: boolean |
| 23 | + }) => { |
| 24 | + linkPrefetch(prefetch) |
| 25 | + return ( |
| 26 | + <a href={href} {...props}> |
| 27 | + {children} |
| 28 | + </a> |
| 29 | + ) |
| 30 | + }, |
| 31 | +})) |
| 32 | + |
| 33 | +import { ChatNavigationLink } from '@/app/workspace/[workspaceId]/w/components/sidebar/components/chat-navigation-link/chat-navigation-link' |
| 34 | + |
| 35 | +describe('ChatNavigationLink', () => { |
| 36 | + let container: HTMLDivElement |
| 37 | + let queryClient: QueryClient |
| 38 | + let root: Root |
| 39 | + let prefetchQuery: ReturnType<typeof vi.spyOn> |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 43 | + vi.useFakeTimers() |
| 44 | + linkPrefetch.mockReset() |
| 45 | + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 46 | + prefetchQuery = vi.spyOn(queryClient, 'prefetchQuery').mockResolvedValue() |
| 47 | + container = document.createElement('div') |
| 48 | + document.body.appendChild(container) |
| 49 | + root = createRoot(container) |
| 50 | + }) |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + act(() => root.unmount()) |
| 54 | + container.remove() |
| 55 | + queryClient.clear() |
| 56 | + vi.useRealTimers() |
| 57 | + }) |
| 58 | + |
| 59 | + function renderLink(chatId = 'chat-1', isCurrentRoute = false) { |
| 60 | + act(() => { |
| 61 | + root.render( |
| 62 | + <QueryClientProvider client={queryClient}> |
| 63 | + <ChatNavigationLink |
| 64 | + chatId={chatId} |
| 65 | + href={`/workspace/ws-1/chat/${chatId}`} |
| 66 | + isCurrentRoute={isCurrentRoute} |
| 67 | + > |
| 68 | + Open chat |
| 69 | + </ChatNavigationLink> |
| 70 | + </QueryClientProvider> |
| 71 | + ) |
| 72 | + }) |
| 73 | + const link = container.querySelector('a') |
| 74 | + if (!link) throw new Error('chat link not rendered') |
| 75 | + return link |
| 76 | + } |
| 77 | + |
| 78 | + it('prefetches the route and exact history after deliberate pointer intent', () => { |
| 79 | + const link = renderLink() |
| 80 | + |
| 81 | + act(() => { |
| 82 | + link.dispatchEvent(new MouseEvent('mouseover', { bubbles: true })) |
| 83 | + vi.advanceTimersByTime(79) |
| 84 | + }) |
| 85 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 86 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 87 | + |
| 88 | + act(() => vi.advanceTimersByTime(1)) |
| 89 | + |
| 90 | + expect(linkPrefetch).toHaveBeenLastCalledWith(true) |
| 91 | + expect(prefetchQuery).toHaveBeenCalledWith( |
| 92 | + expect.objectContaining({ |
| 93 | + queryKey: ['mothership-chats', 'detail', 'chat-1'], |
| 94 | + staleTime: 30_000, |
| 95 | + }) |
| 96 | + ) |
| 97 | + |
| 98 | + act(() => link.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }))) |
| 99 | + |
| 100 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 101 | + expect(prefetchQuery).toHaveBeenCalledTimes(1) |
| 102 | + }) |
| 103 | + |
| 104 | + it('cancels drive-by hover prefetches', () => { |
| 105 | + const link = renderLink() |
| 106 | + |
| 107 | + act(() => { |
| 108 | + link.dispatchEvent(new MouseEvent('mouseover', { bubbles: true })) |
| 109 | + link.dispatchEvent(new MouseEvent('mouseout', { bubbles: true })) |
| 110 | + vi.runAllTimers() |
| 111 | + }) |
| 112 | + |
| 113 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 114 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | + |
| 117 | + it('prefetches immediately for keyboard focus without fetching a new-chat history', () => { |
| 118 | + const link = renderLink('new') |
| 119 | + |
| 120 | + act(() => link.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))) |
| 121 | + |
| 122 | + expect(linkPrefetch).toHaveBeenLastCalledWith(true) |
| 123 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 124 | + }) |
| 125 | + |
| 126 | + it('does not treat touch scrolling as navigation intent', () => { |
| 127 | + const link = renderLink() |
| 128 | + |
| 129 | + act(() => link.dispatchEvent(new TouchEvent('touchstart', { bubbles: true }))) |
| 130 | + |
| 131 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 132 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 133 | + }) |
| 134 | + |
| 135 | + it('does not prefetch the chat that is already open', () => { |
| 136 | + const link = renderLink('chat-1', true) |
| 137 | + |
| 138 | + act(() => link.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))) |
| 139 | + |
| 140 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 141 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 142 | + }) |
| 143 | + |
| 144 | + it('does not prefetch when the click is canceled or opens another browsing context', () => { |
| 145 | + const canceledLink = renderLink() |
| 146 | + |
| 147 | + act(() => { |
| 148 | + root.render( |
| 149 | + <QueryClientProvider client={queryClient}> |
| 150 | + <ChatNavigationLink |
| 151 | + chatId='chat-1' |
| 152 | + href='/workspace/ws-1/chat/chat-1' |
| 153 | + onClick={(event) => event.preventDefault()} |
| 154 | + > |
| 155 | + Open chat |
| 156 | + </ChatNavigationLink> |
| 157 | + </QueryClientProvider> |
| 158 | + ) |
| 159 | + }) |
| 160 | + act(() => { |
| 161 | + canceledLink.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true })) |
| 162 | + }) |
| 163 | + |
| 164 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 165 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 166 | + |
| 167 | + act(() => { |
| 168 | + root.render( |
| 169 | + <QueryClientProvider client={queryClient}> |
| 170 | + <ChatNavigationLink chatId='chat-1' href='/workspace/ws-1/chat/chat-1'> |
| 171 | + Open chat |
| 172 | + </ChatNavigationLink> |
| 173 | + </QueryClientProvider> |
| 174 | + ) |
| 175 | + }) |
| 176 | + const modifiedLink = container.querySelector('a') |
| 177 | + if (!modifiedLink) throw new Error('chat link not rendered') |
| 178 | + act(() => { |
| 179 | + modifiedLink.dispatchEvent( |
| 180 | + new MouseEvent('click', { bubbles: true, cancelable: true, metaKey: true }) |
| 181 | + ) |
| 182 | + }) |
| 183 | + |
| 184 | + expect(linkPrefetch).toHaveBeenLastCalledWith(false) |
| 185 | + expect(prefetchQuery).not.toHaveBeenCalled() |
| 186 | + }) |
| 187 | +}) |
0 commit comments