Skip to content

Instantly share code, notes, and snippets.

@vinnymac
Created February 1, 2023 03:26
Show Gist options
  • Save vinnymac/5720fb05a1d18de1df54046c4ac7f240 to your computer and use it in GitHub Desktop.
Save vinnymac/5720fb05a1d18de1df54046c4ac7f240 to your computer and use it in GitHub Desktop.
React error boundary spec
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event'
import { Account } from './Account';
import { ErrorBoundary } from './ErrorBoundary';
describe('Account', () => {
it('renders a fallback when an account error is thrown', async () => {
const fallback = jest.fn(() => (
<button>Try Again</button>
));
const user = userEvent.setup();
render(
<ErrorBoundary fallback={fallback}>
<Account />
</ErrorBoundary>
);
await userEvent.click(screen.getByRole('button', { name: /try again/i });
expect(fallback).toHaveBeenCalledTimes(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment