Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Side by Side Diff: Tools/GardeningServer/lib/test/util-tests.html

Issue 526633002: Apply object updates from the network without blowing away object identity or UI attributes. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Initial Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!--
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.
5 -->
6
7 <link rel="import" href="../util.html">
8
9 <script>
10
11 (function () {
12 'use strict';
13
14 var assert = chai.assert;
15
16 describe('util', function() {
17 describe('.updateLeft', function() {
18 it('updates an array', function() {
19 var x = {fingerprint: 'x', val: 0};
20 var y1 = {fingerprint: 'y', val: 1};
21 var y2 = {fingerprint: 'y', val: 2};
22 var z = {fingerprint: 'z', val: 3};
23 var target = [x, y1];
24 var source = [y2, z];
25 util.updateLeft(target, source);
26 assert.strictEqual(target[0], y1);
27 assert.strictEqual(target[1], z);
28 assert.equal(target[0].val, 2);
29 });
30
31 it('updates and reorders an array', function() {
32 var x = {fingerprint: 'x', val: 0};
33 var y1 = {fingerprint: 'y', val: 1};
34 var y2 = {fingerprint: 'y', val: 2};
35 var z = {fingerprint: 'z', val: 3};
36 var target = [x, y1];
37 var source = [z, y2];
38 util.updateLeft(target, source);
39 assert.strictEqual(target[0], z);
40 assert.strictEqual(target[1], y1);
41 assert.equal(target[1].val, 2);
42 });
43
44 it('calls custom updateLeft members', function() {
45 var y1 = {fingerprint: 'y', val: 1,
46 updateLeft: function(right) {
47 this.val += right.val;
48 }};
49 var y2 = {fingerprint: 'y', val: 2};
50 var target = [y1];
51 var source = [y2];
52 util.updateLeft(target, source);
53 assert.strictEqual(target[0], y1);
54 assert.propertyVal(target[0], 'val', 3);
55 assert.property(target[0], 'updateLeft');
56 });
57
58 it('calls custom updateLeft members in custom types', function() {
59 function Updatable(val) {
60 this.fingerprint = 'x';
61 this.val = val;
62 };
63 Updatable.prototype.updateLeft = function(right) {
64 this.val += right.val;
65 };
66
67 var y1 = new Updatable(1);
68 var y2 = new Updatable(2);
69 var target = [y1];
70 var source = [y2];
71 util.updateLeft(target, source);
72 assert.strictEqual(target[0], y1);
73 assert.propertyVal(target[0], 'val', 3);
74 assert.property(target[0], 'updateLeft');
75 });
76
77 it('skips UI properties', function() {
78 function Updatable(val, visible) {
79 this.val = val;
80 if (visible !== undefined)
81 this.visible = visible
82 };
83 Updatable.uiStateProperties = ['visible'];
84
85 var target = new Updatable(1, true);
86 var source = new Updatable(2, false);
87 util.updateLeft(target, source);
88 assert.propertyVal(target, 'val', 2);
89 assert.propertyVal(target, 'visible', true);
90
91 var target = new Updatable(1, undefined);
92 var source = new Updatable(2, true);
93 util.updateLeft(target, source);
94 assert.notProperty(target, 'visible');
95
96 var target = new Updatable(1, true);
97 var source = new Updatable(2, undefined);
98 util.updateLeft(target, source);
99 assert.propertyVal(target, 'visible', true);
100 });
101
102 it('updates array containing null', function() {
103 var target = [{fingerprint: 'x', a: {b:1}}];
104 var source = [{fingerprint: 'x', a: null}];
105 util.updateLeft(target, source);
106 assert.propertyVal(target[0], 'a', null);
107
108 var target = [{fingerprint: 'x', a: null}];
109 var source = [{fingerprint: 'x', a: {b:1}}];
110 util.updateLeft(target, source);
111 assert.deepEqual(target[0].a, {b:1});
112 });
113
114 it('updates object containing null', function() {
115 var target = {x: {a: 1}};
116 var source = {x: null};
117 util.updateLeft(target, source);
118 assert.propertyVal(target, 'x', null);
119
120 var target = {x: null};
121 var source = {x: {a: 1}};
122 util.updateLeft(target, source);
123 assert.deepEqual(target.x, {a: 1});
124 });
125
126 it('throws on badly typed arguments', function() {
127 assert.throws(function() {util.updateLeft(1, 2)}, TypeError, '1, 2');
128 assert.throws(function() {util.updateLeft([1], [2])}, TypeError, '[1], [2] ');
129 });
130 });
131 });
132
133 })();
134
135 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698