|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, fireEvent, render } from '@testing-library/react' |
| 3 | +import React, { useState } from 'react' |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { useHead } from '../src' |
| 6 | +import { createHead, renderDOMHead, UnheadProvider } from '../src/client' |
| 7 | + |
| 8 | +/** |
| 9 | + * Reproduction of https://github.com/unjs/unhead/issues/558 |
| 10 | + * React 18 reporter says metadata persists after component unmount |
| 11 | + */ |
| 12 | + |
| 13 | +function Page1() { |
| 14 | + useHead({ |
| 15 | + title: 'Page 1 title', |
| 16 | + meta: [{ name: 'description', content: 'page 1 description' }], |
| 17 | + }) |
| 18 | + return <div>Page 1</div> |
| 19 | +} |
| 20 | + |
| 21 | +function Page2() { |
| 22 | + return <div>Page 2</div> |
| 23 | +} |
| 24 | + |
| 25 | +function App({ head }: { head: ReturnType<typeof createHead> }) { |
| 26 | + const [currPage, setCurrPage] = useState<'Page 1' | 'Page 2'>('Page 2') |
| 27 | + return ( |
| 28 | + <UnheadProvider head={head}> |
| 29 | + <button onClick={() => setCurrPage('Page 1')}>Page 1</button> |
| 30 | + <button onClick={() => setCurrPage('Page 2')}>Page 2</button> |
| 31 | + {currPage === 'Page 1' ? <Page1 /> : <Page2 />} |
| 32 | + </UnheadProvider> |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +function wait(ms = 10) { |
| 37 | + return new Promise<void>(resolve => setTimeout(resolve, ms)) |
| 38 | +} |
| 39 | + |
| 40 | +describe('issue #558 - unmount cleanup', () => { |
| 41 | + it('restores init values after component unmount (DOM rendering)', async () => { |
| 42 | + const head = createHead({ |
| 43 | + init: [{ |
| 44 | + title: 'Example fallback', |
| 45 | + meta: [{ name: 'description', content: 'some description' }], |
| 46 | + }], |
| 47 | + }) |
| 48 | + |
| 49 | + const { getByText } = render(<App head={head} />) |
| 50 | + |
| 51 | + // Initial DOM render |
| 52 | + await act(async () => { |
| 53 | + await renderDOMHead(head) |
| 54 | + await wait() |
| 55 | + }) |
| 56 | + |
| 57 | + expect(document.title).toBe('Example fallback') |
| 58 | + |
| 59 | + // Switch to Page 1 |
| 60 | + await act(async () => { |
| 61 | + fireEvent.click(getByText('Page 1')) |
| 62 | + await wait() |
| 63 | + }) |
| 64 | + await act(async () => { |
| 65 | + await renderDOMHead(head) |
| 66 | + await wait() |
| 67 | + }) |
| 68 | + |
| 69 | + expect(document.title).toBe('Page 1 title') |
| 70 | + |
| 71 | + // Switch back to Page 2 |
| 72 | + await act(async () => { |
| 73 | + fireEvent.click(getByText('Page 2')) |
| 74 | + await wait() |
| 75 | + }) |
| 76 | + await act(async () => { |
| 77 | + await renderDOMHead(head) |
| 78 | + await wait() |
| 79 | + }) |
| 80 | + |
| 81 | + expect(document.title).toBe('Example fallback') |
| 82 | + }) |
| 83 | + |
| 84 | + it('handles multiple page switches correctly (DOM)', async () => { |
| 85 | + const head = createHead({ |
| 86 | + init: [{ |
| 87 | + title: 'Fallback', |
| 88 | + meta: [{ name: 'description', content: 'fallback desc' }], |
| 89 | + }], |
| 90 | + }) |
| 91 | + |
| 92 | + const { getByText } = render(<App head={head} />) |
| 93 | + |
| 94 | + for (let i = 0; i < 3; i++) { |
| 95 | + await act(async () => { |
| 96 | + fireEvent.click(getByText('Page 1')) |
| 97 | + await wait() |
| 98 | + }) |
| 99 | + await act(async () => { |
| 100 | + await renderDOMHead(head) |
| 101 | + await wait() |
| 102 | + }) |
| 103 | + expect(document.title).toBe('Page 1 title') |
| 104 | + |
| 105 | + await act(async () => { |
| 106 | + fireEvent.click(getByText('Page 2')) |
| 107 | + await wait() |
| 108 | + }) |
| 109 | + await act(async () => { |
| 110 | + await renderDOMHead(head) |
| 111 | + await wait() |
| 112 | + }) |
| 113 | + expect(document.title).toBe('Fallback') |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + it('direct unmount restores init values (DOM)', async () => { |
| 118 | + const head = createHead({ |
| 119 | + init: [{ |
| 120 | + title: 'Init Title', |
| 121 | + meta: [{ name: 'description', content: 'init description' }], |
| 122 | + }], |
| 123 | + }) |
| 124 | + |
| 125 | + function PageWithHead() { |
| 126 | + useHead({ |
| 127 | + title: 'Component Title', |
| 128 | + meta: [{ name: 'description', content: 'component description' }], |
| 129 | + }) |
| 130 | + return <div>Has Head</div> |
| 131 | + } |
| 132 | + |
| 133 | + const { unmount } = render( |
| 134 | + <UnheadProvider head={head}> |
| 135 | + <PageWithHead /> |
| 136 | + </UnheadProvider>, |
| 137 | + ) |
| 138 | + |
| 139 | + await act(async () => { |
| 140 | + await renderDOMHead(head) |
| 141 | + await wait() |
| 142 | + }) |
| 143 | + |
| 144 | + expect(document.title).toBe('Component Title') |
| 145 | + |
| 146 | + await act(async () => { |
| 147 | + unmount() |
| 148 | + await wait() |
| 149 | + }) |
| 150 | + await act(async () => { |
| 151 | + await renderDOMHead(head) |
| 152 | + await wait() |
| 153 | + }) |
| 154 | + |
| 155 | + expect(document.title).toBe('Init Title') |
| 156 | + }) |
| 157 | + |
| 158 | + it('entries state is correct through mount/unmount cycle', async () => { |
| 159 | + const head = createHead({ |
| 160 | + init: [{ |
| 161 | + title: 'Init', |
| 162 | + }], |
| 163 | + }) |
| 164 | + |
| 165 | + function PageWithHead() { |
| 166 | + useHead({ title: 'Component' }) |
| 167 | + return <div>Has Head</div> |
| 168 | + } |
| 169 | + |
| 170 | + // Initially, only init entry |
| 171 | + expect(head.entries.size).toBe(1) |
| 172 | + |
| 173 | + const { unmount } = render( |
| 174 | + <UnheadProvider head={head}> |
| 175 | + <PageWithHead /> |
| 176 | + </UnheadProvider>, |
| 177 | + ) |
| 178 | + |
| 179 | + await act(async () => { |
| 180 | + await wait() |
| 181 | + }) |
| 182 | + |
| 183 | + // After mount, should have 2 entries |
| 184 | + expect(head.entries.size).toBe(2) |
| 185 | + |
| 186 | + await act(async () => { |
| 187 | + unmount() |
| 188 | + await wait() |
| 189 | + }) |
| 190 | + |
| 191 | + // After unmount, back to 1 entry (init only) |
| 192 | + expect(head.entries.size).toBe(1) |
| 193 | + }) |
| 194 | +}) |
0 commit comments