test_middleware-task-03.js (1273B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 createStore, 9 applyMiddleware, 10 } = require("resource://devtools/client/shared/vendor/redux.js"); 11 const { 12 task, 13 ERROR_TYPE, 14 } = require("resource://devtools/client/shared/redux/middleware/task.js"); 15 16 /** 17 * Tests that the middleware handles errors thrown in tasks, and rejected promises. 18 */ 19 20 add_task(async function () { 21 const store = applyMiddleware(task)(createStore)(reducer); 22 23 store.dispatch(asyncError()); 24 await waitUntilState(store, () => store.getState().length === 1); 25 equal( 26 store.getState()[0].type, 27 ERROR_TYPE, 28 "generator errors dispatch ERROR_TYPE actions" 29 ); 30 equal( 31 store.getState()[0].error, 32 "task-middleware-error-generator", 33 "generator errors dispatch ERROR_TYPE actions with error" 34 ); 35 }); 36 37 function asyncError() { 38 return async () => { 39 const error = "task-middleware-error-generator"; 40 throw error; 41 }; 42 } 43 44 function reducer(state = [], action) { 45 info("Action called: " + action.type); 46 if (action.type === ERROR_TYPE) { 47 state.push(action); 48 } 49 return [...state]; 50 }