git.fiddlerwoaroof.com
Browse code

Handle initial url loading - should fire action

Edward Langley authored on 14/06/2017 16:44:49
Showing 2 changed files
... ...
@@ -262,6 +262,7 @@ function createActionDispatcher(routesConfig, window) {
262 262
       return (reducer, finalInitialState, enhancer) => {
263 263
         let theStore = nextStoreCreator(reducer, finalInitialState, enhancer);
264 264
         this.activateDispatcher(theStore);
265
+        this.receiveLocation(window.location);
265 266
         return theStore;
266 267
       }
267 268
     },
... ...
@@ -272,7 +273,9 @@ function createActionDispatcher(routesConfig, window) {
272 273
       }
273 274
 
274 275
       const location = ev.detail;
275
-
276
+      this.receiveLocation(location);
277
+    },
278
+    receiveLocation(location) {
276 279
       const match = matchRoute(location, compiledRouteMatchers);
277 280
       if(match) {
278 281
         const action = constructAction(match);
... ...
@@ -15,18 +15,9 @@ function createLocation(path) {
15 15
   };
16 16
 }
17 17
 
18
-function createFakeWindow() {
18
+function createFakeWindow(path='/path/to/thing') {
19 19
   const window = {
20
-    location: {
21
-      hash: '#hash',
22
-      host: 'example.com',
23
-      hostname: 'example',
24
-      origin: '',
25
-      href: '',
26
-      pathname: '/path/to/thing',
27
-      port: 80,
28
-      protocol: 'https:'
29
-    },
20
+    location: createLocation(path),
30 21
     history: {
31 22
       pushState: jest.fn(),
32 23
       replaceState: jest.fn()
... ...
@@ -49,8 +40,8 @@ function createFakeWindow() {
49 40
   return window;
50 41
 }
51 42
 
52
-function setupTest(routesConfig) {
53
-  const window = createFakeWindow();
43
+function setupTest(routesConfig, path='/path/to/thing') {
44
+  const window = createFakeWindow(path);
54 45
   const mockPushState = window.history.pushState;
55 46
 
56 47
   const {middleware, enhancer} = installBrowserRouter(routesConfig, window);
... ...
@@ -228,3 +219,19 @@ it("router should give precedence to exact match first in equally-specific route
228 219
   expect(actionsDispatched()).toEqual([{type: 'ACTION_NAME', dynamic: 'something'}]);
229 220
 
230 221
 });
222
+
223
+it("router handles the current location when initialized", () => {
224
+  // given
225
+  const routesConfig = [
226
+    ["/something/:dynamic", "ACTION_NAME", {}],
227
+    ["/:dyn/something", "ACTION_NAME", {}],
228
+  ];
229
+
230
+  // when
231
+  /// We break the pattern because we're testing store construction.
232
+  const {actionsDispatched} = setupTest(routesConfig, '/something/something');
233
+
234
+  // then
235
+  expect(actionsDispatched()).toEqual([{type: 'ACTION_NAME', dynamic: 'something'}]);
236
+
237
+});