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

Wrap mount renderer render with reactTestUtils.act() #2034

Merged
merged 3 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-13/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"react/no-find-dom-node": 0,
"react/no-multi-comp": 0,
"no-underscore-dangle": 0,
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-14/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"react/no-find-dom-node": 0,
"react/no-multi-comp": 0,
"no-underscore-dangle": 0,
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-15.4/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"react/no-find-dom-node": 0,
"react/no-multi-comp": 0,
"no-underscore-dangle": 0,
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-15/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"react/no-find-dom-node": 0,
"react/no-multi-comp": 0,
"no-underscore-dangle": 0,
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-16.1/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"import/no-extraneous-dependencies": 0,
"import/no-unresolved": 0,
"import/extensions": 0,
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-16.2/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"import/no-extraneous-dependencies": 0,
"import/no-unresolved": 0,
"import/extensions": 0,
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-16.3/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"import/no-extraneous-dependencies": 0,
"import/no-unresolved": 0,
"import/extensions": 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-adapter-react-16/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "airbnb",
"root": true,
"rules": {
"max-len": 0,
"import/no-extraneous-dependencies": 0,
"import/no-unresolved": 0,
"import/extensions": 0,
Expand All @@ -13,7 +14,7 @@
},
"settings": {
"react": {
"version": "16.4.0",
"version": "16",
},
},
}
155 changes: 84 additions & 71 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ function getEmptyStateValue() {
return testRenderer._instance.state;
}

function wrapAct(fn) {
if (typeof TestUtils.act !== 'function') {
return fn();
}
let returnVal;
TestUtils.act(() => { returnVal = fn(); });
return returnVal;
}

class ReactSixteenAdapter extends EnzymeAdapter {
constructor() {
super();
Expand Down Expand Up @@ -316,25 +325,27 @@ class ReactSixteenAdapter extends EnzymeAdapter {
const adapter = this;
return {
render(el, context, callback) {
if (instance === null) {
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
context,
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, { ...options, adapter });
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = hydrateIn
? ReactDOM.hydrate(wrappedEl, domNode)
: ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
return wrapAct(() => {
if (instance === null) {
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
context,
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, { ...options, adapter });
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = hydrateIn
? ReactDOM.hydrate(wrappedEl, domNode)
: ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
}
} else {
instance.setChildProps(el.props, context, callback);
}
} else {
instance.setChildProps(el.props, context, callback);
}
});
},
unmount() {
ReactDOM.unmountComponentAtNode(domNode);
Expand Down Expand Up @@ -378,63 +389,65 @@ class ReactSixteenAdapter extends EnzymeAdapter {
let cachedNode = null;
return {
render(el, unmaskedContext) {
cachedNode = el;
/* eslint consistent-return: 0 */
if (typeof el.type === 'string') {
isDOM = true;
} else {
isDOM = false;
const { type: Component } = el;

const isStateful = Component.prototype && (
Component.prototype.isReactComponent
|| Array.isArray(Component.__reactAutoBindPairs) // fallback for createClass components
);

const context = getMaskedContext(Component.contextTypes, unmaskedContext);

if (!isStateful && isMemo(el.type)) {
const InnerComp = el.type.type;
const wrappedEl = Object.assign(
(...args) => InnerComp(...args), // eslint-disable-line new-cap
InnerComp,
return wrapAct(() => {
cachedNode = el;
/* eslint consistent-return: 0 */
if (typeof el.type === 'string') {
isDOM = true;
} else {
isDOM = false;
const { type: Component } = el;

const isStateful = Component.prototype && (
Component.prototype.isReactComponent
|| Array.isArray(Component.__reactAutoBindPairs) // fallback for createClass components
);
return withSetStateAllowed(() => renderer.render({ ...el, type: wrappedEl }, context));
}

if (!isStateful && typeof Component === 'function') {
const wrappedEl = Object.assign(
(...args) => Component(...args), // eslint-disable-line new-cap
Component,
);
return withSetStateAllowed(() => renderer.render({ ...el, type: wrappedEl }, context));
}
if (isStateful) {
// fix react bug; see implementation of `getEmptyStateValue`
const emptyStateValue = getEmptyStateValue();
if (emptyStateValue) {
Object.defineProperty(Component.prototype, 'state', {
configurable: true,
enumerable: true,
get() {
return null;
},
set(value) {
if (value !== emptyStateValue) {
Object.defineProperty(this, 'state', {
configurable: true,
enumerable: true,
value,
writable: true,
});
}
return true;
},
});
const context = getMaskedContext(Component.contextTypes, unmaskedContext);

if (!isStateful && isMemo(el.type)) {
const InnerComp = el.type.type;
const wrappedEl = Object.assign(
(...args) => InnerComp(...args), // eslint-disable-line new-cap
InnerComp,
);
return withSetStateAllowed(() => renderer.render({ ...el, type: wrappedEl }, context));
}

if (!isStateful && typeof Component === 'function') {
const wrappedEl = Object.assign(
(...args) => Component(...args), // eslint-disable-line new-cap
Component,
);
return withSetStateAllowed(() => renderer.render({ ...el, type: wrappedEl }, context));
}
if (isStateful) {
// fix react bug; see implementation of `getEmptyStateValue`
const emptyStateValue = getEmptyStateValue();
if (emptyStateValue) {
Object.defineProperty(Component.prototype, 'state', {
configurable: true,
enumerable: true,
get() {
return null;
},
set(value) {
if (value !== emptyStateValue) {
Object.defineProperty(this, 'state', {
configurable: true,
enumerable: true,
value,
writable: true,
});
}
return true;
},
});
}
}
return withSetStateAllowed(() => renderer.render(el, context));
}
return withSetStateAllowed(() => renderer.render(el, context));
}
});
},
unmount() {
renderer.unmount();
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme-adapter-react-helper/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
"rules": {
"max-len": 0,
},
"settings": {
"react": {
"version": "detect",
},
},
}
8 changes: 8 additions & 0 deletions packages/enzyme-adapter-utils/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
},
},
],
"rules": {
"max-len": 0,
},
"settings": {
"react": {
"version": "detect",
},
},
}
3 changes: 3 additions & 0 deletions packages/enzyme-test-suite/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"plugins": ["mocha"],
"settings": {
"mocha/additionalTestFunctions": ["itIf", "describeIf"],
"react": {
"version": "detect",
},
},
"rules": {
"max-len": 0,
Expand Down
38 changes: 38 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
forwardRef,
memo,
PureComponent,
useEffect,
useState,
} from './_helpers/react-compat';
import {
describeWithDOM,
Expand Down Expand Up @@ -648,6 +650,42 @@ describeWithDOM('mount', () => {
});
});

describeIf(is('>= 16.8'), 'hooks', () => {
it('works with `useEffect`', (done) => {
function ComponentUsingEffectHook() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 1e3);
}, []);
return (
<div>
{ctr}
</div>
);
}
const wrapper = mount(<ComponentUsingEffectHook />);

expect(wrapper.debug()).to.equal(`<ComponentUsingEffectHook>
<div>
1
</div>
</ComponentUsingEffectHook>`);

setTimeout(() => {
wrapper.update();
expect(wrapper.debug()).to.equal(`<ComponentUsingEffectHook>
<div>
2
</div>
</ComponentUsingEffectHook>`);
done();
}, 1e3);
});
});

describe('.contains(node)', () => {
it('allows matches on the root node', () => {
const a = <div className="foo" />;
Expand Down
35 changes: 35 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
forwardRef,
memo,
PureComponent,
useEffect,
useState,
} from './_helpers/react-compat';
import {
describeIf,
Expand Down Expand Up @@ -653,6 +655,39 @@ describe('shallow', () => {
});
});

describeIf(is('>= 16.8'), 'hooks', () => {
// TODO: enable when the shallow renderer fixes its bug
it.skip('works with `useEffect`', (done) => {
function ComponentUsingEffectHook() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setCtr(1);
setTimeout(() => {
setCtr(2);
}, 1e3);
}, []);
return (
<div>
{ctr}
</div>
);
}
const wrapper = shallow(<ComponentUsingEffectHook />);

expect(wrapper.debug()).to.equal(`<div>
1
</div>`);

setTimeout(() => {
wrapper.update();
expect(wrapper.debug()).to.equal(`<div>
2
</div>`);
done();
}, 1e3);
});
});

describe('.contains(node)', () => {
it('allows matches on the root node', () => {
const a = <div className="foo" />;
Expand Down