| 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 suiteSetup(function() { | 5 suiteSetup(function() { |
| 6 cr.define('bookmarks', function() { | 6 cr.define('bookmarks', function() { |
| 7 var TestStore = function(data) { | 7 var TestStore = function(data) { |
| 8 bookmarks.Store.call(this); | 8 bookmarks.Store.call(this); |
| 9 this.data_ = Object.assign(bookmarks.util.createEmptyState(), data); | 9 this.data_ = Object.assign(bookmarks.util.createEmptyState(), data); |
| 10 this.initialized_ = true; | 10 this.initialized_ = true; |
| 11 | 11 |
| 12 this.lastAction_ = null; | 12 this.lastAction_ = null; |
| 13 this.acceptInit_ = false; | 13 this.acceptInit_ = false; |
| 14 this.enableReducers_ = false; | 14 this.enableReducers_ = false; |
| 15 /** @type {!Map<string, !PromiseResolver>} */ |
| 16 this.resolverMap_ = new Map(); |
| 15 }; | 17 }; |
| 16 | 18 |
| 17 TestStore.prototype = { | 19 TestStore.prototype = { |
| 18 __proto__: bookmarks.Store.prototype, | 20 __proto__: bookmarks.Store.prototype, |
| 19 | 21 |
| 20 init: function(state) { | 22 init: function(state) { |
| 21 if (this.acceptInit_) | 23 if (this.acceptInit_) |
| 22 bookmarks.Store.prototype.init.call(this, state); | 24 bookmarks.Store.prototype.init.call(this, state); |
| 23 }, | 25 }, |
| 24 | 26 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 47 * @param {boolean} enabled | 49 * @param {boolean} enabled |
| 48 */ | 50 */ |
| 49 setReducersEnabled: function(enabled) { | 51 setReducersEnabled: function(enabled) { |
| 50 this.enableReducers_ = enabled; | 52 this.enableReducers_ = enabled; |
| 51 }, | 53 }, |
| 52 | 54 |
| 53 reduce_: function(action) { | 55 reduce_: function(action) { |
| 54 this.lastAction_ = action; | 56 this.lastAction_ = action; |
| 55 if (this.enableReducers_) | 57 if (this.enableReducers_) |
| 56 bookmarks.Store.prototype.reduce_.call(this, action); | 58 bookmarks.Store.prototype.reduce_.call(this, action); |
| 59 if (this.resolverMap_.has(action.name)) |
| 60 this.resolverMap_.get(action.name).resolve(action); |
| 57 }, | 61 }, |
| 58 | 62 |
| 59 /** | 63 /** |
| 60 * Notifies UI elements that the store data has changed. When reducers are | 64 * Notifies UI elements that the store data has changed. When reducers are |
| 61 * disabled, tests are responsible for manually changing the data to make | 65 * disabled, tests are responsible for manually changing the data to make |
| 62 * UI elements update correctly (eg, tests must replace the whole list | 66 * UI elements update correctly (eg, tests must replace the whole list |
| 63 * when changing a single element). | 67 * when changing a single element). |
| 64 */ | 68 */ |
| 65 notifyObservers: function() { | 69 notifyObservers: function() { |
| 66 this.notifyObservers_(this.data); | 70 this.notifyObservers_(this.data); |
| 67 }, | 71 }, |
| 68 | 72 |
| 69 // Call in order to accept data from an init call to the TestStore once. | 73 // Call in order to accept data from an init call to the TestStore once. |
| 70 acceptInitOnce: function() { | 74 acceptInitOnce: function() { |
| 71 this.acceptInit_ = true; | 75 this.acceptInit_ = true; |
| 72 this.initialized_ = false; | 76 this.initialized_ = false; |
| 73 }, | 77 }, |
| 78 |
| 79 /** |
| 80 * Track actions called |name|, allowing that type of action to be waited |
| 81 * for with `waitForAction`. |
| 82 * @param {string} name |
| 83 */ |
| 84 expectAction: function(name) { |
| 85 this.resolverMap_.set(name, new PromiseResolver()); |
| 86 }, |
| 87 |
| 88 /** |
| 89 * Returns a Promise that will resolve when an action called |name| is |
| 90 * dispatched. The promise must be prepared by calling |
| 91 * `expectAction(name)` before the action is dispatched. |
| 92 * @param {string} name |
| 93 * @return {!Promise<!Action>} |
| 94 */ |
| 95 waitForAction: function(name) { |
| 96 assertTrue( |
| 97 this.resolverMap_.has(name), |
| 98 'Must call expectAction before each call to waitForAction'); |
| 99 return this.resolverMap_.get(name).promise.then((action) => { |
| 100 this.resolverMap_.delete(name); |
| 101 return action; |
| 102 }); |
| 103 }, |
| 74 }; | 104 }; |
| 75 | 105 |
| 76 return { | 106 return { |
| 77 TestStore: TestStore, | 107 TestStore: TestStore, |
| 78 }; | 108 }; |
| 79 }); | 109 }); |
| 80 }); | 110 }); |
| OLD | NEW |