From 32671b5db224d1568d5225d099992980e1d5111f Mon Sep 17 00:00:00 2001 From: Marcelo Serpa <81248+fullofcaffeine@users.noreply.github.com> Date: Fri, 8 Jul 2022 17:34:49 -0500 Subject: [PATCH] Add unit tests --- .../src/components/test/error-boundary.js | 38 +++++++++++++++++++ .../src/components/test/error-boundary.js | 38 +++++++++++++++++++ .../src/components/test/error-boundary.js | 38 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 packages/customize-widgets/src/components/test/error-boundary.js create mode 100644 packages/edit-site/src/components/test/error-boundary.js create mode 100644 packages/edit-widgets/src/components/test/error-boundary.js diff --git a/packages/customize-widgets/src/components/test/error-boundary.js b/packages/customize-widgets/src/components/test/error-boundary.js new file mode 100644 index 00000000000000..7b9977e10f1315 --- /dev/null +++ b/packages/customize-widgets/src/components/test/error-boundary.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import * as wpHooks from '@wordpress/hooks'; +/** + * Internal dependencies + */ +import ErrorBoundary from '../error-boundary'; +/** + * External dependencies + */ +import { render } from '@testing-library/react'; + +const theError = new Error( 'Kaboom' ); + +const ChildComponent = () => { + throw theError; +}; + +describe( 'Error Boundary', () => { + describe( 'when error is thrown from a Child component', () => { + it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => { + const doAction = jest.spyOn( wpHooks, 'doAction' ); + + render( + + + + ); + + expect( doAction ).toHaveBeenCalledWith( + 'editor.ErrorBoundary.errorLogged', + theError + ); + expect( console ).toHaveErrored(); + } ); + } ); +} ); diff --git a/packages/edit-site/src/components/test/error-boundary.js b/packages/edit-site/src/components/test/error-boundary.js new file mode 100644 index 00000000000000..7b9977e10f1315 --- /dev/null +++ b/packages/edit-site/src/components/test/error-boundary.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import * as wpHooks from '@wordpress/hooks'; +/** + * Internal dependencies + */ +import ErrorBoundary from '../error-boundary'; +/** + * External dependencies + */ +import { render } from '@testing-library/react'; + +const theError = new Error( 'Kaboom' ); + +const ChildComponent = () => { + throw theError; +}; + +describe( 'Error Boundary', () => { + describe( 'when error is thrown from a Child component', () => { + it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => { + const doAction = jest.spyOn( wpHooks, 'doAction' ); + + render( + + + + ); + + expect( doAction ).toHaveBeenCalledWith( + 'editor.ErrorBoundary.errorLogged', + theError + ); + expect( console ).toHaveErrored(); + } ); + } ); +} ); diff --git a/packages/edit-widgets/src/components/test/error-boundary.js b/packages/edit-widgets/src/components/test/error-boundary.js new file mode 100644 index 00000000000000..7b9977e10f1315 --- /dev/null +++ b/packages/edit-widgets/src/components/test/error-boundary.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import * as wpHooks from '@wordpress/hooks'; +/** + * Internal dependencies + */ +import ErrorBoundary from '../error-boundary'; +/** + * External dependencies + */ +import { render } from '@testing-library/react'; + +const theError = new Error( 'Kaboom' ); + +const ChildComponent = () => { + throw theError; +}; + +describe( 'Error Boundary', () => { + describe( 'when error is thrown from a Child component', () => { + it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => { + const doAction = jest.spyOn( wpHooks, 'doAction' ); + + render( + + + + ); + + expect( doAction ).toHaveBeenCalledWith( + 'editor.ErrorBoundary.errorLogged', + theError + ); + expect( console ).toHaveErrored(); + } ); + } ); +} );