OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('bookmarks', function() { | 5 suiteSetup(function() { |
6 var TestStore = function(data) { | 6 cr.define('bookmarks', function() { |
7 this.data = Object.assign(bookmarks.util.createEmptyState(), data); | 7 var TestStore = function(data) { |
8 this.lastAction_ = null; | 8 bookmarks.Store.call(this); |
9 this.observers_ = []; | 9 this.data_ = Object.assign(bookmarks.util.createEmptyState(), data); |
10 this.acceptInit_ = false; | 10 this.initialized_ = true; |
11 }; | |
12 | 11 |
13 TestStore.prototype = { | 12 this.lastAction_ = null; |
14 addObserver: function(client) { | 13 this.acceptInit_ = false; |
15 this.observers_.push(client); | 14 this.enableReducers_ = false; |
16 }, | 15 }; |
17 | 16 |
18 init: function(state) { | 17 TestStore.prototype = { |
19 if (this.acceptInit_) { | 18 __proto__: bookmarks.Store.prototype, |
20 this.data = state; | |
21 this.acceptInit_ = false; | |
22 } | |
23 }, | |
24 | 19 |
25 removeObserver: function(client) {}, | 20 init: function(state) { |
| 21 if (this.acceptInit_) |
| 22 bookmarks.Store.prototype.init.call(this, state); |
| 23 }, |
26 | 24 |
27 isInitialized: function() { | 25 get lastAction() { |
28 return true; | 26 return this.lastAction_; |
29 }, | 27 }, |
30 | 28 |
31 handleAction: function(action) { | 29 get data() { |
32 this.lastAction_ = action; | 30 return this.data_; |
33 }, | 31 }, |
34 | 32 |
35 get lastAction() { | 33 set data(newData) { |
36 return this.lastAction_; | 34 this.data_ = newData; |
37 }, | 35 }, |
38 | 36 |
39 notifyObservers: function() { | 37 /** |
40 // TODO(tsergeant): Revisit how state modifications work in UI tests. | 38 * Enable or disable calling bookmarks.reduceAction for each action. |
41 // We don't want tests to worry about modifying the whole state tree. | 39 * With reducers disabled (the default), TestStore is a stub which |
42 // Instead, we could perform a deep clone in here to ensure that every | 40 * requires state be managed manually (suitable for unit tests). With |
43 // StoreClient is updated. | 41 * reducers enabled, TestStore becomes a proxy for observing actions |
44 this.observers_.forEach((client) => client.onStateChanged(this.data)); | 42 * (suitable for integration tests). |
45 }, | 43 * @param {boolean} enabled |
| 44 */ |
| 45 setReducersEnabled: function(enabled) { |
| 46 this.enableReducers_ = enabled; |
| 47 }, |
46 | 48 |
47 // Call in order to accept data from an init call to the TestStore once. | 49 reduce_: function(action) { |
48 acceptInitOnce: function() { | 50 this.lastAction_ = action; |
49 this.acceptInit_ = true; | 51 if (this.enableReducers_) |
50 }, | 52 bookmarks.Store.prototype.reduce_.call(this, action); |
51 }; | 53 }, |
52 | 54 |
53 return { | 55 notifyObservers: function() { |
54 TestStore: TestStore, | 56 // TODO(tsergeant): Revisit how state modifications work in UI tests. |
55 }; | 57 // We don't want tests to worry about modifying the whole state tree. |
| 58 // Instead, we could perform a deep clone in here to ensure that every |
| 59 // StoreClient is updated. |
| 60 this.notifyObservers_(this.data); |
| 61 }, |
| 62 |
| 63 // Call in order to accept data from an init call to the TestStore once. |
| 64 acceptInitOnce: function() { |
| 65 this.acceptInit_ = true; |
| 66 this.initialized_ = false; |
| 67 }, |
| 68 }; |
| 69 |
| 70 return { |
| 71 TestStore: TestStore, |
| 72 }; |
| 73 }); |
56 }); | 74 }); |
OLD | NEW |