From c5d9cd7005bc7042b73ed8a3c26a89b15d138c04 Mon Sep 17 00:00:00 2001 From: Ramana Venkata Date: Mon, 15 Jun 2015 16:32:22 +0530 Subject: [PATCH] Test shallowEquality --- src/utils/shallowEqual.js | 7 -- test/utils/shallowEquality.spec.js | 134 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 test/utils/shallowEquality.spec.js diff --git a/src/utils/shallowEqual.js b/src/utils/shallowEqual.js index 563dbb9618..c5faea85f6 100644 --- a/src/utils/shallowEqual.js +++ b/src/utils/shallowEqual.js @@ -18,13 +18,6 @@ export default function shallowEqual(objA, objB) { return false; } - - var valA = objA[keysA[i]]; - var valB = objB[keysA[i]]; - - if (valA !== valB) { - return false; - } } return true; diff --git a/test/utils/shallowEquality.spec.js b/test/utils/shallowEquality.spec.js new file mode 100644 index 0000000000..8ba48e6bdd --- /dev/null +++ b/test/utils/shallowEquality.spec.js @@ -0,0 +1,134 @@ +import expect from 'expect'; +import shallowEqualScalar from '../../src/utils/shallowEqualScalar'; +import shallowEqual from '../../src/utils/shallowEqual'; + +describe('Utils', () => { + // More info: https://github.com/gaearon/redux/pull/75#issuecomment-111635748 + describe('shallowEqualScalar', () => { + it('returns true if both arguments are the same object', () => { + const o = { a: 1, b: 2 }; + expect(shallowEqualScalar(o, o)).toBe(true); + }); + + it('returns false if either argument is null', () => { + expect(shallowEqualScalar(null, {})).toBe(false); + expect(shallowEqualScalar({}, null)).toBe(false); + }); + + it('returns true if arguments fields are equal', () => { + expect( + shallowEqualScalar( + { a: 1, b: 2, c: undefined }, + { a: 1, b: 2, c: undefined } + ) + ).toBe(true); + + expect( + shallowEqualScalar( + { a: 1, b: 2, c: 3 }, + { a: 1, b: 2, c: 3 } + ) + ).toBe(true); + }); + + it('returns false if first argument has too many keys', () => { + expect( + shallowEqualScalar( + { a: 1, b: 2, c: 3 }, + { a: 1, b: 2 } + ) + ).toBe(false); + }); + + it('returns false if second argument has too many keys', () => { + expect( + shallowEqualScalar( + { a: 1, b: 2 }, + { a: 1, b: 2, c: 3 } + ) + ).toBe(false); + }); + + it('returns false if arguments have keys dont have same value', () => { + expect( + shallowEqualScalar( + { a: 1, b: 2 }, + { a: 1, b: 3 } + ) + ).toBe(false); + }); + + it('returns false if arguments have field that are objects', () => { + const o = {}; + expect( + shallowEqualScalar( + { a: 1, b: 2, c: o }, + { a: 1, b: 2, c: o } + ) + ).toBe(false); + }); + + it('returns false if arguments have different keys', () => { + expect( + shallowEqualScalar( + { a: 1, b: 2, c: undefined }, + { a: 1, bb: 2, c: undefined } + ) + ).toBe(false); + }); + }); + + describe('shallowEqual', () => { + it('returns true if arguments fields are equal', () => { + expect( + shallowEqual( + { a: 1, b: 2, c: undefined }, + { a: 1, b: 2, c: undefined } + ) + ).toBe(true); + + expect( + shallowEqual( + { a: 1, b: 2, c: 3 }, + { a: 1, b: 2, c: 3 } + ) + ).toBe(true); + + const o = {}; + expect( + shallowEqual( + { a: 1, b: 2, c: o }, + { a: 1, b: 2, c: o } + ) + ).toBe(true); + }); + + it('returns false if first argument has too many keys', () => { + expect( + shallowEqual( + { a: 1, b: 2, c: 3 }, + { a: 1, b: 2 } + ) + ).toBe(false); + }); + + it('returns false if second argument has too many keys', () => { + expect( + shallowEqual( + { a: 1, b: 2 }, + { a: 1, b: 2, c: 3 } + ) + ).toBe(false); + }); + + it('returns false if arguments have different keys', () => { + expect( + shallowEqual( + { a: 1, b: 2, c: undefined }, + { a: 1, bb: 2, c: undefined } + ) + ).toBe(false); + }); + }); + +});