mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #3317 from ianobermiller/fix-shallow-equal
shallowEqual: bail if either argument is falsey
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014-2015, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*
|
||||||
|
* @emails react-core
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
require('mock-modules')
|
||||||
|
.dontMock('shallowEqual');
|
||||||
|
|
||||||
|
var shallowEqual;
|
||||||
|
|
||||||
|
describe('shallowEqual', function() {
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
shallowEqual = require('shallowEqual');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false if either argument is null', function() {
|
||||||
|
expect(shallowEqual(null, {})).toBe(false);
|
||||||
|
expect(shallowEqual({}, null)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true if both arguments are null or undefined', function() {
|
||||||
|
expect(shallowEqual(null, null)).toBe(true);
|
||||||
|
expect(shallowEqual(undefined, undefined)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true if arguments are shallow equal', function() {
|
||||||
|
expect(
|
||||||
|
shallowEqual(
|
||||||
|
{a: 1, b: 2, c: 3},
|
||||||
|
{a: 1, b: 2, c: 3}
|
||||||
|
)
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false if first argument has too many keys', function() {
|
||||||
|
expect(
|
||||||
|
shallowEqual(
|
||||||
|
{a: 1, b: 2, c: 3},
|
||||||
|
{a: 1, b: 2}
|
||||||
|
)
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false if second argument has too many keys', function() {
|
||||||
|
expect(
|
||||||
|
shallowEqual(
|
||||||
|
{a: 1, b: 2},
|
||||||
|
{a: 1, b: 2, c: 3}
|
||||||
|
)
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false if arguments are not shallow equal', function() {
|
||||||
|
expect(
|
||||||
|
shallowEqual(
|
||||||
|
{a: 1, b: 2, c: {}},
|
||||||
|
{a: 1, b: 2, c: {}}
|
||||||
|
)
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -22,6 +22,11 @@ function shallowEqual(objA, objB) {
|
|||||||
if (objA === objB) {
|
if (objA === objB) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!objA || !objB) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var key;
|
var key;
|
||||||
// Test for A's keys different from B.
|
// Test for A's keys different from B.
|
||||||
for (key in objA) {
|
for (key in objA) {
|
||||||
|
|||||||
Reference in New Issue
Block a user