From c72f5b4f2a4a57fdd972513f0b623f83f533a8d7 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 10 Jul 2018 00:47:09 -0400 Subject: [PATCH] Switch away from using enzyme.mount in tests (components/higher-order/with-filters/test/index.js) (#7793) * switch away from using enzyme.mount in tests This switches all tests in `components/higher-order/with-filters/test/index.js` from using enzyme.mount to `React.TestUtilities`. This is because `enzyme` does not fully support React 16.3+ (and movement to do so is really slow). This will fix issues with breakage due to the enzyme incompatibility as components receive React 16.3+ features (such as `forwardRef` usage in #7557). * switch test to be against root node. * fix linting issues * remove unnecessary unmount --- .../higher-order/with-filters/test/index.js | 85 ++++++++++++++----- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/components/higher-order/with-filters/test/index.js b/components/higher-order/with-filters/test/index.js index be57e7c7a091ac..8b41886c11621e 100644 --- a/components/higher-order/with-filters/test/index.js +++ b/components/higher-order/with-filters/test/index.js @@ -1,35 +1,63 @@ /** * External dependencies */ -import { mount, shallow } from 'enzyme'; +import { shallow } from 'enzyme'; +import TestUtils from 'react-dom/test-utils'; +import ReactDOM from 'react-dom'; /** * WordPress dependencies */ import { addFilter, removeAllFilters, removeFilter } from '@wordpress/hooks'; +import { Component } from '@wordpress/element'; /** * Internal dependencies */ import withFilters from '..'; +const assertExpectedHtml = ( wrapper, expectedHTML ) => { + /* eslint-disable react/no-find-dom-node */ + const element = ReactDOM.findDOMNode( wrapper ); + /* eslint-enable react/no-find-dom-node */ + expect( element.outerHTML ).toBe( expectedHTML ); +}; + +// this is needed because TestUtils does not accept a stateless component. +// anything run through a HOC ends up as a stateless component. +const getTestComponent = ( WrappedComponent ) => { + class TestComponent extends Component { + render() { + return ; + } + } + return ; +}; + describe( 'withFilters', () => { - let wrapper; + let shallowWrapper, wrapper; const hookName = 'EnhancedComponent'; const MyComponent = () =>
My component
; afterEach( () => { - wrapper.unmount(); + if ( wrapper ) { + /* eslint-disable react/no-find-dom-node */ + ReactDOM.unmountComponentAtNode( ReactDOM.findDOMNode( wrapper ).parentNode ); + } + if ( shallowWrapper ) { + shallowWrapper.unmount(); + } + /* eslint-enable react/no-find-dom-node */ removeAllFilters( hookName ); } ); it( 'should display original component when no filters applied', () => { const EnhancedComponent = withFilters( hookName )( MyComponent ); - wrapper = shallow( ); + shallowWrapper = shallow( ); - expect( wrapper.html() ).toBe( '
My component
' ); + expect( shallowWrapper.html() ).toBe( '
My component
' ); } ); it( 'should display a component overridden by the filter', () => { @@ -41,9 +69,9 @@ describe( 'withFilters', () => { ); const EnhancedComponent = withFilters( hookName )( MyComponent ); - wrapper = shallow( ); + shallowWrapper = shallow( ); - expect( wrapper.html() ).toBe( '
Overridden component
' ); + expect( shallowWrapper.html() ).toBe( '
Overridden component
' ); } ); it( 'should display two components composed by the filter', () => { @@ -60,9 +88,9 @@ describe( 'withFilters', () => { ); const EnhancedComponent = withFilters( hookName )( MyComponent ); - wrapper = shallow( ); + shallowWrapper = shallow( ); - expect( wrapper.html() ).toBe( '
My component
Composed component
' ); + expect( shallowWrapper.html() ).toBe( '
My component
Composed component
' ); } ); it( 'should not re-render component when new filter added before component was mounted', () => { @@ -82,12 +110,15 @@ describe( 'withFilters', () => { ); const EnhancedComponent = withFilters( hookName )( SpiedComponent ); - wrapper = mount( ); + wrapper = TestUtils.renderIntoDocument( getTestComponent( EnhancedComponent ) ); jest.runAllTimers(); expect( spy ).toHaveBeenCalledTimes( 1 ); - expect( wrapper.html() ).toBe( '
Spied component
' ); + assertExpectedHtml( + wrapper, + '
Spied component
' + ); } ); it( 'should re-render component once when new filter added after component was mounted', () => { @@ -98,7 +129,7 @@ describe( 'withFilters', () => { }; const EnhancedComponent = withFilters( hookName )( SpiedComponent ); - wrapper = mount( ); + wrapper = TestUtils.renderIntoDocument( getTestComponent( EnhancedComponent ) ); spy.mockClear(); addFilter( @@ -113,7 +144,10 @@ describe( 'withFilters', () => { jest.runAllTimers(); expect( spy ).toHaveBeenCalledTimes( 1 ); - expect( wrapper.html() ).toBe( '
Spied component
' ); + assertExpectedHtml( + wrapper, + '
Spied component
' + ); } ); it( 'should re-render component once when two filters added in the same animation frame', () => { @@ -123,7 +157,7 @@ describe( 'withFilters', () => { return
Spied component
; }; const EnhancedComponent = withFilters( hookName )( SpiedComponent ); - wrapper = mount( ); + wrapper = TestUtils.renderIntoDocument( getTestComponent( EnhancedComponent ) ); spy.mockClear(); @@ -148,7 +182,10 @@ describe( 'withFilters', () => { jest.runAllTimers(); expect( spy ).toHaveBeenCalledTimes( 1 ); - expect( wrapper.html() ).toBe( '
Spied component
' ); + assertExpectedHtml( + wrapper, + '
Spied component
' + ); } ); it( 'should re-render component twice when new filter added and removed in two different animation frames', () => { @@ -158,7 +195,7 @@ describe( 'withFilters', () => { return
Spied component
; }; const EnhancedComponent = withFilters( hookName )( SpiedComponent ); - wrapper = mount( ); + wrapper = TestUtils.renderIntoDocument( getTestComponent( EnhancedComponent ) ); spy.mockClear(); addFilter( @@ -179,7 +216,10 @@ describe( 'withFilters', () => { jest.runAllTimers(); expect( spy ).toHaveBeenCalledTimes( 2 ); - expect( wrapper.html() ).toBe( '
Spied component
' ); + assertExpectedHtml( + wrapper, + '
Spied component
' + ); } ); it( 'should re-render both components once each when one filter added', () => { @@ -190,12 +230,12 @@ describe( 'withFilters', () => { }; const EnhancedComponent = withFilters( hookName )( SpiedComponent ); const CombinedComponents = () => ( -
+
-
+ ); - wrapper = mount( ); + wrapper = TestUtils.renderIntoDocument( getTestComponent( CombinedComponents ) ); spy.mockClear(); addFilter( @@ -210,6 +250,9 @@ describe( 'withFilters', () => { jest.runAllTimers(); expect( spy ).toHaveBeenCalledTimes( 2 ); - expect( wrapper.html() ).toBe( '
Spied component
Spied component
' ); + assertExpectedHtml( + wrapper, + '
Spied component
Spied component
' + ); } ); } );