Skip to content

Commit

Permalink
get toMatchElement() to work with mount
Browse files Browse the repository at this point in the history
  • Loading branch information
finnigantime committed May 31, 2017
1 parent bc19037 commit 3e2ad19
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 63 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ expect(wrapper.find('input').at(1)).toHaveValue('bar');

| render | mount | shallow |
| -------|-------|-------- |
| no | no | yes |
| no | yes | yes |

Assert the wrapper matches the provided react instance. This is a matcher form of Enzyme's wrapper.matchesElement(), which returns a bool with no indication of what caused a failed match. This matcher includes the actual and expected debug trees as contextual information when it fails. NOTE: The semantics are slightly different than enzyme's wrapper.matchesElement(), which ignores props. This matcher does consider props when comparing the actual to the expected values.

Expand All @@ -478,9 +478,7 @@ const wrapper = shallow(<Fixture />); // mount/render/shallow when applicable

expect(wrapper).toMatchElement(<Fixture />);
expect(wrapper.find('span')).toMatchElement(
<div>
<span id="foo" className="bar" />
</div>
<span id="foo" className="bar" />
);
expect(wrapper).not.toMatchElement(<div />);
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`toMatchElement provides contextual information for the message 1`] = `
exports[`toMatchElement mount provides contextual information for the message 1`] = `
"
actual:
<Fixture>
<div>
<span id=\\"child\\" className=\\"foo\\" />
</div>
</Fixture>
expected:
<Fixture>
<div>
<span id=\\"child\\" className=\\"foo\\" />
</div>
</Fixture>
"
`;

exports[`toMatchElement mount provides contextual information for the message 2`] = `
"
actual:
<div>
<span id=\\"child\\" className=\\"foo\\" />
</div>
expected:
<div>
<span id=\\"child\\" className=\\"foo\\" />
</div>
"
`;

exports[`toMatchElement provides contextual information for the message 2`] = `
exports[`toMatchElement mount provides contextual information for the message 3`] = `
"
actual:
<Fixture>
<div>
<span id=\\"child\\" className=\\"foo\\" />
</div>
</Fixture>
expected:
<div />
"
`;

exports[`toMatchElement mount returns the message with the proper fail verbage 1`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement mount returns the message with the proper fail verbage 2`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement mount returns the message with the proper fail verbage 3`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement mount returns the message with the proper pass verbage 1`] = `"Expected actual value to match the expected value."`;

exports[`toMatchElement mount returns the message with the proper pass verbage 2`] = `"Expected actual value to match the expected value."`;

exports[`toMatchElement mount returns the message with the proper pass verbage 3`] = `"Expected actual value to match the expected value."`;

exports[`toMatchElement shallow provides contextual information for the message 1`] = `
"
actual:
<div>
Expand All @@ -28,7 +68,17 @@ expected:
"
`;

exports[`toMatchElement provides contextual information for the message 3`] = `
exports[`toMatchElement shallow provides contextual information for the message 2`] = `
"
actual:
<span id=\\"child\\" className=\\"foo\\" />
expected:
<span id=\\"child\\" className=\\"foo\\" />
"
`;

exports[`toMatchElement shallow provides contextual information for the message 3`] = `
"
actual:
<div>
Expand All @@ -40,20 +90,14 @@ expected:
"
`;

exports[`toMatchElement provides contextual information for the message 4`] = `Object {}`;

exports[`toMatchElement returns the message with the proper fail verbage 1`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper fail verbage 2`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper fail verbage 3`] = `"Did not expect actual value to match the expected value."`;
exports[`toMatchElement shallow returns the message with the proper fail verbage 1`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper fail verbage 4`] = `"toMatchElement must be called on a ShallowWrapper"`;
exports[`toMatchElement shallow returns the message with the proper fail verbage 2`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper pass verbage 1`] = `"Expected actual value to match the expected value."`;
exports[`toMatchElement shallow returns the message with the proper fail verbage 3`] = `"Did not expect actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper pass verbage 2`] = `"Expected actual value to match the expected value."`;
exports[`toMatchElement shallow returns the message with the proper pass verbage 1`] = `"Expected actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper pass verbage 3`] = `"Expected actual value to match the expected value."`;
exports[`toMatchElement shallow returns the message with the proper pass verbage 2`] = `"Expected actual value to match the expected value."`;

exports[`toMatchElement returns the message with the proper pass verbage 4`] = `"toMatchElement must be called on a ShallowWrapper"`;
exports[`toMatchElement shallow returns the message with the proper pass verbage 3`] = `"Expected actual value to match the expected value."`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,38 @@ function Fixture() {
}

describe('toMatchElement', () => {
const wrapper = shallow(<Fixture />);
const truthyResults = toMatchElement(wrapper, <Fixture />);
const truthyResults2 = toMatchElement(wrapper,
<div>
<span id="child" className="foo" />
</div>
);
const falsyResults = toMatchElement(wrapper, <div />);
const badInputResults = toMatchElement(mount(<Fixture />));
[shallow, mount].forEach(builder => {
describe(builder.name, () => {
const wrapper = builder(<Fixture />);
const truthyResults = toMatchElement(wrapper, <Fixture />);
const truthyResults2 = toMatchElement(wrapper.find('span'),
<span id="child" className="foo" />
);
const falsyResults = toMatchElement(wrapper, <div />);

it('returns the pass flag properly', () => {
expect(truthyResults.pass).toBeTruthy();
expect(truthyResults2.pass).toBeTruthy();
expect(falsyResults.pass).toBeFalsy();
expect(badInputResults.pass).toBeFalsy();
});
it('returns the pass flag properly', () => {
expect(truthyResults.pass).toBeTruthy();
expect(truthyResults2.pass).toBeTruthy();
expect(falsyResults.pass).toBeFalsy();
});

it('returns the message with the proper pass verbage', () => {
expect(truthyResults.message).toMatchSnapshot();
expect(truthyResults2.message).toMatchSnapshot();
expect(falsyResults.message).toMatchSnapshot();
expect(badInputResults.message).toMatchSnapshot();
});
it('returns the message with the proper pass verbage', () => {
expect(truthyResults.message).toMatchSnapshot();
expect(truthyResults2.message).toMatchSnapshot();
expect(falsyResults.message).toMatchSnapshot();
});

it('returns the message with the proper fail verbage', () => {
expect(truthyResults.negatedMessage).toMatchSnapshot();
expect(truthyResults2.negatedMessage).toMatchSnapshot();
expect(falsyResults.negatedMessage).toMatchSnapshot();
expect(badInputResults.negatedMessage).toMatchSnapshot();
});
it('returns the message with the proper fail verbage', () => {
expect(truthyResults.negatedMessage).toMatchSnapshot();
expect(truthyResults2.negatedMessage).toMatchSnapshot();
expect(falsyResults.negatedMessage).toMatchSnapshot();
});

it('provides contextual information for the message', () => {
expect(truthyResults.contextualInformation).toMatchSnapshot();
expect(truthyResults2.contextualInformation).toMatchSnapshot();
expect(falsyResults.contextualInformation).toMatchSnapshot();
expect(badInputResults.contextualInformation).toMatchSnapshot();
it('provides contextual information for the message', () => {
expect(truthyResults.contextualInformation).toMatchSnapshot();
expect(truthyResults2.contextualInformation).toMatchSnapshot();
expect(falsyResults.contextualInformation).toMatchSnapshot();
});
});
});
});
13 changes: 5 additions & 8 deletions packages/enzyme-matchers/src/assertions/toMatchElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
* @flow
*/

import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import type { Matcher } from '../../../../types/Matcher';
import type { EnzymeObject } from '../../../../types/EnzymeObject';
import isShallowWrapper from '../utils/isShallowWrapper';
import single from '../utils/single';

function toMatchElement(actualEnzymeWrapper:EnzymeObject, reactInstance:Object) : Matcher {
let expectedWrapper:EnzymeObject;
if (!isShallowWrapper(actualEnzymeWrapper)) {
return {
pass: false,
message: 'toMatchElement must be called on a ShallowWrapper',
negatedMessage: 'toMatchElement must be called on a ShallowWrapper',
contextualInformation: {},
};
expectedWrapper = mount(reactInstance);
} else {
expectedWrapper = shallow(reactInstance);
}

const expectedWrapper:EnzymeObject = shallow(reactInstance);
const actual = actualEnzymeWrapper.debug();
const expected = expectedWrapper.debug();
const pass = actual === expected;
Expand Down

0 comments on commit 3e2ad19

Please sign in to comment.