Skip to content

Commit

Permalink
Merge branch 'master' into feat/Profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Mar 27, 2019
2 parents b2e28b4 + 03ef729 commit 47d30ab
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 64 deletions.
4 changes: 2 additions & 2 deletions docs/api/ReactWrapper/reduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function Foo() {

```jsx
const wrapper = mount(<Foo />);
const total = wrapper.find(Bar).reduce((amount, n) => amount + n.prop('amount'));
expect(total).to.equal(16);
const total = wrapper.find(Bar).reduce((amount, n) => amount + n.prop('amount'), 0);
expect(total).to.equal(14);
```


Expand Down
4 changes: 2 additions & 2 deletions docs/api/ReactWrapper/reduceRight.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function Foo() {

```jsx
const wrapper = mount(<Foo />);
const total = wrapper.find(Bar).reduceRight((amount, n) => amount + n.prop('amount'));
expect(total).to.equal(16);
const total = wrapper.find(Bar).reduceRight((amount, n) => amount + n.prop('amount'), 0);
expect(total).to.equal(14);
```


Expand Down
4 changes: 2 additions & 2 deletions docs/api/ShallowWrapper/reduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function Foo() {

```jsx
const wrapper = shallow(<Foo />);
const total = wrapper.find(Bar).reduce((amount, n) => amount + n.prop('amount'));
expect(total).to.equal(16);
const total = wrapper.find(Bar).reduce((amount, n) => amount + n.prop('amount'), 0);
expect(total).to.equal(14);
```


Expand Down
4 changes: 2 additions & 2 deletions docs/api/ShallowWrapper/reduceRight.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function Foo() {

```jsx
const wrapper = shallow(<Foo />);
const total = wrapper.find(Bar).reduceRight((amount, n) => amount + n.prop('amount'));
expect(total).to.equal(16);
const total = wrapper.find(Bar).reduceRight((amount, n) => amount + n.prop('amount'), 0);
expect(total).to.equal(14);
```


Expand Down
51 changes: 39 additions & 12 deletions docs/guides/react-native.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ prop, that can be used a selector similar to `className` in standard React:
expect(wrapper.findWhere(node => node.prop('testID') === 'todo-item')).toExist();
```

## Example configuration for Jest
## Default example configuration for Jest and JSDOM replacement

To perform the necessary configuration in your testing framework, it is recommended to use a setup script,
such as with Jest's `setupFilesAfterEnv` setting.
Expand Down Expand Up @@ -104,23 +104,50 @@ copyProps(window, global);
* and inspect the DOM in tests.
*/
Enzyme.configure({ adapter: new Adapter() });
```

## Configure enzyme with other test libraries and include JSDOM on the fly

Update the file specified in `setupFilesAfterEnv`, in this case `setup-tests.js` in the project root:

```jsx
import 'react-native';
import 'jest-enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Enzyme from 'enzyme';

/**
* Set up Enzyme to mount to DOM, simulate events,
* and inspect the DOM in tests.
*/
Enzyme.configure({ adapter: new Adapter() });
```

### Create a separate test file

Create a file prefixed with enzyme.test.ts for example `component.enzyme.test.js`:

```jsx
/**
* Ignore some expected warnings
* see: https://jestjs.io/docs/en/tutorial-react.html#snapshot-testing-with-mocks-enzyme-and-react-16
* see https://github.com/Root-App/react-native-mock-render/issues/6
* @jest-environment jsdom
*/
const originalConsoleError = console.error;
console.error = (message) => {
if (message.startsWith('Warning:')) {
return;
}
import React from 'react';
import { mount } from 'enzyme';
import { Text } from '../../../component/text';

originalConsoleError(message);
};
describe('Component tested with airbnb enzyme', () => {
test('App mount with enzyme', () => {
const wrapper = mount(<Text />);
// other tests operations
});
});
```

You should then be able to start writing tests!
**The most important part is to ensure that the test runs with the `jestEnvironment` set to `jsdom`** - one way is to include a `/* @jest-environment jsdom */` comment at the top of the file.



Then you should then be able to start writing tests!

Note that you may want to perform some additional mocking around native components,
or if you want to perform snapshot testing against React Native components. Notice
Expand Down
7 changes: 4 additions & 3 deletions packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"license": "MIT",
"dependencies": {
"chai": "^4.1.2",
"create-react-class": "^15.6.3",
"enzyme": "^3.8.0",
"enzyme-adapter-utils": "^1.9.1",
"html-element-map": "^1.0.0",
Expand All @@ -40,21 +41,21 @@
"object-inspect": "^1.6.0",
"object.assign": "^4.1.0",
"prop-types": "^15.7.2",
"react-is": "^16.8.4",
"semver": "^5.6.0",
"sinon-sandbox": "^2.0.0",
"sinon": "^5.1.1"
},
"peerDependencies": {
"react": "^15.5.0",
"react-dom": "^15.5.0"
},
"devDependencies": {
"create-react-class": "^15.6.3",
"eslint": "^5.15.1",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-mocha": "^5.2.1",
"eslint-plugin-react": "^7.12.4",
"react-is": "^16.8.4"
"eslint-plugin-react": "^7.12.4"
}
}
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import React from 'react';
import wrap from 'mocha-wrap';
import sinon from 'sinon';
import sinon from 'sinon-sandbox';

import { mount, shallow } from 'enzyme';
import { get } from 'enzyme/build/configuration';
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/test/RSTTraversal-spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import sinon from 'sinon';
import sinon from 'sinon-sandbox';
import { expect } from 'chai';
import { elementToTree } from 'enzyme-adapter-utils';
import {
Expand Down
36 changes: 8 additions & 28 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import sinon from 'sinon';
import sinon from 'sinon-sandbox';
import wrap from 'mocha-wrap';
import isEqual from 'lodash.isequal';
import getData from 'html-element-map/getData';
Expand Down Expand Up @@ -417,26 +417,6 @@ describeWithDOM('mount', () => {
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});

itIf(is('< 16'), 'works with SFCs', () => {
const Foo = ({ foo }) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);

Foo.contextTypes = {
_: PropTypes.string,
};

const wrapper = mount(<Foo foo="qux" />, {
context: {
_: 'foo',
},
});
expect(wrapper.context('_')).to.equal('foo');
});
});
});

Expand Down Expand Up @@ -2768,7 +2748,7 @@ describeWithDOM('mount', () => {
const wrapper = mount(<SimpleComponent />);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(
Error,
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option', // eslint-disable-line max-len
'ReactWrapper::setContext() can only be called on a wrapper that was originally passed a context option', // eslint-disable-line max-len
);
});

Expand Down Expand Up @@ -2797,7 +2777,7 @@ describeWithDOM('mount', () => {
const wrapper = mount(<SimpleComponent />);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(
Error,
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option', // eslint-disable-line max-len
'ReactWrapper::setContext() can only be called on a wrapper that was originally passed a context option', // eslint-disable-line max-len
);
});
});
Expand Down Expand Up @@ -7518,12 +7498,12 @@ describeWithDOM('mount', () => {
}
}

const cDU = sinon.spy(DummyComp.prototype, 'componentDidUpdate');
const gDSFP = sinon.spy(DummyComp, 'getDerivedStateFromProps');
let cDU;
let gDSFP;

beforeEach(() => { // eslint-disable-line mocha/no-sibling-hooks
cDU.resetHistory();
gDSFP.resetHistory();
cDU = sinon.spy(DummyComp.prototype, 'componentDidUpdate');
gDSFP = sinon.spy(DummyComp, 'getDerivedStateFromProps');
});

it('with no state changes, calls both methods with a sync and async setProps', () => {
Expand Down Expand Up @@ -8997,7 +8977,7 @@ describeWithDOM('mount', () => {

onChange() {
// enzyme can't handle the update because `this` is a ReactComponent instance,
// not a ShallowWrapper instance.
// not a Wrapper instance.
this.setState({ foo: 'onChange update' });
}

Expand Down
15 changes: 8 additions & 7 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import sinon from 'sinon';
import sinon from 'sinon-sandbox';
import wrap from 'mocha-wrap';
import isEqual from 'lodash.isequal';
import getData from 'html-element-map/getData';
Expand Down Expand Up @@ -284,6 +284,7 @@ describe('shallow', () => {
);
SimpleComponent.contextTypes = { name: PropTypes.string };

const context = { name: 'foo' };
const wrapper = shallow(<SimpleComponent />, { context });

expect(wrapper.context().name).to.equal(context.name);
Expand Down Expand Up @@ -790,11 +791,11 @@ describe('shallow', () => {

expect(() => wrapper.contains({})).to.throw(
Error,
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.', // eslint-disable-line max-len
'ShallowWrapper::contains() can only be called with a ReactElement (or an array of them), a string, or a number as an argument.',
);
expect(() => wrapper.contains(() => ({}))).to.throw(
Error,
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.', // eslint-disable-line max-len
'ShallowWrapper::contains() can only be called with a ReactElement (or an array of them), a string, or a number as an argument.',
);
});

Expand Down Expand Up @@ -7727,12 +7728,12 @@ describe('shallow', () => {
}
}

const cDU = sinon.spy(DummyComp.prototype, 'componentDidUpdate');
const gDSFP = sinon.spy(DummyComp, 'getDerivedStateFromProps');
let cDU;
let gDSFP;

beforeEach(() => { // eslint-disable-line mocha/no-sibling-hooks
cDU.resetHistory();
gDSFP.resetHistory();
cDU = sinon.spy(DummyComp.prototype, 'componentDidUpdate');
gDSFP = sinon.spy(DummyComp, 'getDerivedStateFromProps');
});

it('with no state changes, calls both methods with a sync and async setProps', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/test/Utils-spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { expect } from 'chai';
import wrap from 'mocha-wrap';
import sinon from 'sinon';
import sinon from 'sinon-sandbox';
import {
childrenToSimplifiedArray,
nodeEqual,
Expand Down
5 changes: 5 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/beforeEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sinon from 'sinon-sandbox';

beforeEach(() => { // eslint-disable-line mocha/no-top-level-hooks
sinon.restore();
});
2 changes: 1 addition & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class ReactWrapper {
throw new Error('ReactWrapper::setContext() can only be called on the root');
}
if (!this[OPTIONS].context) {
throw new Error('ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option');
throw new Error('ReactWrapper::setContext() can only be called on a wrapper that was originally passed a context option');
}
this[RENDERER].render(this[UNRENDERED], context, () => this.update());
return this;
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ class ShallowWrapper {
contains(nodeOrNodes) {
const adapter = getAdapter(this[OPTIONS]);
if (!isReactElementAlike(nodeOrNodes, adapter)) {
throw new Error('ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.');
throw new Error('ShallowWrapper::contains() can only be called with a ReactElement (or an array of them), a string, or a number as an argument.');
}
const predicate = Array.isArray(nodeOrNodes)
? other => containsChildrenSubArray(
Expand Down
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--require ./packages/enzyme/withDom.js ./packages/enzyme-test-suite/test/_helpers/setupAdapters.js
--require ./packages/enzyme/withDom.js ./packages/enzyme-test-suite/test/_helpers/setupAdapters.js ./packages/enzyme-test-suite/test/_helpers/beforeEach.js
--compilers js:babel-core/register,jsx:babel-core/register
--extensions js,jsx

0 comments on commit 47d30ab

Please sign in to comment.