2 Copyright 2014 The Chromium Authors. All rights reserved.
3 Use of this source code is governed by a BSD-style license that can be
4 found in the LICENSE file.
7 <link rel="import" href="../update-util.html">
14 var assert = chai.assert;
16 describe('update-util', function() {
17 describe('.updateLeft', function() {
18 it('updates an array', function() {
19 var x = {key: 'x', val: 0};
20 var y1 = {key: 'y', val: 1};
21 var y2 = {key: 'y', val: 2};
22 var z = {key: 'z', val: 3};
25 target = updateUtil.updateLeft(target, source);
26 assert.strictEqual(target[0], y1);
27 assert.strictEqual(target[1], z);
28 assert.equal(target[0].val, 2);
31 it('updates and reorders an array', function() {
32 var x = {key: 'x', val: 0};
33 var y1 = {key: 'y', val: 1};
34 var y2 = {key: 'y', val: 2};
35 var z = {key: 'z', val: 3};
38 target = updateUtil.updateLeft(target, source);
39 assert.strictEqual(target[0], z);
40 assert.strictEqual(target[1], y1);
41 assert.equal(target[1].val, 2);
44 it('updates an array to empty', function() {
45 var x = {key: 'x', val: 0};
48 assert.deepEqual(updateUtil.updateLeft(target, source), []);
54 assert.deepEqual(updateUtil.updateLeft(target, source), []);
58 it('calls custom updateLeft members', function() {
59 var y1 = {key: 'y', val: 1,
60 updateLeft: function(right) {
61 this.val += right.val;
64 var y2 = {key: 'y', val: 2};
67 target = updateUtil.updateLeft(target, source);
68 assert.strictEqual(target[0], y1);
69 assert.propertyVal(target[0], 'val', 3);
70 assert.property(target[0], 'updateLeft');
73 it('calls custom updateLeft members in custom types', function() {
74 function Updatable(val) {
78 Updatable.prototype.updateLeft = function(right) {
79 this.val += right.val;
83 var y1 = new Updatable(1);
84 var y2 = new Updatable(2);
87 target = updateUtil.updateLeft(target, source);
88 assert.strictEqual(target[0], y1);
89 assert.propertyVal(target[0], 'val', 3);
90 assert.property(target[0], 'updateLeft');
93 it('skips transient properties', function() {
94 function Updatable(val, visible) {
96 if (visible !== undefined)
97 this.visible = visible
99 Updatable.transientProperties = ['visible'];
101 var target = new Updatable(1, true);
102 var source = new Updatable(2, false);
103 assert.strictEqual(updateUtil.updateLeft(target, source), target);
104 assert.propertyVal(target, 'val', 2);
105 assert.propertyVal(target, 'visible', true);
107 var target = new Updatable(1, undefined);
108 var source = new Updatable(2, true);
109 updateUtil.updateLeft(target, source);
110 assert.notProperty(target, 'visible');
112 var target = new Updatable(1, true);
113 var source = new Updatable(2, undefined);
114 updateUtil.updateLeft(target, source);
115 assert.propertyVal(target, 'visible', true);
118 it('updates array containing null', function() {
119 var target = [{key: 'x', a: {b:1}}];
120 var source = [{key: 'x', a: null}];
121 target = updateUtil.updateLeft(target, source);
122 assert.propertyVal(target[0], 'a', null);
124 var target = [{key: 'x', a: null}];
125 var source = [{key: 'x', a: {b:1}}];
126 target = updateUtil.updateLeft(target, source);
127 assert.deepEqual(target[0].a, {b:1});
130 it('updates object containing null', function() {
131 var target = {x: {a: 1}};
132 var source = {x: null};
133 assert.strictEqual(updateUtil.updateLeft(target, source), target);
134 assert.propertyVal(target, 'x', null);
136 var target = {x: null};
137 var source = {x: {a: 1}};
138 assert.strictEqual(updateUtil.updateLeft(target, source), target);
139 assert.deepEqual(target.x, {a: 1});
142 it('returns source on badly typed arguments', function() {
143 assert.strictEqual(updateUtil.updateLeft(1, 2), 2);
145 assert.strictEqual(updateUtil.updateLeft([1], source), source);