|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import type * as SentryBrowser from '@sentry/browser'; |
| 3 | +import { createTransport, getCurrentScope, setCurrentClient } from '@sentry/core'; |
| 4 | +import { render } from '@solidjs/testing-library'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | +import { vi } from 'vitest'; |
| 7 | + |
| 8 | +import { ErrorBoundary } from 'solid-js'; |
| 9 | +import { BrowserClient, withSentryErrorBoundary } from '../src'; |
| 10 | + |
| 11 | +const mockCaptureException = vi.fn(); |
| 12 | +vi.mock('@sentry/browser', async () => { |
| 13 | + const actual = await vi.importActual<typeof SentryBrowser>('@sentry/browser'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + captureException: (...args) => mockCaptureException(...args), |
| 17 | + } as typeof SentryBrowser; |
| 18 | +}); |
| 19 | + |
| 20 | +const user = userEvent.setup(); |
| 21 | +const SentryErrorBoundary = withSentryErrorBoundary(ErrorBoundary); |
| 22 | + |
| 23 | +describe('withSentryErrorBoundary', () => { |
| 24 | + function createMockBrowserClient(): BrowserClient { |
| 25 | + return new BrowserClient({ |
| 26 | + integrations: [], |
| 27 | + tracesSampleRate: 1, |
| 28 | + transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})), |
| 29 | + stackParser: () => [], |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + |
| 36 | + const client = createMockBrowserClient(); |
| 37 | + setCurrentClient(client); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + getCurrentScope().setClient(undefined); |
| 42 | + }); |
| 43 | + |
| 44 | + it('calls `captureException` when an error occurs`', () => { |
| 45 | + render(() => ( |
| 46 | + <SentryErrorBoundary fallback={<div>Ooops, an error occurred.</div>}> |
| 47 | + <NonExistentComponent /> |
| 48 | + </SentryErrorBoundary> |
| 49 | + )); |
| 50 | + |
| 51 | + expect(mockCaptureException).toHaveBeenCalledTimes(1); |
| 52 | + expect(mockCaptureException).toHaveBeenLastCalledWith(new ReferenceError('NonExistentComponent is not defined')); |
| 53 | + }); |
| 54 | + |
| 55 | + it('renders the fallback component', async () => { |
| 56 | + const { findByText } = render(() => ( |
| 57 | + <SentryErrorBoundary fallback={<div>Ooops, an error occurred.</div>}> |
| 58 | + <NonExistentComponent /> |
| 59 | + </SentryErrorBoundary> |
| 60 | + )); |
| 61 | + |
| 62 | + expect(await findByText('Ooops, an error occurred.')).toBeInTheDocument(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('passes the `error` and `reset` function to the fallback component', () => { |
| 66 | + const mockFallback = vi.fn(); |
| 67 | + |
| 68 | + render(() => { |
| 69 | + <SentryErrorBoundary fallback={mockFallback}> |
| 70 | + <NonExistentComponent /> |
| 71 | + </SentryErrorBoundary>; |
| 72 | + }); |
| 73 | + |
| 74 | + expect(mockFallback).toHaveBeenCalledTimes(1); |
| 75 | + expect(mockFallback).toHaveBeenCalledWith( |
| 76 | + new ReferenceError('NonExistentComponent is not defined'), |
| 77 | + expect.any(Function), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('calls `captureException` again after resetting', async () => { |
| 82 | + const { findByRole } = render(() => ( |
| 83 | + <SentryErrorBoundary fallback={(_, reset) => <button onClick={reset}>Reset</button>}> |
| 84 | + <NonExistentComponent /> |
| 85 | + </SentryErrorBoundary> |
| 86 | + )); |
| 87 | + |
| 88 | + expect(mockCaptureException).toHaveBeenCalledTimes(1); |
| 89 | + expect(mockCaptureException).toHaveBeenNthCalledWith(1, new ReferenceError('NonExistentComponent is not defined')); |
| 90 | + |
| 91 | + const button = await findByRole('button'); |
| 92 | + await user.click(button); |
| 93 | + |
| 94 | + expect(mockCaptureException).toHaveBeenCalledTimes(2); |
| 95 | + expect(mockCaptureException).toHaveBeenNthCalledWith(2, new ReferenceError('NonExistentComponent is not defined')); |
| 96 | + }); |
| 97 | + |
| 98 | + it('renders children when there is no error', async () => { |
| 99 | + const { queryByText } = render(() => ( |
| 100 | + <SentryErrorBoundary fallback={<div>Oops, an error occurred.</div>}> |
| 101 | + <div>Adopt a cat</div> |
| 102 | + </SentryErrorBoundary> |
| 103 | + )); |
| 104 | + |
| 105 | + expect(await queryByText('Adopt a cat')).toBeInTheDocument(); |
| 106 | + expect(await queryByText('Ooops, an error occurred')).not.toBeInTheDocument(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments