Fixing Mocha tests for React tests (#10818)

* Fixing Mocha tests for React tests

* Adding externals for Enzyme tests

* Adding comment linking to docs and changing window to true
This commit is contained in:
Chris Cowan 2017-03-21 14:10:50 -07:00 committed by GitHub
parent d0e83f2c9d
commit 8f51ec6d84
5 changed files with 150 additions and 136 deletions

View file

@ -1,67 +1,67 @@
// import React from 'react';
// import { expect } from 'chai';
// import { shallow } from 'enzyme';
// import sinon from 'sinon';
// import AddDeleteButtons from '../add_delete_buttons';
// import Tooltip from '../tooltip';
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import AddDeleteButtons from '../add_delete_buttons';
import Tooltip from '../tooltip';
// describe('<AddDeleteButtons />', () => {
describe('<AddDeleteButtons />', () => {
// it('calls onAdd={handleAdd}', () => {
// const handleAdd = sinon.spy();
// const wrapper = shallow(
// <AddDeleteButtons onAdd={handleAdd} />
// );
// wrapper.find('a').at(0).simulate('click');
// expect(handleAdd.calledOnce).to.equal(true);
// });
it('calls onAdd={handleAdd}', () => {
const handleAdd = sinon.spy();
const wrapper = shallow(
<AddDeleteButtons onAdd={handleAdd} />
);
wrapper.find('a').at(0).simulate('click');
expect(handleAdd.calledOnce).to.equal(true);
});
// it('calls onDelete={handleDelete}', () => {
// const handleDelete = sinon.spy();
// const wrapper = shallow(
// <AddDeleteButtons onDelete={handleDelete} />
// );
// wrapper.find('a').at(1).simulate('click');
// expect(handleDelete.calledOnce).to.equal(true);
// });
it('calls onDelete={handleDelete}', () => {
const handleDelete = sinon.spy();
const wrapper = shallow(
<AddDeleteButtons onDelete={handleDelete} />
);
wrapper.find('a').at(1).simulate('click');
expect(handleDelete.calledOnce).to.equal(true);
});
// it('calls onClone={handleClone}', () => {
// const handleClone = sinon.spy();
// const wrapper = shallow(
// <AddDeleteButtons onClone={handleClone} />
// );
// wrapper.find('a').at(0).simulate('click');
// expect(handleClone.calledOnce).to.equal(true);
// });
it('calls onClone={handleClone}', () => {
const handleClone = sinon.spy();
const wrapper = shallow(
<AddDeleteButtons onClone={handleClone} />
);
wrapper.find('a').at(0).simulate('click');
expect(handleClone.calledOnce).to.equal(true);
});
// it('disableDelete={true}', () => {
// const wrapper = shallow(
// <AddDeleteButtons disableDelete={true} />
// );
// expect(wrapper.find({ text: 'Delete' })).to.have.length(0);
// });
it('disableDelete={true}', () => {
const wrapper = shallow(
<AddDeleteButtons disableDelete={true} />
);
expect(wrapper.find({ text: 'Delete' })).to.have.length(0);
});
// it('disableAdd={true}', () => {
// const wrapper = shallow(
// <AddDeleteButtons disableAdd={true} />
// );
// expect(wrapper.find({ text: 'Add' })).to.have.length(0);
// });
it('disableAdd={true}', () => {
const wrapper = shallow(
<AddDeleteButtons disableAdd={true} />
);
expect(wrapper.find({ text: 'Add' })).to.have.length(0);
});
// it('should not display clone by default', () => {
// const wrapper = shallow(
// <AddDeleteButtons />
// );
// expect(wrapper.find({ text: 'Clone' })).to.have.length(0);
// });
it('should not display clone by default', () => {
const wrapper = shallow(
<AddDeleteButtons />
);
expect(wrapper.find({ text: 'Clone' })).to.have.length(0);
});
// it('should not display clone when disableAdd={true}', () => {
// const fn = sinon.spy();
// const wrapper = shallow(
// <AddDeleteButtons onClone={fn} disableAdd={true} />
// );
// expect(wrapper.find({ text: 'Clone' })).to.have.length(0);
// });
it('should not display clone when disableAdd={true}', () => {
const fn = sinon.spy();
const wrapper = shallow(
<AddDeleteButtons onClone={fn} disableAdd={true} />
);
expect(wrapper.find({ text: 'Clone' })).to.have.length(0);
});
// });
});

View file

@ -1,33 +1,33 @@
// import React from 'react';
// import { expect } from 'chai';
// import { shallow } from 'enzyme';
// import sinon from 'sinon';
// import YesNo from '../yes_no';
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import YesNo from '../yes_no';
// describe('<YesNo />', () => {
describe('<YesNo />', () => {
// it('call onChange={handleChange} on yes', () => {
// const handleChange = sinon.spy();
// const wrapper = shallow(
// <YesNo name="test" onChange={handleChange} />
// );
// wrapper.find('input').first().simulate('change');
// expect(handleChange.calledOnce).to.equal(true);
// expect(handleChange.firstCall.args[0]).to.eql({
// test: 1
// });
// });
it('call onChange={handleChange} on yes', () => {
const handleChange = sinon.spy();
const wrapper = shallow(
<YesNo name="test" onChange={handleChange} />
);
wrapper.find('input').first().simulate('change');
expect(handleChange.calledOnce).to.equal(true);
expect(handleChange.firstCall.args[0]).to.eql({
test: 1
});
});
// it('call onChange={handleChange} on no', () => {
// const handleChange = sinon.spy();
// const wrapper = shallow(
// <YesNo name="test" onChange={handleChange} />
// );
// wrapper.find('input').last().simulate('change');
// expect(handleChange.calledOnce).to.equal(true);
// expect(handleChange.firstCall.args[0]).to.eql({
// test: 0
// });
// });
it('call onChange={handleChange} on no', () => {
const handleChange = sinon.spy();
const wrapper = shallow(
<YesNo name="test" onChange={handleChange} />
);
wrapper.find('input').last().simulate('change');
expect(handleChange.calledOnce).to.equal(true);
expect(handleChange.firstCall.args[0]).to.eql({
test: 0
});
});
// });
});

View file

@ -1,58 +1,58 @@
// import React from 'react';
// import { expect } from 'chai';
// import { shallow } from 'enzyme';
// import sinon from 'sinon';
// import addScope from '../add_scope';
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import addScope from '../add_scope';
// const Component = React.createClass({
// render() {
// return (<div/>);
// }
// });
const Component = React.createClass({
render() {
return (<div/>);
}
});
// describe('addScope()', () => {
describe('addScope()', () => {
// let unsubStub;
// let watchCollectionStub;
// let $scope;
let unsubStub;
let watchCollectionStub;
let $scope;
// beforeEach(() => {
// unsubStub = sinon.stub();
// watchCollectionStub = sinon.stub().returns(unsubStub);
// $scope = {
// $watchCollection: watchCollectionStub,
// testOne: 1,
// testTwo: 2
// };
// });
beforeEach(() => {
unsubStub = sinon.stub();
watchCollectionStub = sinon.stub().returns(unsubStub);
$scope = {
$watchCollection: watchCollectionStub,
testOne: 1,
testTwo: 2
};
});
// it('adds $scope variables as props to wrapped component', () => {
// const WrappedComponent = addScope(Component, $scope, ['testOne', 'testTwo']);
// const wrapper = shallow(<WrappedComponent/>);
// expect(wrapper.state('testOne')).to.equal(1);
// expect(wrapper.state('testTwo')).to.equal(2);
// });
it('adds $scope variables as props to wrapped component', () => {
const WrappedComponent = addScope(Component, $scope, ['testOne', 'testTwo']);
const wrapper = shallow(<WrappedComponent/>);
expect(wrapper.state('testOne')).to.equal(1);
expect(wrapper.state('testTwo')).to.equal(2);
});
// it('calls $scope.$watchCollection on each scoped item', () => {
// const WrappedComponent = addScope(Component, $scope, ['testOne', 'testTwo']);
// const wrapper = shallow(<WrappedComponent/>);
// expect(watchCollectionStub.calledTwice).to.equal(true);
// expect(watchCollectionStub.firstCall.args[0]).to.equal('testOne');
// expect(watchCollectionStub.secondCall.args[0]).to.equal('testTwo');
// });
it('calls $scope.$watchCollection on each scoped item', () => {
const WrappedComponent = addScope(Component, $scope, ['testOne', 'testTwo']);
const wrapper = shallow(<WrappedComponent/>);
expect(watchCollectionStub.calledTwice).to.equal(true);
expect(watchCollectionStub.firstCall.args[0]).to.equal('testOne');
expect(watchCollectionStub.secondCall.args[0]).to.equal('testTwo');
});
// it('unsubscribes from watches', () => {
// const WrappedComponent = addScope(Component, $scope, ['testOne', 'testTwo']);
// const wrapper = shallow(<WrappedComponent/>);
// wrapper.unmount();
// expect(unsubStub.calledTwice).to.equal(true);
// });
it('unsubscribes from watches', () => {
const WrappedComponent = addScope(Component, $scope, ['testOne', 'testTwo']);
const wrapper = shallow(<WrappedComponent/>);
wrapper.unmount();
expect(unsubStub.calledTwice).to.equal(true);
});
// it('updates state when watch is called', () => {
// const WrappedComponent = addScope(Component, $scope, ['testOne']);
// const wrapper = shallow(<WrappedComponent/>);
// watchCollectionStub.firstCall.args[1].call(null, 3);
// expect(wrapper.state('testOne')).to.equal(3);
// });
it('updates state when watch is called', () => {
const WrappedComponent = addScope(Component, $scope, ['testOne']);
const wrapper = shallow(<WrappedComponent/>);
watchCollectionStub.firstCall.args[1].call(null, 3);
expect(wrapper.state('testOne')).to.equal(3);
});
// });
});

View file

@ -19,6 +19,7 @@ exports.nodePresets = [
}
],
require.resolve('babel-preset-stage-1'),
require.resolve('babel-preset-react'),
];
exports.webpackPresets = [

View file

@ -90,7 +90,7 @@ class BaseOptimizer {
return ExtractTextPlugin.extract(makeLoaderString(loaders));
};
return {
const config = {
node: { fs: 'empty' },
context: fromRoot('.'),
entry: this.bundles.toWebpackEntries(),
@ -163,6 +163,19 @@ class BaseOptimizer {
}, {})
}
};
// In the test env we need to add react-addons (and a few other bits) for the
// enzyme tests to work.
// https://github.com/airbnb/enzyme/blob/master/docs/guides/webpack.md
if (this.env.context.env === 'development') {
config.externals = {
'react/lib/ExecutionEnvironment': true,
'react/addons': true,
'react/lib/ReactContext': true,
};
}
return config;
}
pluginsForEnv(env) {