tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_middleware-task-02.js (2218B)


      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 /**
      8 * Tests that task middleware allows dispatching generators that dispatch
      9 * additional sync and async actions.
     10 */
     11 
     12 const {
     13  createStore,
     14  applyMiddleware,
     15 } = require("resource://devtools/client/shared/vendor/redux.js");
     16 const {
     17  task,
     18 } = require("resource://devtools/client/shared/redux/middleware/task.js");
     19 
     20 add_task(async function () {
     21  const store = applyMiddleware(task)(createStore)(reducer);
     22 
     23  store.dispatch(comboAction());
     24  await waitUntilState(store, () => store.getState().length === 4);
     25 
     26  equal(
     27    store.getState()[0].type,
     28    "fetchAsync-start",
     29    "Async dispatched actions in a generator task are fired"
     30  );
     31  equal(
     32    store.getState()[1].type,
     33    "fetchAsync-end",
     34    "Async dispatched actions in a generator task are fired"
     35  );
     36  equal(
     37    store.getState()[2].type,
     38    "fetchSync",
     39    "Return values of yielded sync dispatched actions are correct"
     40  );
     41  equal(
     42    store.getState()[3].type,
     43    "fetch-done",
     44    "Return values of yielded async dispatched actions are correct"
     45  );
     46  equal(
     47    store.getState()[3].data.sync.data,
     48    "sync",
     49    "Return values of dispatched sync values are correct"
     50  );
     51  equal(
     52    store.getState()[3].data.async,
     53    "async",
     54    "Return values of dispatched async values are correct"
     55  );
     56 });
     57 
     58 function comboAction() {
     59  return async function ({ dispatch }) {
     60    const data = {};
     61    data.async = await dispatch(fetchAsync("async"));
     62    data.sync = await dispatch(fetchSync("sync"));
     63    dispatch({ type: "fetch-done", data });
     64  };
     65 }
     66 
     67 function fetchSync(data) {
     68  return { type: "fetchSync", data };
     69 }
     70 
     71 function fetchAsync(data) {
     72  return async function ({ dispatch }) {
     73    dispatch({ type: "fetchAsync-start" });
     74    const val = await new Promise(resolve => resolve(data));
     75    dispatch({ type: "fetchAsync-end" });
     76    return val;
     77  };
     78 }
     79 
     80 function reducer(state = [], action) {
     81  info("Action called: " + action.type);
     82  if (/fetch/.test(action.type)) {
     83    state.push(action);
     84  }
     85  return [...state];
     86 }