git.fiddlerwoaroof.com
Browse code

Export init from setupTest and actually test it.

Ed Langley authored on 18/06/2017 03:17:16
Showing 2 changed files
... ...
@@ -48,29 +48,29 @@ function setupTest(routesConfig, path='/path/to/thing') {
48 48
   addMissingHistoryEvents(window, window.history);
49 49
   addChangeUrlEvent(window, window.history);
50 50
 
51
-  const {middleware, enhancer} = installBrowserRouter(routesConfig, window);
52
-    const reduce = jest.fn();
53
-
54
-    const store = createStore(
55
-      reduce,
56
-      compose(
57
-        enhancer,
58
-        applyMiddleware(
59
-          middleware)));
60
-
61
-    function urlChanges() {
62
-      return mockPushState.mock.calls.map(item => item[2]);
63
-    }
51
+  const {middleware, enhancer, init} = installBrowserRouter(routesConfig, window);
52
+  const reduce = jest.fn();
64 53
 
65
-    function actionsDispatched() {
66
-      return reduce.mock.calls.map(item => item[1]).slice(1);
67
-    }
54
+  const store = createStore(
55
+    reduce,
56
+    compose(
57
+      enhancer,
58
+      applyMiddleware(
59
+        middleware)));
68 60
 
69
-    function fireUrlChange(path) {
70
-      window.dispatchEvent(new CustomEvent('urlchanged', {detail: createLocation(path)}));
71
-    }
61
+  function urlChanges() {
62
+    return mockPushState.mock.calls.map(item => item[2]);
63
+  }
64
+
65
+  function actionsDispatched() {
66
+    return reduce.mock.calls.map(item => item[1]).slice(1);
67
+  }
68
+
69
+  function fireUrlChange(path) {
70
+    window.dispatchEvent(new CustomEvent('urlchanged', {detail: createLocation(path)}));
71
+  }
72 72
 
73
-    return {store, reduce, window, urlChanges, actionsDispatched, fireUrlChange};
73
+  return {store, reduce, window, urlChanges, actionsDispatched, fireUrlChange, init};
74 74
 }
75 75
 
76 76
 it("router handles exact match in preference to wildcard match", () => {
... ...
@@ -280,7 +280,8 @@ it("router handles the current location when initialized", () => {
280 280
 
281 281
   // when
282 282
   /// We break the pattern because we're testing store construction.
283
-  const {actionsDispatched} = setupTest(routesConfig, '/something/something');
283
+  const {actionsDispatched, init} = setupTest(routesConfig, '/something/something');
284
+  init();
284 285
 
285 286
   // then
286 287
   expect(actionsDispatched()).toEqual([{type: 'ACTION_NAME', dynamic: 'something'}]);
... ...
@@ -68,12 +68,10 @@ it("given event handler should generate a urlchange event only when url changes"
68 68
   expect(calls.length).toEqual(5);
69 69
   expect(calls[4].type).toEqual('urlchanged');
70 70
   expect(calls[4].detail).toEqual(window.location);
71
-
72
-  console.log(map);
73 71
 });
74 72
 
75 73
 it("should only add url events 1x when addChangeUrlEvent is called on window more than 1x", () => {
76
-// given
74
+  // given
77 75
   const window = {};
78 76
   const map = {};
79 77