Skip to content

Commit

Permalink
[enzyme-shallow-equal] v1.0.0
Browse files Browse the repository at this point in the history
 - [enzyme-adapter-react-16, enzyme] use `enzyme-shallow-equal`
  • Loading branch information
ljharb committed Jun 16, 2019
1 parent e3d6d5e commit 399db46
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-adapter-react-16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"license": "MIT",
"dependencies": {
"enzyme-adapter-utils": "^1.12.0",
"enzyme-shallow-equal": "^1.0.0",
"has": "^1.0.3",
"object.assign": "^4.1.0",
"object.values": "^1.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
Suspense,
} from 'react-is';
import { EnzymeAdapter } from 'enzyme';
import { typeOfNode, shallowEqual } from 'enzyme/build/Utils';
import { typeOfNode } from 'enzyme/build/Utils';
import shallowEqual from 'enzyme-shallow-equal';
import {
displayNameOfNode,
elementToTree as utilElementToTree,
Expand Down
7 changes: 7 additions & 0 deletions packages/enzyme-shallow-equal/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": ["airbnb"],
"plugins": [
["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }],
],
sourceMaps: "both",
}
1 change: 1 addition & 0 deletions packages/enzyme-shallow-equal/.eslintignore
4 changes: 4 additions & 0 deletions packages/enzyme-shallow-equal/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "airbnb-base"
}
1 change: 1 addition & 0 deletions packages/enzyme-shallow-equal/.npmignore
1 change: 1 addition & 0 deletions packages/enzyme-shallow-equal/.npmrc
5 changes: 5 additions & 0 deletions packages/enzyme-shallow-equal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# enzyme-shallow-equal

This is the implementation of shallowEqual that enzyme uses.

It's adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68
53 changes: 53 additions & 0 deletions packages/enzyme-shallow-equal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "enzyme-shallow-equal",
"version": "1.0.0",
"description": "Adaptation of react-addons-shallow-compare, for independent usage",
"homepage": "http://airbnb.io/enzyme/",
"main": "build",
"scripts": {
"clean": "rimraf build",
"lint": "eslint --ext js,jsx .",
"pretest": "npm run lint",
"prebuild": "npm run clean",
"build": "babel --source-maps=both src --out-dir build",
"watch": "npm run build -- -w",
"prepublish": "npm run build && safe-publish-latest && (not-in-publish || cp ../../LICENSE.md ./)"
},
"repository": {
"type": "git",
"url": "https://github.com/airbnb/enzyme.git",
"directory": "packages/enzyme-shallow-equal"
},
"keywords": [
"javascript",
"shallow rendering",
"shallowRender",
"test",
"reactjs",
"react",
"flux",
"testing",
"test utils",
"assertion helpers",
"tdd",
"mocha"
],
"author": "Jordan Harband <ljharb@gmail.com>",
"license": "MIT",
"dependencies": {
"has": "^1.0.3",
"object-is": "^1.0.1"
},
"peerDependencies": {},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-replace-object-assign": "^1.0.0",
"babel-preset-airbnb": "^2.6.0",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.3",
"in-publish": "^2.0.0",
"rimraf": "^2.6.3",
"safe-publish-latest": "^1.1.2"
}
}
32 changes: 32 additions & 0 deletions packages/enzyme-shallow-equal/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import is from 'object-is';
import has from 'has';

// adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68
export default function shallowEqual(objA, objB) {
if (is(objA, objB)) {
return true;
}

if (!objA || !objB || typeof objA !== 'object' || typeof objB !== 'object') {
return false;
}

const keysA = Object.keys(objA);
const keysB = Object.keys(objB);

if (keysA.length !== keysB.length) {
return false;
}

keysA.sort();
keysB.sort();

// Test for A's keys different from B.
for (let i = 0; i < keysA.length; i += 1) {
if (!has(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}

return true;
}
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"enzyme": "^3.9.0",
"enzyme-adapter-react-helper": "^1.3.3",
"enzyme-adapter-utils": "^1.10.1",
"enzyme-shallow-equal": "^1.0.0",
"html-element-map": "^1.0.1",
"jsdom": "^6.5.1",
"lodash.isequal": "^4.5.0",
Expand Down
49 changes: 49 additions & 0 deletions packages/enzyme-test-suite/test/enzyme-shallow-equal-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import shallowEqual from 'enzyme-shallow-equal';

import { expect } from 'chai';

describe('shallowEqual', () => {
it('returns true for things that are SameValue', () => {
[{}, [], NaN, 42, 'foo', '', 0, Infinity, () => {}, /a/g, true, false, null, undefined].forEach((x) => {
expect(shallowEqual(x, x)).to.equal(true);
});
});

it('returns false if one is falsy and one is truthy', () => {
[null, undefined, false, 0, NaN].forEach((x) => {
expect(shallowEqual(true, x)).to.equal(false);
});
});

it('returns true if both have zero keys', () => {
expect(shallowEqual({}, {})).to.equal(true);
});

it('returns false if they have different numbers of keys', () => {
expect(shallowEqual({ a: 1 }, {})).to.equal(false);
expect(shallowEqual({}, { a: 1 })).to.equal(false);
});

it('returns false if they have the same number, but differently named, keys', () => {
expect(shallowEqual({ a: 1 }, { b: 1 })).to.equal(false);
});

it('returns false if they have the same keys, with different values', () => {
[{}, [], NaN, 42, 'foo', '', 0, Infinity, () => {}, /a/g, true, false, null, undefined].forEach((x) => {
expect(shallowEqual({ a: x }, { a: {} })).to.equal(false);
expect(shallowEqual({ a: {} }, { a: x })).to.equal(false);
});
});

it('returns false if an undefined key in one is absent in the other', () => {
expect(shallowEqual({ a: undefined, b: true }, { b: true, c: undefined })).to.equal(false);
expect(shallowEqual({ c: undefined, b: true }, { b: true, a: undefined })).to.equal(false);
});

it('returns true if they have the same keys, with the same values', () => {
[{}, [], NaN, 42, 'foo', '', 0, Infinity, () => {}, /a/g, true, false, null, undefined].forEach((x) => {
expect(shallowEqual({ a: x }, { a: x })).to.equal(true);
expect(shallowEqual({ a: x }, { a: x })).to.equal(true);
});
});
});
1 change: 1 addition & 0 deletions packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"array.prototype.flat": "^1.2.1",
"cheerio": "^1.0.0-rc.2",
"enzyme-shallow-equal": "^1.0.0",
"function.prototype.name": "^1.1.0",
"has": "^1.0.3",
"html-element-map": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import flat from 'array.prototype.flat';
import cheerio from 'cheerio';
import has from 'has';
import shallowEqual from 'enzyme-shallow-equal';

import {
nodeEqual,
Expand All @@ -18,7 +19,6 @@ import {
privateSet,
cloneElement,
spyMethod,
shallowEqual,
isEmptyValue,
} from './Utils';
import getAdapter from './getAdapter';
Expand Down
31 changes: 1 addition & 30 deletions packages/enzyme/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,36 +330,7 @@ export function spyMethod(instance, methodName, getStub = () => {}) {
};
}

// adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68
export function shallowEqual(objA, objB) {
if (is(objA, objB)) {
return true;
}


if (!objA || !objB || typeof objA !== 'object' || typeof objB !== 'object') {
return false;
}

const keysA = Object.keys(objA);
const keysB = Object.keys(objB);

if (keysA.length !== keysB.length) {
return false;
}

keysA.sort();
keysB.sort();

// Test for A's keys different from B.
for (let i = 0; i < keysA.length; i += 1) {
if (!has(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}

return true;
}
export { default as shallowEqual } from 'enzyme-shallow-equal';

export function isEmptyValue(renderedValue) {
return renderedValue === null || renderedValue === false;
Expand Down

0 comments on commit 399db46

Please sign in to comment.