Files
react-native/Libraries/Utilities/differ/__tests__/deepDiffer-test.js
T
James Ide 0ee5f68929 Migrate "Libraries" from Haste to standard path-based requires (sans vendor & renderers) (#24749)
Summary:
This is the next step in moving RN towards standard path-based requires. All the requires in `Libraries` have been rewritten to use relative requires with a few exceptions, namely, `vendor` and `Renderer/oss` since those need to be changed upstream. This commit uses relative requires instead of `react-native/...` so that if Facebook were to stop syncing out certain folders and therefore remove code from the react-native package, internal code at Facebook would not need to change.

See the umbrella issue at https://github.com/facebook/react-native/issues/24316 for more detail.

[General] [Changed] - Migrate "Libraries" from Haste to standard path-based requires
Pull Request resolved: https://github.com/facebook/react-native/pull/24749

Differential Revision: D15258017

Pulled By: cpojer

fbshipit-source-id: a1f480ea36c05c659b6f37c8f02f6f9216d5a323
2019-05-08 08:48:59 -07:00

132 lines
4.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';
const deepDiffer = require('../deepDiffer');
describe('deepDiffer', function() {
it('should diff primitives of the same type', () => {
expect(deepDiffer(1, 2)).toBe(true);
expect(deepDiffer(42, 42)).toBe(false);
expect(deepDiffer('foo', 'bar')).toBe(true);
expect(deepDiffer('foo', 'foo')).toBe(false);
expect(deepDiffer(true, false)).toBe(true);
expect(deepDiffer(false, true)).toBe(true);
expect(deepDiffer(true, true)).toBe(false);
expect(deepDiffer(false, false)).toBe(false);
expect(deepDiffer(null, null)).toBe(false);
expect(deepDiffer(undefined, undefined)).toBe(false);
});
it('should diff primitives of different types', () => {
expect(deepDiffer(1, '1')).toBe(true);
expect(deepDiffer(true, 'true')).toBe(true);
expect(deepDiffer(true, 1)).toBe(true);
expect(deepDiffer(false, 0)).toBe(true);
expect(deepDiffer(null, undefined)).toBe(true);
expect(deepDiffer(null, 0)).toBe(true);
expect(deepDiffer(null, false)).toBe(true);
expect(deepDiffer(null, '')).toBe(true);
expect(deepDiffer(undefined, 0)).toBe(true);
expect(deepDiffer(undefined, false)).toBe(true);
expect(deepDiffer(undefined, '')).toBe(true);
});
it('should diff Objects', () => {
expect(deepDiffer({}, {})).toBe(false);
expect(deepDiffer({}, null)).toBe(true);
expect(deepDiffer(null, {})).toBe(true);
expect(deepDiffer({a: 1}, {a: 1})).toBe(false);
expect(deepDiffer({a: 1}, {a: 2})).toBe(true);
expect(deepDiffer({a: 1}, {a: 1, b: null})).toBe(true);
expect(deepDiffer({a: 1}, {a: 1, b: 1})).toBe(true);
expect(deepDiffer({a: 1, b: 1}, {a: 1})).toBe(true);
expect(deepDiffer({a: {A: 1}, b: 1}, {a: {A: 1}, b: 1})).toBe(false);
expect(deepDiffer({a: {A: 1}, b: 1}, {a: {A: 2}, b: 1})).toBe(true);
expect(
deepDiffer(
{a: {A: {aA: 1, bB: 1}}, b: 1},
{a: {A: {aA: 1, bB: 1}}, b: 1},
),
).toBe(false);
expect(
deepDiffer(
{a: {A: {aA: 1, bB: 1}}, b: 1},
{a: {A: {aA: 1, cC: 1}}, b: 1},
),
).toBe(true);
});
it('should diff Arrays', () => {
expect(deepDiffer([], [])).toBe(false);
expect(deepDiffer([], null)).toBe(true);
expect(deepDiffer(null, [])).toBe(true);
expect(deepDiffer([42], [42])).toBe(false);
expect(deepDiffer([1], [2])).toBe(true);
expect(deepDiffer([1, 2, 3], [1, 2, 3])).toBe(false);
expect(deepDiffer([1, 2, 3], [1, 2, 4])).toBe(true);
expect(deepDiffer([1, 2, 3], [1, 4, 3])).toBe(true);
expect(deepDiffer([1, 2, 3, 4], [1, 2, 3])).toBe(true);
expect(deepDiffer([1, 2, 3], [1, 2, 3, 4])).toBe(true);
expect(deepDiffer([0, null, false, ''], [0, null, false, ''])).toBe(false);
expect(deepDiffer([0, null, false, ''], ['', false, null, 0])).toBe(true);
});
it('should diff mixed types', () => {
expect(deepDiffer({}, [])).toBe(true);
expect(deepDiffer([], {})).toBe(true);
expect(
deepDiffer(
{a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]},
{a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]},
),
).toBe(false);
expect(
deepDiffer(
{a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]},
{a: [{A: {aA: 1, bB: 2}}, 'bar'], c: [1, [false]]},
),
).toBe(true);
expect(
deepDiffer(
{a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]},
{a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false], null]},
),
).toBe(true);
expect(
deepDiffer(
{a: [{A: {aA: 1, bB: 1}}, 'bar'], c: [1, [false]]},
{a: [{A: {aA: 1, bB: 1}}, ['bar']], c: [1, [false]]},
),
).toBe(true);
});
it('should distinguish between proper Array and Object', () => {
expect(deepDiffer(['a', 'b'], {0: 'a', 1: 'b', length: 2})).toBe(true);
expect(deepDiffer(['a', 'b'], {length: 2, 0: 'a', 1: 'b'})).toBe(true);
});
it('should diff same object', () => {
const obj = [1, [2, 3]];
expect(deepDiffer(obj, obj)).toBe(false);
});
it('should respect maxDepth arg', () => {
expect(
deepDiffer(
{a: {A: {aA: 1, bB: 1}}, b: 1},
{a: {A: {aA: 1, bB: 1}}, b: 1},
3,
),
).toBe(true);
expect(
deepDiffer(
{a: {A: {aA: 1, bB: 1}}, b: 1},
{a: {A: {aA: 1, bB: 1}}, b: 1},
4,
),
).toBe(false);
});
});