|
| 1 | +import * as React from 'react' |
| 2 | +import { cleanup, fireEvent, render, screen } from '@testing-library/react' |
| 3 | +import { afterEach, expect, test, vi } from 'vitest' |
| 4 | +import { createControlledPromise } from '@tanstack/router-core' |
| 5 | +import { lazyRouteComponent } from '../src' |
| 6 | + |
| 7 | +// https://github.com/TanStack/router/issues/8827 |
| 8 | +const message = |
| 9 | + 'error loading dynamically imported module: http://localhost/assets/page.js' |
| 10 | +const reloadKey = `tanstack_router_reload:${message}` |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + cleanup() |
| 14 | + sessionStorage.clear() |
| 15 | + vi.restoreAllMocks() |
| 16 | +}) |
| 17 | + |
| 18 | +// jsdom cannot reload, so React retries the render and reaches the error that |
| 19 | +// the reload guard rethrows once its key is set. |
| 20 | +class ErrorBoundary extends React.Component< |
| 21 | + { children: React.ReactNode }, |
| 22 | + { error: unknown } |
| 23 | +> { |
| 24 | + state: { error: unknown } = { error: undefined } |
| 25 | + static getDerivedStateFromError(error: unknown) { |
| 26 | + return { error } |
| 27 | + } |
| 28 | + render() { |
| 29 | + return this.state.error ? <div>Error</div> : this.props.children |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function renderLazy(Page: ReturnType<typeof lazyRouteComponent>) { |
| 34 | + return render( |
| 35 | + <ErrorBoundary> |
| 36 | + <React.Suspense fallback={<div>Loading</div>}> |
| 37 | + <Page /> |
| 38 | + </React.Suspense> |
| 39 | + </ErrorBoundary>, |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +test('does not reload when the import is cancelled because the page is unloading', async () => { |
| 44 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 45 | + const first = createControlledPromise<never>() |
| 46 | + const importer = vi |
| 47 | + .fn() |
| 48 | + .mockReturnValueOnce(first) |
| 49 | + .mockResolvedValueOnce({ default: () => <div>Loaded</div> }) |
| 50 | + const Page = lazyRouteComponent(importer) |
| 51 | + |
| 52 | + const preload = Page.preload!() |
| 53 | + window.dispatchEvent(new Event('beforeunload')) |
| 54 | + first.reject(new TypeError(message)) |
| 55 | + await preload |
| 56 | + |
| 57 | + renderLazy(Page) |
| 58 | + expect(screen.getByText('Loading')).toBeInTheDocument() |
| 59 | + expect(sessionStorage.getItem(reloadKey)).toBeNull() |
| 60 | + |
| 61 | + // The page stayed after all, so the import is retried. |
| 62 | + fireEvent.pointerDown(document.body) |
| 63 | + expect(await screen.findByText('Loaded')).toBeInTheDocument() |
| 64 | + expect(importer).toHaveBeenCalledTimes(2) |
| 65 | + expect(sessionStorage.getItem(reloadKey)).toBeNull() |
| 66 | +}) |
| 67 | + |
| 68 | +test('reloads once if the retried import is still missing after the page stays', async () => { |
| 69 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 70 | + const first = createControlledPromise<never>() |
| 71 | + const importer = vi |
| 72 | + .fn() |
| 73 | + .mockReturnValueOnce(first) |
| 74 | + .mockRejectedValueOnce(new TypeError(message)) |
| 75 | + const Page = lazyRouteComponent(importer) |
| 76 | + |
| 77 | + const preload = Page.preload!() |
| 78 | + window.dispatchEvent(new Event('beforeunload')) |
| 79 | + first.reject(new TypeError(message)) |
| 80 | + await preload |
| 81 | + |
| 82 | + renderLazy(Page) |
| 83 | + fireEvent.keyDown(document.body) |
| 84 | + await vi.waitFor(() => expect(sessionStorage.getItem(reloadKey)).toBe('1')) |
| 85 | + expect(importer).toHaveBeenCalledTimes(2) |
| 86 | +}) |
| 87 | + |
| 88 | +test('still reloads once for a missing module when the page is not unloading', async () => { |
| 89 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 90 | + const Page = lazyRouteComponent(() => Promise.reject(new TypeError(message))) |
| 91 | + |
| 92 | + await Page.preload!() |
| 93 | + renderLazy(Page) |
| 94 | + |
| 95 | + expect(sessionStorage.getItem(reloadKey)).toBe('1') |
| 96 | +}) |
| 97 | + |
| 98 | +test('stops watching for unload once the import settles', async () => { |
| 99 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 100 | + const Page = lazyRouteComponent(() => Promise.reject(new TypeError(message))) |
| 101 | + |
| 102 | + await Page.preload!() |
| 103 | + // A later unload must not be attributed to the import that already failed. |
| 104 | + window.dispatchEvent(new Event('beforeunload')) |
| 105 | + renderLazy(Page) |
| 106 | + |
| 107 | + expect(sessionStorage.getItem(reloadKey)).toBe('1') |
| 108 | +}) |
0 commit comments