test: improve coverage parameters for some files in base (#33207)

This commit is contained in:
Saumya Talwani
2026-03-12 12:27:31 +05:30
committed by GitHub
parent c43307dae1
commit 68982f910e
86 changed files with 7513 additions and 765 deletions

View File

@ -238,6 +238,32 @@ describe('ErrorBoundary', () => {
})
})
it('should not reset when resetKeys reference changes but values are identical', async () => {
const onReset = vi.fn()
const StableKeysHarness = () => {
const [keys, setKeys] = React.useState<Array<string | number>>([1, 2])
return (
<>
<button onClick={() => setKeys([1, 2])}>Update keys same values</button>
<ErrorBoundary resetKeys={keys} onReset={onReset}>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>
</>
)
}
render(<StableKeysHarness />)
await screen.findByText('Something went wrong')
fireEvent.click(screen.getByRole('button', { name: 'Update keys same values' }))
await waitFor(() => {
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
})
expect(onReset).not.toHaveBeenCalled()
})
it('should reset after children change when resetOnPropsChange is true', async () => {
const ResetOnPropsHarness = () => {
const [shouldThrow, setShouldThrow] = React.useState(true)
@ -269,6 +295,24 @@ describe('ErrorBoundary', () => {
expect(screen.getByText('second child')).toBeInTheDocument()
})
})
it('should call window.location.reload when Reload Page is clicked', async () => {
const reloadSpy = vi.fn()
Object.defineProperty(window, 'location', {
value: { ...window.location, reload: reloadSpy },
writable: true,
})
render(
<ErrorBoundary>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
fireEvent.click(await screen.findByRole('button', { name: 'Reload Page' }))
expect(reloadSpy).toHaveBeenCalledTimes(1)
})
})
})
@ -358,6 +402,16 @@ describe('ErrorBoundary utility exports', () => {
expect(Wrapped.displayName).toBe('withErrorBoundary(NamedComponent)')
})
it('should fallback displayName to Component when wrapped component has no displayName and empty name', () => {
const Nameless = (() => <div>nameless</div>) as React.FC
Object.defineProperty(Nameless, 'displayName', { value: undefined, configurable: true })
Object.defineProperty(Nameless, 'name', { value: '', configurable: true })
const Wrapped = withErrorBoundary(Nameless)
expect(Wrapped.displayName).toBe('withErrorBoundary(Component)')
})
})
// Validate simple fallback helper component.