-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [enzyme-adapter-react-16, enzyme] use `enzyme-shallow-equal`
- Loading branch information
Showing
15 changed files
with
160 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.eslintignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"root": true, | ||
"extends": "airbnb-base" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.npmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/enzyme-test-suite/test/enzyme-shallow-equal-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters