Skip to content

Commit

Permalink
[Tests] add coverage:
Browse files Browse the repository at this point in the history
 - `Utils.AND`
 - enzyme-adapter-utils `simulateError`
 - shallow `invoke`
 - mount `.unmount`/`.mount`/`.detach` throwing
 - `.context` throwing
 - `:focus` when there is no DOM
 - `mount`: finding by HTML element constructor
 - `mount`: `state()` throws on multiple nodes
 - `setState`: works when adapter lacks `invokeSetStateCallback`
 - `simulateError`: throws when adapter’s renderer lacks `simulateError`
 - `shallow`: `getChildContext`: better cover when `childContextTypes` is omitted
 - `hasClass` warning for a selector-like classname
  • Loading branch information
ljharb committed Feb 29, 2020
1 parent 4c591f7 commit 72f3fde
Show file tree
Hide file tree
Showing 14 changed files with 1,011 additions and 660 deletions.
48 changes: 25 additions & 23 deletions packages/enzyme-test-suite/test/Adapter-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -677,35 +677,37 @@ describe('Adapter', () => {
});
});

it('render node with updated props', () => {
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
describeWithDOM('mount renderer', () => {
it('render node with updated props', () => {
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}

increment() {
this.setState(({ count }) => ({ count: count + 1 }));
}
increment() {
this.setState(({ count }) => ({ count: count + 1 }));
}

render() {
return <RendersNull {...this.state} />;
render() {
return <RendersNull {...this.state} />;
}
}
}

const options = { mode: 'mount' };
const renderer = adapter.createRenderer(options);
const options = { mode: 'mount' };
const renderer = adapter.createRenderer(options);

renderer.render(<Counter />);
renderer.render(<Counter />);

let tree = renderer.getNode();
expect(tree.rendered.props).to.have.property('count', 0);
tree.instance.increment();
tree = renderer.getNode();
expect(tree.rendered.props).to.have.property('count', 1);
tree.instance.increment();
tree = renderer.getNode();
expect(tree.rendered.props).to.have.property('count', 2);
let tree = renderer.getNode();
expect(tree.rendered.props).to.have.property('count', 0);
tree.instance.increment();
tree = renderer.getNode();
expect(tree.rendered.props).to.have.property('count', 1);
tree.instance.increment();
tree = renderer.getNode();
expect(tree.rendered.props).to.have.property('count', 2);
});
});

it('renders basic shallow as well', () => {
Expand Down
48 changes: 25 additions & 23 deletions packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,11 @@ describe('debug', () => {
ParentOfNamed = () => <NamedComponent />;
});

it('works with a `mount` wrapper', () => {
const wrapper = mount(<Parent foo="hello" />);
expect(wrapper.debug()).to.equal((
`<Parent foo="hello">
describeWithDOM('', () => {
it('works with a `mount` wrapper', () => {
const wrapper = mount(<Parent foo="hello" />);
expect(wrapper.debug()).to.equal((
`<Parent foo="hello">
<span>
<ForwardRef foo="hello">
<div>
Expand All @@ -877,19 +878,31 @@ describe('debug', () => {
</ForwardRef>
</span>
</Parent>`
));
});
));
});

it('works with a `mount` `.find` wrapper', () => {
const wrapper = mount(<Parent foo="hello" />);
const results = wrapper.find(SomeComponent);
expect(results.debug()).to.equal((
`<ForwardRef foo="hello">
it('works with a `mount` `.find` wrapper', () => {
const wrapper = mount(<Parent foo="hello" />);
const results = wrapper.find(SomeComponent);
expect(results.debug()).to.equal((
`<ForwardRef foo="hello">
<div>
<span className="child1" />
</div>
</ForwardRef>`
));
));
});

it('works with a displayName with mount', () => {
const wrapper = mount(<ParentOfNamed />);
expect(wrapper.debug()).to.equal((
`<ParentOfNamed>
<${NamedComponent.displayName}>
<div />
</${NamedComponent.displayName}>
</ParentOfNamed>`
));
});
});

it('works with a displayName with shallow', () => {
Expand All @@ -898,16 +911,5 @@ describe('debug', () => {
`<${NamedComponent.displayName} />`
));
});

it('works with a displayName with mount', () => {
const wrapper = mount(<ParentOfNamed />);
expect(wrapper.debug()).to.equal((
`<ParentOfNamed>
<${NamedComponent.displayName}>
<div />
</${NamedComponent.displayName}>
</ParentOfNamed>`
));
});
});
});
39 changes: 39 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,17 @@ describeWithDOM('mount', () => {
expect(didMount).to.have.property('callCount', 2);
expect(willUnmount).to.have.property('callCount', 1);
});

it('throws on non-root', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}
const wrapper = mount(<Foo />);
const child = wrapper.find('div');
expect(() => child.mount()).to.throw(Error);
});
});

describe('.getDOMNode()', () => {
Expand Down Expand Up @@ -1401,6 +1412,34 @@ describeWithDOM('mount', () => {
});
});

describe('.detach', () => {
class Comp extends React.Component {
render() {
return <div><span>hi</span></div>;
}
}
it('throws on non-root', () => {
const div = global.document.createElement('div');
global.document.body.appendChild(div);

const wrapper = mount(<Comp />, { attachTo: div });
const span = wrapper.find('span');
expect(span).to.have.lengthOf(1);
expect(() => span.detach()).to.throw(
Error,
'ReactWrapper::detach() can only be called on the root',
);
});

it('throws without the attachTo option', () => {
const wrapper = mount(<Comp />);
expect(() => wrapper.detach()).to.throw(
Error,
'ReactWrapper::detach() can only be called on when the `attachTo` option was passed into `mount()`.',
);
});
});

describe('attachTo option', () => {
it('attaches and stuff', () => {
class Foo extends React.Component {
Expand Down
34 changes: 34 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { expect } from 'chai';
import sinon from 'sinon-sandbox';
import wrap from 'mocha-wrap';
import isEqual from 'lodash.isequal';
import inspect from 'object-inspect';

import {
shallow,
ShallowWrapper,
Expand Down Expand Up @@ -940,6 +942,37 @@ describe('shallow', () => {
expect(consumer.context()).to.eql(expectedContext);
});

class Provider extends React.Component {
getChildContext() {
return {
foo: 'foo!',
bar: 'bar!',
};
}

render() {
const { children } = this.props;
return children;
}
}

class Receiver extends React.Component {
render() {
return <div>{inspect(this.context)}</div>;
}
}

// react 0.14 and 15 throw an invariant exception in this case
itIf(is('0.13 || > 15'), 'warns and works but provides no context, without childContextTypes', () => {
const stub = sinon.stub(console, 'warn');
const wrapper = shallow(<Provider><Receiver /></Provider>).dive();
expect(wrapper.debug()).to.equal(`<div>
{}
</div>`);
expect(stub).to.have.property('callCount', 1);
expect(stub.args).to.eql([['Provider.getChildContext(): childContextTypes must be defined in order to use getChildContext().']]);
});

wrap()
.withConsoleThrows()
.it('warns if childContextTypes is not defined', () => {
Expand Down Expand Up @@ -1253,6 +1286,7 @@ describe('shallow', () => {
'hostNodes',
'html',
'instance',
'invoke',
'is',
'isEmpty',
'isEmptyRender',
Expand Down
44 changes: 44 additions & 0 deletions packages/enzyme-test-suite/test/Utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
renderedDive,
isCustomComponent,
loadCheerioRoot,
AND,
} from 'enzyme/build/Utils';
import getAdapter from 'enzyme/build/getAdapter';
import EnzymeAdapter from 'enzyme/build/EnzymeAdapter';
Expand Down Expand Up @@ -1087,4 +1088,47 @@ describe('Utils', () => {
expect(loadCheerioRoot('<div>malformed</><<html')).to.have.property('cheerio', '[cheerio object]');
});
});

describe('AND', () => {
it('throws on a non-array', () => {
expect(() => AND({})).to.throw(TypeError);
});

it('does not mutate the array', () => {
const fn1 = () => {};
const fn2 = () => {};
const arr = [fn1, fn2];
AND(arr);
expect(arr).to.eql([fn1, fn2]);
});

it('returns a thunk that calls each function in reverse order', () => {
const spy = sinon.spy();
const fn1 = (...args) => {
spy(1, args);
return true;
};
const fn2 = (...args) => {
spy(2, args);
return true;
};

const thunk = AND([fn1, fn2]);

expect(thunk).to.be.a('function');
expect(thunk()).to.equal(true);

expect(spy).to.have.property('callCount', 2);
const { args } = spy;
expect(args).to.eql([
[2, [undefined]],
[1, [undefined]],
]);
});

it('matches `every`', () => {
expect(AND([() => true, () => false])()).to.equal(false);
expect(AND([() => true, () => true])()).to.equal(true);
});
});
});

0 comments on commit 72f3fde

Please sign in to comment.