Browse Source

transpiling es2015 with babel

Herbert Vojčík 8 years ago
parent
commit
fdcad51dc2
2 changed files with 66 additions and 2 deletions
  1. 59 0
      lib/node.js
  2. 7 2
      package.json

+ 59 - 0
lib/node.js

@@ -0,0 +1,59 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+  value: true
+});
+/*
+ * Helpers for testing Redux.
+ */
+
+var FINISHED = "@@test/SPYSHOULDBEFINISHED";
+
+/**
+ * Creates the middleware that checks if specified actions are dispatched.
+ * Use it like `const spy = spyInTheMiddle(fn1, fn2, ...), store = applyMiddleware(..., spy)(reducer);`.
+ * The fn1, ... get action in the parameter and should return truthy value if it is the expected one,
+ * in which case the expectation will be removed; and shoudl return falsy value if it can be ignored.
+ * For the moment, only first expectation in the list is checked until it returns true, then it is removed and the next one will be used,
+ * In case the action is invalid (like, it has right type but wrong payload), the expectation fn can (and should) throw,
+ * for example using the test assertion (in chai/expect, assertions are truthy, so your expectation fn can look like below)
+ *
+ *     ({type, payload: {foo}}) => type == "FOOIFY" && expect(foo).to.equal("bar")
+ *
+ * All actions are passed downstream, unless you threw.
+ *
+ * @param expectations
+ */
+var spyInTheMiddle = exports.spyInTheMiddle = function spyInTheMiddle() {
+  for (var _len = arguments.length, expectations = Array(_len), _key = 0; _key < _len; _key++) {
+    expectations[_key] = arguments[_key];
+  }
+
+  return function (store) {
+    return function (next) {
+      return function (action) {
+        if (action.type == FINISHED && expectations.length > 0) throw new Error("Expectations left unfulfilled: " + expectations.length);
+        if (expectations[0] && expectations[0](action)) {
+          expectations.shift();
+        }
+        return next(action);
+      };
+    };
+  };
+};
+
+/**
+ * Call `store.dispatch(SPY_VERIFY)` at the end of the test.
+ * It will pass if all expectations were fulfilled and fail if there are still some hanging there.
+ * @type {{type: string}}
+ */
+var SPY_VERIFY = exports.SPY_VERIFY = { type: FINISHED };
+
+/**
+ * Reducer that always returns unchanged state.
+ * @param state
+ * @param action
+ */
+var dumbReducer = exports.dumbReducer = function dumbReducer(state, action) {
+  return state;
+};

+ 7 - 2
package.json

@@ -2,8 +2,9 @@
   "name": "test-redux-goodies",
   "version": "0.0.1",
   "description": "Helpers to test Redux code.",
-  "main": "src/index.js",
+  "main": "lib/index.js",
   "scripts": {
+    "build": "babel src --presets es2015 --out-dir lib",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "repository": {
@@ -19,5 +20,9 @@
   "bugs": {
     "url": "https://github.com/herby/test-redux-goodies/issues"
   },
-  "homepage": "https://github.com/herby/test-redux-goodies#readme"
+  "homepage": "https://github.com/herby/test-redux-goodies#readme",
+  "devDependencies": {
+    "babel-cli": "^6.3.17",
+    "babel-preset-es2015": "^6.3.13"
+  }
 }