Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch away from using enzyme.mount in tests (components/higher-order/with-filters/test/index.js) #7793

Conversation

nerrad
Copy link
Contributor

@nerrad nerrad commented Jul 8, 2018

Description

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).

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

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).
@nerrad nerrad requested a review from gziolo July 8, 2018 20:45
@nerrad nerrad self-assigned this Jul 8, 2018
@nerrad nerrad added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Jul 8, 2018

/**
* Internal dependencies
*/
import withFilters from '..';

const assertExpectedHtml = ( wrapper, containerTag, expectedHTML ) => {
const element = TestUtils.findRenderedDOMComponentWithTag( wrapper, containerTag );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically speaking, we should be searching for a root component in here. Did you consider using https://reactjs.org/docs/test-utils.html#findrenderedcomponentwithtype instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow why I want to search for the root component here. Can you elaborate?TestUtils.findRenderedComponentWithType returns a component instance, what I want here is the DOM Element matching the given tag (wrapper is the root component which the given element is derived from).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm I think I grasp now the reason for your comment. It's to ensure the html output for the entire component matches the expectation.

import { mount, shallow } from 'enzyme';
import { shallow } from 'enzyme';
import TestUtils from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this shouldn't refer react-dom directly, but rather @wordpress/element. I'm not quite sure what is the best way to handle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm sure, but then we'd be indirectly testing the export from @wordpress/element right? This is being used more as a test utility in this case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s fine as is for the time being. We need to create our own lib anyways 😃

@gziolo gziolo requested review from gziolo and aduth July 9, 2018 07:16
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good now, thanks for addressing my feedback. I have some smaller thing ls to address before merging.

import { mount, shallow } from 'enzyme';
import { shallow } from 'enzyme';
import TestUtils from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s fine as is for the time being. We need to create our own lib anyways 😃


expect( wrapper.html() ).toBe( '<div>My component</div>' );
expect( shallowWrapper.html() ).toBe( '<div>My component</div>' );
shallowWrapper.unmount();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do it, afterEach will handle it

@@ -82,12 +111,15 @@ describe( 'withFilters', () => {
);
const EnhancedComponent = withFilters( hookName )( SpiedComponent );

wrapper = mount( <EnhancedComponent /> );
wrapper = TestUtils.renderIntoDocument( getTestComponent( EnhancedComponent ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use destructuring when importing TestUtils to avoid using the variable??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestUtils is the default export from react-dom/test-utils. So I'd have to destructure on a subsequent statement. If renderIntoDocument was available as a direct import I'd definitely do it but its not. For now, I think it's a bit useful knowing at a glance that renderIntoDocument is part of TestUtils?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s quite surprising but good yo know 😃

@gziolo gziolo merged commit c72f5b4 into master Jul 10, 2018
@gziolo gziolo deleted the update/replace-usage-of-enzyme.mount-with-React.TestUtils-with-filters.js branch July 10, 2018 04:47
@gziolo gziolo added this to the 3.3 milestone Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants