Skip to content

Commit

Permalink
fix(mountToJson): Fixed a bug where instrumentation code shows up in …
Browse files Browse the repository at this point in the history
…the snapshot output

#13
  • Loading branch information
adriantoine committed Oct 3, 2016
1 parent dffe929 commit 0c68c3a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"lodash.compact": "^3.0.1",
"lodash.get": "^4.4.2",
"lodash.omit": "^4.5.0",
"lodash.pickby": "^4.6.0"
"lodash.pickby": "^4.6.0",
"react-display-name": "^0.2.0"
},
"peerDependencies": {
"enzyme": "^2.4.1"
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import omit from 'lodash.omit';
import pickBy from 'lodash.pickby';
import compact from 'lodash.compact';
import get from 'lodash.get';
import getDisplayName from 'react-display-name';

export function shallowToJson(wrapper) {
const type = wrapper.type();
Expand Down Expand Up @@ -33,7 +34,7 @@ export function shallowToJson(wrapper) {

const jsonChildren = json.children;
json.children = [{
type: element.type,
type: getDisplayName(element.type),
props: omit(element.props, 'children'),
children: jsonChildren,
$$typeof: Symbol.for('react.test.json'),
Expand Down
55 changes: 55 additions & 0 deletions test/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
exports[`test converts a class mount with a class component in it as a direct child 1`] = `
<ClassWithDirectComponent
className="class">
<ClassWithPure
className="nested-pure">
<BasicPure
className="nested-pure">
<div
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<span>
<span>
<strong />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</div>
</BasicPure>
</ClassWithPure>
</ClassWithDirectComponent>
`;

exports[`test converts a class mount with a pure function in it 1`] = `
<ClassWithPure
className="class">
Expand Down Expand Up @@ -28,6 +61,28 @@ exports[`test converts a class mount with a pure function in it 1`] = `
</ClassWithPure>
`;

exports[`test converts a class mount with a pure function in it as a direct child 1`] = `
<ClassWithDirectPure
className="class">
<BasicPure
className="nested-pure">
<div
className="group"
id="group-id">
<span>
<span>
<strong />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</BasicPure>
</ClassWithDirectPure>
`;

exports[`test converts basic class mount 1`] = `
<BasicClass
className="class">
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ export class ClassWithPure extends Component {
);
}
}

export class ClassWithDirectPure extends Component {
render() {
return (
<BasicPure className="nested-pure">
<span>{this.props.children}</span>
<span className="empty"></span>
</BasicPure>
);
}
}

export class ClassWithDirectComponent extends Component {
render() {
return (
<ClassWithPure className="nested-pure">
<span>{this.props.children}</span>
<span className="empty"></span>
</ClassWithPure>
);
}
}
18 changes: 17 additions & 1 deletion test/mount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import { mount } from 'enzyme';
import { mountToJson } from '../src';
import { BasicPure } from './fixtures/pure-function';
import { BasicClass, ClassWithPure } from './fixtures/class';
import { BasicClass, ClassWithPure, ClassWithDirectPure, ClassWithDirectComponent } from './fixtures/class';

it('converts basic pure mount', () => {
const mounted = mount(
Expand All @@ -29,3 +29,19 @@ it('converts a class mount with a pure function in it', () => {

expect(mountToJson(mounted)).toMatchSnapshot();
});

it('converts a class mount with a pure function in it as a direct child', () => {
const mounted = mount(
<ClassWithDirectPure className="class"><strong>Hello!</strong></ClassWithDirectPure>
);

expect(mountToJson(mounted)).toMatchSnapshot();
});

it('converts a class mount with a class component in it as a direct child', () => {
const mounted = mount(
<ClassWithDirectComponent className="class"><strong>Hello!</strong></ClassWithDirectComponent>
);

expect(mountToJson(mounted)).toMatchSnapshot();
});

0 comments on commit 0c68c3a

Please sign in to comment.