Skip to content

Commit

Permalink
fix: Replaced lodash.compact by another solution to keep 0 values (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantoine authored Feb 9, 2017
1 parent 95001b0 commit 5ad7fd5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 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 @@
},
"license": "MIT",
"dependencies": {
"lodash.compact": "^3.0.1",
"lodash.filter": "^4.6.0",
"lodash.isplainobject": "^4.0.6",
"lodash.omitby": "^4.5.0",
"object-values": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/mount.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import compact from 'lodash.compact';
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';
import {typeName} from 'enzyme/build/Debug';
import {childrenOfNode} from 'enzyme/build/ShallowTraversal';
import {compact} from './utils';

function instToJson(inst) {
if (typeof inst === 'string' || typeof inst === 'number') return inst;
Expand Down
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compact from 'lodash.compact';
import {compact} from './utils';

const renderChildToJson = child => {
if(!child) return null;
Expand Down
2 changes: 1 addition & 1 deletion src/shallow.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import compact from 'lodash.compact';
import isPlainObject from 'lodash.isplainobject';
import omitBy from 'lodash.omitby';
import entries from 'object.entries';
import {propsOfNode} from 'enzyme/build/Utils';
import {typeName} from 'enzyme/build/Debug';
import {childrenOfNode} from 'enzyme/build/ShallowTraversal';
import {isElement} from 'enzyme/build/react-compat';
import {compact} from './utils';

function nodeToJson(node) {
if(!node) return node;
Expand Down
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import filter from 'lodash.filter';
import ShallowWrapper from 'enzyme/build/ShallowWrapper';
import ReactWrapper from 'enzyme/build/ReactWrapper';

Expand All @@ -9,3 +10,5 @@ export const isReactWrapper = wrapper => wrapper && wrapper.constructor && wrapp
export const isCheerioWrapper = wrapper => wrapper && wrapper.cheerio;

export const isEnzymeWrapper = wrapper => isShallowWrapper(wrapper) || isReactWrapper(wrapper) || isCheerioWrapper(wrapper);

export const compact = array => filter(array, item => item !== null && typeof item !== 'undefined' && item !== '');
8 changes: 8 additions & 0 deletions tests/core/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ exports[`test converts pure mount with mixed children 1`] = `

exports[`test handles a component which returns null 1`] = `<ClassWithNull />`;

exports[`test renders zero-children 1`] = `
<ComponentWithAZeroChildren>
<div>
0
</div>
</ComponentWithAZeroChildren>
`;

exports[`test skips undefined props 1`] = `
<BasicWithUndefined>
<button>
Expand Down
4 changes: 4 additions & 0 deletions tests/core/fixtures/pure-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export function BasicWithAList() {
</div>
);
}

export function ComponentWithAZeroChildren() {
return <div>{0}</div>;
}
7 changes: 6 additions & 1 deletion tests/core/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, BasicWithUndefined } from './fixtures/pure-function';
import { BasicPure, BasicWithUndefined, ComponentWithAZeroChildren } from './fixtures/pure-function';
import { BasicClass, ClassWithPure, ClassWithDirectPure, ClassWithDirectComponent, ClassWithNull } from './fixtures/class';

it('converts basic pure mount', () => {
Expand Down Expand Up @@ -75,3 +75,8 @@ it('skips undefined props', () => {

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

it('renders zero-children', () => {
const mounted = mount(<ComponentWithAZeroChildren />);
expect(mountToJson(mounted)).toMatchSnapshot();
});
21 changes: 6 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1265,15 +1265,14 @@ entities@^1.1.1, entities@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0"

enzyme@^2.4.1:
version "2.6.0"
resolved "https://registry.yarnpkg.com/enzyme/-/enzyme-2.6.0.tgz#148d742b25e2565f7e80870a0c92aea9be1b90ea"
enzyme@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/enzyme/-/enzyme-2.7.1.tgz#76370e1d99e91f73091bb8c4314b7c128cc2d621"
dependencies:
cheerio "^0.22.0"
function.prototype.name "^1.0.0"
in-publish "^2.0.0"
is-subset "^0.1.1"
lodash "^4.16.4"
lodash "^4.17.2"
object-is "^1.0.1"
object.assign "^4.0.4"
object.entries "^1.0.3"
Expand Down Expand Up @@ -1959,10 +1958,6 @@ imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"

in-publish@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/in-publish/-/in-publish-2.0.0.tgz#e20ff5e3a2afc2690320b6dc552682a9c7fadf51"

indent-string@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/indent-string/-/indent-string-2.1.0.tgz#8e2d48348742121b4a8218b7a137e9a52049dc80"
Expand Down Expand Up @@ -2643,15 +2638,11 @@ lodash.clonedeep@^3.0.0:
lodash._baseclone "^3.0.0"
lodash._bindcallback "^3.0.0"

lodash.compact@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash.compact/-/lodash.compact-3.0.1.tgz#540ce3837745975807471e16b4a2ba21e7256ca5"

lodash.defaults@^4.0.1:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"

lodash.filter@^4.4.0:
lodash.filter@^4.4.0, lodash.filter@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.filter/-/lodash.filter-4.6.0.tgz#668b1d4981603ae1cc5a6fa760143e480b4c4ace"

Expand Down Expand Up @@ -2731,7 +2722,7 @@ lodash@^3.6.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.16.4, lodash@^4.2.0, lodash@^4.3.0:
lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.2"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"

Expand Down

0 comments on commit 5ad7fd5

Please sign in to comment.