2017-12-01 17:23:03 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-11-22 00:14:06 -05:00
|
|
|
import {devModeEqual} from '@angular/core/src/change_detection/change_detection_util';
|
|
|
|
|
2018-01-17 11:55:55 -05:00
|
|
|
import {flatten, isDifferent} from '../../src/render3/util';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
describe('util', () => {
|
|
|
|
|
|
|
|
describe('isDifferent', () => {
|
|
|
|
|
2018-08-01 13:57:16 -04:00
|
|
|
describe('checkNoChangeMode = false', () => {
|
|
|
|
it('should mark non-equal arguments as different', () => {
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(isDifferent({}, {})).toBeTruthy();
|
|
|
|
expect(isDifferent('foo', 'bar')).toBeTruthy();
|
|
|
|
expect(isDifferent(0, 1)).toBeTruthy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not mark equal arguments as different', () => {
|
|
|
|
const obj = {};
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(isDifferent(obj, obj)).toBeFalsy();
|
|
|
|
expect(isDifferent('foo', 'foo')).toBeFalsy();
|
|
|
|
expect(isDifferent(1, 1)).toBeFalsy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-11-22 00:14:06 -05:00
|
|
|
it('should not mark NaN as different', () => { expect(isDifferent(NaN, NaN)).toBeFalsy(); });
|
2018-08-01 13:57:16 -04:00
|
|
|
|
|
|
|
it('should mark NaN with other values as different', () => {
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(isDifferent(NaN, 'foo')).toBeTruthy();
|
|
|
|
expect(isDifferent(5, NaN)).toBeTruthy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
2018-08-01 13:57:16 -04:00
|
|
|
describe('checkNoChangeMode = true', () => {
|
|
|
|
// Assert relaxed constraint in checkNoChangeMode
|
|
|
|
it('should not mark non-equal arrays, object and function as different', () => {
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(!devModeEqual([], [])).toBeFalsy();
|
|
|
|
expect(!devModeEqual(() => 0, () => 0)).toBeFalsy();
|
|
|
|
expect(!devModeEqual({}, {})).toBeFalsy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should mark non-equal arguments as different', () => {
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(!devModeEqual('foo', 'bar')).toBeTruthy();
|
|
|
|
expect(!devModeEqual(0, 1)).toBeTruthy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-08-01 13:57:16 -04:00
|
|
|
it('should not mark equal arguments as different', () => {
|
|
|
|
const obj = {};
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(isDifferent(obj, obj)).toBeFalsy();
|
|
|
|
expect(isDifferent('foo', 'foo')).toBeFalsy();
|
|
|
|
expect(isDifferent(1, 1)).toBeFalsy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
|
|
|
|
2018-11-22 00:14:06 -05:00
|
|
|
it('should not mark NaN as different', () => { expect(isDifferent(NaN, NaN)).toBeFalsy(); });
|
2018-08-01 13:57:16 -04:00
|
|
|
|
|
|
|
it('should mark NaN with other values as different', () => {
|
2018-11-22 00:14:06 -05:00
|
|
|
expect(isDifferent(NaN, 'foo')).toBeTruthy();
|
|
|
|
expect(isDifferent(5, NaN)).toBeTruthy();
|
2018-08-01 13:57:16 -04:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
2018-08-01 13:57:16 -04:00
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
2018-01-17 11:55:55 -05:00
|
|
|
|
|
|
|
describe('flatten', () => {
|
|
|
|
|
|
|
|
it('should flatten an empty array', () => { expect(flatten([])).toEqual([]); });
|
|
|
|
|
|
|
|
it('should flatten a flat array', () => { expect(flatten([1, 2, 3])).toEqual([1, 2, 3]); });
|
|
|
|
|
|
|
|
it('should flatten a nested array', () => {
|
|
|
|
expect(flatten([1, [2], 3])).toEqual([1, 2, 3]);
|
|
|
|
expect(flatten([[1], 2, [3]])).toEqual([1, 2, 3]);
|
|
|
|
expect(flatten([1, [2, [3]], 4])).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(flatten([1, [2, [3]], [4]])).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(flatten([1, [2, [3]], [[[4]]]])).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(flatten([1, [], 2])).toEqual([1, 2]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|