| 1 | /** |
| 2 | * @vitest-environment happy-dom |
| 3 | */ |
| 4 | import React, { act } from 'react' |
| 5 | import { afterEach, describe, expect, it, vi } from 'vitest' |
| 6 | import { createRoot } from 'react-dom/client' |
| 7 | import { RendererErrorBoundary } from '../../../src/renderer/src/components/RendererErrorBoundary' |
| 8 | |
| 9 | function BrokenPage(): React.JSX.Element { |
| 10 | throw new Error('render failed') |
| 11 | } |
| 12 | |
| 13 | describe('RendererErrorBoundary', () => { |
| 14 | afterEach(() => { |
| 15 | vi.restoreAllMocks() |
| 16 | document.body.innerHTML = '' |
| 17 | }) |
| 18 | |
| 19 | it('replaces a crashed React tree with a refresh action', async () => { |
| 20 | vi.spyOn(console, 'error').mockImplementation(() => undefined) |
| 21 | const container = document.createElement('div') |
| 22 | document.body.appendChild(container) |
| 23 | const root = createRoot(container) |
| 24 | |
| 25 | await act(async () => { |
| 26 | root.render(React.createElement(RendererErrorBoundary, null, React.createElement(BrokenPage))) |
| 27 | }) |
| 28 | |
| 29 | expect(container.textContent).toContain('页面遇到错误') |
| 30 | expect(container.querySelector('button')?.textContent).toBe('刷新应用') |
| 31 | |
| 32 | await act(async () => { |
| 33 | root.unmount() |
| 34 | }) |
| 35 | }) |
| 36 | }) |
| 37 |