OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 suite('bookmarks.StoreClient', function() { | |
6 var store; | |
7 var client; | |
8 | |
9 function update(newState) { | |
10 store.notifyObservers_(newState); | |
11 Polymer.dom.flush(); | |
12 } | |
13 | |
14 function getRenderedItems() { | |
15 return Array.from(client.root.querySelectorAll('.item')) | |
16 .map((div) => div.textContent.trim()); | |
17 } | |
18 | |
19 suiteSetup(function() { | |
20 document.body.innerHTML = ` | |
21 <dom-module is="test-store-client"> | |
22 <template> | |
23 <template is="dom-repeat" items="[[items]]"> | |
24 <div class="item">[[item]]</div> | |
25 </template> | |
26 </template> | |
27 </dom-module> | |
28 `; | |
29 | |
30 Polymer({ | |
31 is: 'test-store-client', | |
32 | |
33 behaviors: [bookmarks.StoreClient], | |
34 | |
35 properties: { | |
36 items: { | |
37 type: Array, | |
38 observer: 'itemsChanged_', | |
39 }, | |
40 }, | |
41 | |
42 attached: function() { | |
43 this.hasChanged = false; | |
44 this.watch('items', function(state) { | |
45 return state.items; | |
46 }); | |
47 this.updateFromStore(); | |
48 }, | |
49 | |
50 itemsChanged_: function(newItems, oldItems) { | |
51 if (oldItems) | |
52 this.hasChanged = true; | |
53 }, | |
54 }); | |
55 }); | |
56 | |
57 setup(function() { | |
58 PolymerTest.clearBody(); | |
59 | |
60 // Reset store instance: | |
61 bookmarks.Store.instance_ = new bookmarks.Store(); | |
62 store = bookmarks.Store.getInstance(); | |
63 store.init({ | |
64 items: ['apple', 'banana', 'cantaloupe'], | |
65 count: 3, | |
66 }); | |
67 | |
68 client = document.createElement('test-store-client'); | |
69 document.body.appendChild(client); | |
70 Polymer.dom.flush(); | |
71 }); | |
72 | |
73 test('renders initial data', function() { | |
74 assertDeepEquals(['apple', 'banana', 'cantaloupe'], getRenderedItems()); | |
75 }); | |
76 | |
77 test('renders changes to watched state', function() { | |
78 var newItems = ['apple', 'banana', 'courgette', 'durian']; | |
79 var newState = Object.assign({}, store.data, { | |
80 items: newItems, | |
81 }); | |
82 update(newState); | |
83 | |
84 assertTrue(client.hasChanged); | |
85 assertDeepEquals(newItems, getRenderedItems()); | |
86 }); | |
87 | |
88 test('ignores changes to other subtrees', function() { | |
89 var newState = Object.assign({}, store.data, {count: 2}); | |
90 update(newState); | |
91 | |
92 assertFalse(client.hasChanged); | |
93 }); | |
94 }); | |
OLD | NEW |