git.fiddlerwoaroof.com
Browse code

Test action-router for action/url loop, fix it

Ed Langley authored on 20/06/2017 20:17:02
Showing 3 changed files
... ...
@@ -13,7 +13,6 @@
13 13
 
14 14
 (define head-tag-num (make-parameter 0))
15 15
 (defun current-head-tag ()
16
-  (println (format "at head tag: ~a" (head-tag-num)))
17 16
   (string->symbol (format "h~d" (head-tag-num))))
18 17
 
19 18
 (defun headline (element)
... ...
@@ -41,7 +40,6 @@
41 40
   (syntax-rules ()
42 41
     [(section #:headline %headline . body)
43 42
      (parameterize ([head-tag-num (1+ (head-tag-num))])
44
-       (println (format "At head-tag-num ~d" (head-tag-num)))
45 43
        (section `,(headline %headline) . body))]
46 44
     [(section . body)
47 45
      (txexpr 'section empty
... ...
@@ -320,6 +320,8 @@ function createActionDispatcher(routesConfig, window) {
320 320
       let matcher = matchAction(action, compiledActionMatchers);
321 321
       if(matcher) {
322 322
         let path = constructPath(matcher);
323
+        // TODO: Solves double action, need to figure out test
324
+        this.currentLocation = path;
323 325
         window.history.pushState({}, '', path);
324 326
       }
325 327
     },
... ...
@@ -21,7 +21,7 @@ function createFakeWindow(path='/path/to/thing') {
21 21
   const window = {
22 22
     location: createLocation(path),
23 23
     history: {
24
-      pushState: jest.fn(),
24
+      pushState: jest.fn((_, __, path) => {window.location = createLocation(path)}),
25 25
       replaceState: jest.fn()
26 26
     }
27 27
   };
... ...
@@ -74,7 +74,6 @@ function setupTest(routesConfig, path='/path/to/thing') {
74 74
 }
75 75
 
76 76
 it("router handles exact match in preference to wildcard match", () => {
77
-
78 77
   //given
79 78
   const actionType = 'THE_ACTION';
80 79
   const action = {type: actionType, id: 1};
... ...
@@ -92,6 +91,25 @@ it("router handles exact match in preference to wildcard match", () => {
92 91
 
93 92
 });
94 93
 
94
+it("router doees not dispatch an action from url change that is caused by action dispatch", () => {
95
+  //given
96
+  const actionType = 'THE_ACTION';
97
+  const id = "1";
98
+  const view = "home";
99
+  const action = {type: actionType, id, view};
100
+  const routesConfig = [
101
+    ["/somewhere/:id/:view", actionType, {}],
102
+    ["/somewhere/:id/default", actionType, {view: "home"}],
103
+  ];
104
+  const {urlChanges, store, actionsDispatched, init} = setupTest(routesConfig);
105
+
106
+  // when
107
+  store.dispatch(action);
108
+
109
+  // then
110
+  expect(actionsDispatched()).toEqual([action]);
111
+});
112
+
95 113
 it("router handles wildcard with extra args correctly", () => {
96 114
 
97 115
   //given