61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { render, screen } from '@testing-library/react'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import ErrorBoundary from '../../src/components/common/ErrorBoundary'
|
|
import i18n from '../../src/i18n'
|
|
|
|
function Boom(): never {
|
|
throw new Error('pane-crash')
|
|
}
|
|
|
|
describe('ErrorBoundary', () => {
|
|
beforeEach(() => {
|
|
vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await i18n.changeLanguage('zh-CN')
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('shows compact pane copy in Chinese', async () => {
|
|
await i18n.changeLanguage('zh-CN')
|
|
render(
|
|
<ErrorBoundary compact>
|
|
<Boom />
|
|
</ErrorBoundary>
|
|
)
|
|
|
|
expect(screen.getByText(i18n.t('common:error.bodyPane'))).toBeVisible()
|
|
expect(screen.getByRole('button', { name: i18n.t('common:error.retryPane') })).toBeVisible()
|
|
})
|
|
|
|
it('shows compact pane copy in English', async () => {
|
|
await i18n.changeLanguage('en')
|
|
render(
|
|
<ErrorBoundary compact>
|
|
<Boom />
|
|
</ErrorBoundary>
|
|
)
|
|
|
|
expect(screen.getByText('This tab hit an unexpected problem. Other tabs still work.')).toBeVisible()
|
|
expect(screen.getByRole('button', { name: 'Retry this tab' })).toBeVisible()
|
|
})
|
|
|
|
it('clears the captured error when resetKey changes', async () => {
|
|
await i18n.changeLanguage('zh-CN')
|
|
const { rerender } = render(
|
|
<ErrorBoundary compact resetKey="tab-a">
|
|
<Boom />
|
|
</ErrorBoundary>
|
|
)
|
|
expect(screen.getByText(i18n.t('common:error.bodyPane'))).toBeVisible()
|
|
|
|
rerender(
|
|
<ErrorBoundary compact resetKey="tab-b">
|
|
<div>ok-pane</div>
|
|
</ErrorBoundary>
|
|
)
|
|
expect(screen.getByText('ok-pane')).toBeVisible()
|
|
})
|
|
})
|