From e29b40990c53efa1416be815f5140f66c4343770 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 24 Mar 2019 13:18:29 -0400 Subject: [PATCH] Updating SentryBoundary to use react-testing-library --- .../SentryBoundary/__snapshots__/test.js.snap | 21 ++++++++-- src/components/SentryBoundary/test.js | 38 ++++++++++++------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/components/SentryBoundary/__snapshots__/test.js.snap b/src/components/SentryBoundary/__snapshots__/test.js.snap index 013e4a1..330b19e 100644 --- a/src/components/SentryBoundary/__snapshots__/test.js.snap +++ b/src/components/SentryBoundary/__snapshots__/test.js.snap @@ -1,7 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`SentryBoundary error handling 1`] = ``; +exports[`SentryBoundary error handling 1`] = ` + + Example content + +`; -exports[`SentryBoundary error handling 2`] = ``; +exports[`SentryBoundary error handling 2`] = ` + + + +`; -exports[`SentryBoundary rendering 1`] = `"Example content"`; +exports[`SentryBoundary rendering 1`] = ` + + Example content + +`; diff --git a/src/components/SentryBoundary/test.js b/src/components/SentryBoundary/test.js index 456392e..76a869b 100644 --- a/src/components/SentryBoundary/test.js +++ b/src/components/SentryBoundary/test.js @@ -1,41 +1,53 @@ jest.mock('@sentry/browser'); +jest.mock('components/SentryError', () => + require('__mocks__/component-mock')('components/SentryError')); import React from 'react'; -import { shallow } from 'enzyme'; +import { render } from 'react-testing-library'; import * as Sentry from '@sentry/browser'; import SentryBoundary from 'components/SentryBoundary'; describe('SentryBoundary', () => { test('rendering', () => { - const component = shallow( + const { asFragment } = render( Example content ); - expect(component).toMatchSnapshot(); + expect(asFragment()).toMatchSnapshot(); }); test('error handling', () => { - const Child = () => 'Example content'; - const component = shallow( + // Hide React's console logging about the error + jest.spyOn(console, 'error').mockImplementation(() => {}); + + const error =new Error('Example error'); + const Child = ({ shouldThrow }) => { + if (shouldThrow) { + throw error; + } + + return 'Example content'; + }; + const { asFragment, rerender } = render( - + ); - expect(component).toMatchSnapshot(); + expect(asFragment()).toMatchSnapshot(); - const error = new Error('Example error'); - component.find('Child').simulateError(error); - // NOTE: Enzyme doesn't call getDerivedStateFromError yet, so we have to - // set the state manually - component.setState(SentryBoundary.getDerivedStateFromError(error)); + rerender( + + + + ); const scope = { setExtra: jest.fn() }; expect(Sentry.withScope).toHaveBeenCalled(); Sentry.withScope.mock.calls[0][0](scope); expect(Sentry.captureException).toHaveBeenCalledWith(error); - expect(component).toMatchSnapshot(); + expect(asFragment()).toMatchSnapshot(); }); });