Skip to content

Commit

Permalink
feat: Skip undefined props in snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantoine committed Nov 1, 2016
1 parent 6df7863 commit e010536
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"lodash.compact": "^3.0.1",
"lodash.isplainobject": "^4.0.6",
"lodash.omit": "^4.5.0",
"lodash.omitby": "^4.5.0",
"object-values": "^1.0.0",
"object.entries": "^1.0.3"
},
Expand Down
4 changes: 2 additions & 2 deletions src/mount.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import compact from 'lodash.compact';
import omit from 'lodash.omit';
import omitBy from 'lodash.omitby';
import values from 'object-values';
import {isDOMComponent, isElement} from 'enzyme/build/react-compat';
import {internalInstance, propsOfNode} from 'enzyme/build/Utils';
Expand All @@ -21,7 +21,7 @@ function instToJson(inst) {

const currentElement = inst._currentElement;
const type = typeName(currentElement);
const props = omit(propsOfNode(currentElement), 'children');
const props = omitBy(propsOfNode(currentElement), (val, key) => key === 'children' || val === undefined);
const children = [];
if (isDOMComponent(publicInst)) {
const renderedChildren = inst._renderedChildren;
Expand Down
4 changes: 2 additions & 2 deletions src/shallow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import compact from 'lodash.compact';
import isPlainObject from 'lodash.isplainobject';
import omit from 'lodash.omit';
import omitBy from 'lodash.omitby';
import entries from 'object.entries';
import {propsOfNode} from 'enzyme/build/Utils';
import {typeName} from 'enzyme/build/Debug';
Expand Down Expand Up @@ -31,7 +31,7 @@ function nodeToJson(node) {

const children = compact(childrenOfNode(node).map(n => nodeToJson(n)));
const type = typeName(node);
const props = omit(propsOfNode(node), 'children');
const props = omitBy(propsOfNode(node), (val, key) => key === 'children' || val === undefined);

return {
type,
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ exports[`test converts basic pure mount 1`] = `
</div>
</BasicPure>
`;

exports[`test skips undefined props 1`] = `
<BasicWithUndefined>
<button>
Hello
</button>
</BasicWithUndefined>
`;
6 changes: 6 additions & 0 deletions test/__snapshots__/shallow.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ exports[`test ignores non-plain objects 1`] = `
}
} />
`;

exports[`test skips undefined props 1`] = `
<button>
Hello
</button>
`;
6 changes: 6 additions & 0 deletions test/fixtures/pure-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ export function BasicPure(props) {
</div>
);
}

export function BasicWithUndefined(props) {
return (
<button disabled={props.disabled}>Hello</button>
);
}
10 changes: 9 additions & 1 deletion test/mount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { mountToJson } from '../src';
import { BasicPure } from './fixtures/pure-function';
import { BasicPure, BasicWithUndefined } from './fixtures/pure-function';
import { BasicClass, ClassWithPure, ClassWithDirectPure, ClassWithDirectComponent } from './fixtures/class';

it('converts basic pure mount', () => {
Expand Down Expand Up @@ -45,3 +45,11 @@ it('converts a class mount with a class component in it as a direct child', () =

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

it('skips undefined props', () => {
const mounted = mount(
<BasicWithUndefined>Hello!</BasicWithUndefined>
);

expect(mountToJson(mounted)).toMatchSnapshot();
});
10 changes: 9 additions & 1 deletion test/shallow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import { shallow } from 'enzyme';
import { shallowToJson } from '../src';
import { BasicPure } from './fixtures/pure-function';
import { BasicPure, BasicWithUndefined } from './fixtures/pure-function';
import { BasicClass, ClassWithPure, ClassWithNull } from './fixtures/class';

function WrapperComponent(props) {
Expand Down Expand Up @@ -80,3 +80,11 @@ it('ignores non-plain objects', () => {

expect(shallowToJson(shallowed)).toMatchSnapshot();
});

it('skips undefined props', () => {
const shallowed = shallow(
<BasicWithUndefined>Hello!</BasicWithUndefined>
);

expect(shallowToJson(shallowed)).toMatchSnapshot();
});

0 comments on commit e010536

Please sign in to comment.