|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { act, createElement } from 'react'; |
| 4 | +import { createRoot, type Root } from 'react-dom/client'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vite-plus/test'; |
| 6 | +import type { LinkPrefetchValue, VoidRouter } from 'void/pages-client'; |
| 7 | +import { RouterContext } from '../../src/runtime/context.ts'; |
| 8 | +import { Link, type LinkProps } from '../../src/runtime/link.tsx'; |
| 9 | + |
| 10 | +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 11 | + |
| 12 | +const mountedRoots: Array<{ root: Root; container: HTMLDivElement }> = []; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + for (const { root, container } of mountedRoots.splice(0)) { |
| 16 | + act(() => root.unmount()); |
| 17 | + container.remove(); |
| 18 | + } |
| 19 | +}); |
| 20 | + |
| 21 | +function createRouter(defaultStrategy?: LinkPrefetchValue) { |
| 22 | + const prefetch = vi.fn(() => Promise.resolve()); |
| 23 | + const router: VoidRouter = { |
| 24 | + _hoverDelay: 75, |
| 25 | + _prefetchDefaultStrategy: defaultStrategy, |
| 26 | + get url() { |
| 27 | + return new URL(window.location.href); |
| 28 | + }, |
| 29 | + get path() { |
| 30 | + return window.location.pathname; |
| 31 | + }, |
| 32 | + get query() { |
| 33 | + return new URLSearchParams(window.location.search); |
| 34 | + }, |
| 35 | + get params() { |
| 36 | + return {}; |
| 37 | + }, |
| 38 | + visit: vi.fn(), |
| 39 | + refresh: vi.fn(), |
| 40 | + prefetch, |
| 41 | + flush: vi.fn(), |
| 42 | + flushAll: vi.fn(), |
| 43 | + }; |
| 44 | + return { router, prefetch }; |
| 45 | +} |
| 46 | + |
| 47 | +async function mountLink(router: VoidRouter, props: LinkProps) { |
| 48 | + const container = document.createElement('div'); |
| 49 | + document.body.append(container); |
| 50 | + const root = createRoot(container); |
| 51 | + mountedRoots.push({ root, container }); |
| 52 | + await act(async () => { |
| 53 | + root.render( |
| 54 | + createElement(RouterContext.Provider, { value: router }, createElement(Link, props, 'Go')), |
| 55 | + ); |
| 56 | + }); |
| 57 | + return container.firstElementChild as HTMLElement; |
| 58 | +} |
| 59 | + |
| 60 | +describe('Link default prefetch strategy', () => { |
| 61 | + it('prefetches a Link without a prefetch prop with the app default', async () => { |
| 62 | + const { router, prefetch } = createRouter('mount'); |
| 63 | + await mountLink(router, { href: '/users' }); |
| 64 | + |
| 65 | + expect(prefetch).toHaveBeenCalledWith('/users', {}); |
| 66 | + }); |
| 67 | + |
| 68 | + it('lets the Link prop replace the default', async () => { |
| 69 | + const { router, prefetch } = createRouter('mount'); |
| 70 | + const link = await mountLink(router, { href: '/users', prefetch: 'hover' }); |
| 71 | + expect(prefetch).not.toHaveBeenCalled(); |
| 72 | + |
| 73 | + await act(async () => { |
| 74 | + link.dispatchEvent(new FocusEvent('focusin', { bubbles: true })); |
| 75 | + }); |
| 76 | + expect(prefetch).toHaveBeenCalledWith('/users', {}); |
| 77 | + }); |
| 78 | + |
| 79 | + it('turns the default off with prefetch={false}', async () => { |
| 80 | + const { router, prefetch } = createRouter('mount'); |
| 81 | + await mountLink(router, { href: '/users', prefetch: false }); |
| 82 | + |
| 83 | + expect(prefetch).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('skips non-GET links without throwing', async () => { |
| 87 | + const { router, prefetch } = createRouter('mount'); |
| 88 | + const button = await mountLink(router, { href: '/users', method: 'POST' }); |
| 89 | + |
| 90 | + expect(button.tagName).toBe('BUTTON'); |
| 91 | + expect(prefetch).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('skips links to other sites', async () => { |
| 95 | + const { router, prefetch } = createRouter('mount'); |
| 96 | + await mountLink(router, { href: 'https://example.com/users' }); |
| 97 | + |
| 98 | + expect(prefetch).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('does not prefetch without an app default', async () => { |
| 102 | + const { router, prefetch } = createRouter(); |
| 103 | + await mountLink(router, { href: '/users' }); |
| 104 | + |
| 105 | + expect(prefetch).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments