| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
 | 'use strict';
describe('$route', function() {
  var scope;
  beforeEach(function(){
    scope = angular.scope();
  });
  afterEach(function(){
    dealoc(scope);
  });
  it('should route and fire change event', function(){
    var log = '',
        $location, $route;
    function BookChapter() {
      this.log = '<init>';
    }
    scope = compile('<div></div>')();
    $location = scope.$service('$location');
    $route = scope.$service('$route');
    $route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'});
    $route.when('/Blank');
    $route.onChange(function(){
      log += 'onChange();';
    });
    $location.update('http://server#/Book/Moby/Chapter/Intro?p=123');
    scope.$eval();
    expect(log).toEqual('onChange();');
    expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
    expect($route.current.scope.log).toEqual('<init>');
    var lastId = $route.current.scope.$id;
    log = '';
    $location.update('http://server#/Blank?ignore');
    scope.$eval();
    expect(log).toEqual('onChange();');
    expect($route.current.params).toEqual({ignore:true});
    expect($route.current.scope.$id).not.toEqual(lastId);
    log = '';
    $location.update('http://server#/NONE');
    scope.$eval();
    expect(log).toEqual('onChange();');
    expect($route.current).toEqual(null);
    $route.when('/NONE', {template:'instant update'});
    scope.$eval();
    expect($route.current.template).toEqual('instant update');
  });
  it('should return fn registered with onChange()', function() {
    var $route = scope.$service('$route'),
        fn = function() {};
    expect($route.onChange(fn)).toBe(fn);
  });
  it('should match a route that contains special chars in the path', function() {
    var $route = scope.$service('$route'),
        $location = scope.$service('$location');
    $route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'});
    $location.hashPath = '/test';
    scope.$eval();
    expect($route.current).toBe(null);
    $location.hashPath = '/$testX23/foo(bar)/222';
    scope.$eval();
    expect($route.current).toBe(null);
    $location.hashPath = '/$test.23/foo(bar)/222';
    scope.$eval();
    expect($route.current).toBeDefined();
    $location.hashPath = '/$test.23/foo\\(bar)/222';
    scope.$eval();
    expect($route.current).toBe(null);
  });
  it('should allow routes to be defined with just templates without controllers', function() {
    var scope = angular.scope(),
        $location = scope.$service('$location'),
        $route = scope.$service('$route'),
        onChangeSpy = jasmine.createSpy('onChange');
    $route.when('/foo', {template: 'foo.html'});
    $route.onChange(onChangeSpy);
    expect($route.current).toBeNull();
    expect(onChangeSpy).not.toHaveBeenCalled();
    $location.updateHash('/foo');
    scope.$eval();
    expect($route.current.template).toEqual('foo.html');
    expect($route.current.controller).toBeUndefined();
    expect(onChangeSpy).toHaveBeenCalled();
  });
  it('should handle unknown routes with "otherwise" route definition', function() {
    var scope = angular.scope(),
        $location = scope.$service('$location'),
        $route = scope.$service('$route'),
        onChangeSpy = jasmine.createSpy('onChange');
    function NotFoundCtrl() {this.notFoundProp = 'not found!';}
    $route.when('/foo', {template: 'foo.html'});
    $route.otherwise({template: '404.html', controller: NotFoundCtrl});
    $route.onChange(onChangeSpy);
    expect($route.current).toBeNull();
    expect(onChangeSpy).not.toHaveBeenCalled();
    $location.updateHash('/unknownRoute');
    scope.$eval();
    expect($route.current.template).toBe('404.html');
    expect($route.current.controller).toBe(NotFoundCtrl);
    expect($route.current.scope.notFoundProp).toBe('not found!');
    expect(onChangeSpy).toHaveBeenCalled();
    onChangeSpy.reset();
    $location.updateHash('/foo');
    scope.$eval();
    expect($route.current.template).toEqual('foo.html');
    expect($route.current.controller).toBeUndefined();
    expect($route.current.scope.notFoundProp).toBeUndefined();
    expect(onChangeSpy).toHaveBeenCalled();
  });
  describe('redirection', function() {
    it('should support redirection via redirectTo property by updating $location', function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $browser = scope.$service('$browser'),
          $route = scope.$service('$route'),
          onChangeSpy = jasmine.createSpy('onChange');
      $route.when('', {redirectTo: '/foo'});
      $route.when('/foo', {template: 'foo.html'});
      $route.when('/bar', {template: 'bar.html'});
      $route.when('/baz', {redirectTo: '/bar'});
      $route.otherwise({template: '404.html'});
      $route.onChange(onChangeSpy);
      expect($route.current).toBeNull();
      expect(onChangeSpy).not.toHaveBeenCalled();
      scope.$eval(); //triggers initial route change - match the redirect route
      $browser.defer.flush(); //triger route change - match the route we redirected to
      expect($location.hash).toBe('/foo');
      expect($route.current.template).toBe('foo.html');
      expect(onChangeSpy.callCount).toBe(1);
      onChangeSpy.reset();
      $location.updateHash('');
      scope.$eval(); //match the redirect route + update $browser
      $browser.defer.flush(); //match the route we redirected to
      expect($location.hash).toBe('/foo');
      expect($route.current.template).toBe('foo.html');
      expect(onChangeSpy.callCount).toBe(1);
      onChangeSpy.reset();
      $location.updateHash('/baz');
      scope.$eval(); //match the redirect route + update $browser
      $browser.defer.flush(); //match the route we redirected to
      expect($location.hash).toBe('/bar');
      expect($route.current.template).toBe('bar.html');
      expect(onChangeSpy.callCount).toBe(1);
    });
    it('should interpolate route variables in the redirected hashPath from the original hashPath',
        function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $browser = scope.$service('$browser'),
          $route = scope.$service('$route');
      $route.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'});
      $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
      scope.$eval();
      $location.updateHash('/foo/id1/foo/subid3/gah');
      scope.$eval(); //triggers initial route change - match the redirect route
      $browser.defer.flush(); //triger route change - match the route we redirected to
      expect($location.hash).toBe('/bar/id1/subid3/23?extraId=gah');
      expect($route.current.template).toBe('bar.html');
    });
    it('should interpolate route variables in the redirected hashPath from the original hashSearch',
        function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $browser = scope.$service('$browser'),
          $route = scope.$service('$route');
      $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
      $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'});
      scope.$eval();
      $location.hash = '/foo/id3/eId?subid=sid1&appended=true';
      scope.$eval(); //triggers initial route change - match the redirect route
      $browser.defer.flush(); //triger route change - match the route we redirected to
      expect($location.hash).toBe('/bar/id3/sid1/99?appended=true&extra=eId');
      expect($route.current.template).toBe('bar.html');
    });
    it('should allow custom redirectTo function to be used', function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $browser = scope.$service('$browser'),
          $route = scope.$service('$route');
      $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
      $route.when('/foo/:id',
                  {redirectTo: customRedirectFn});
      scope.$eval();
      $location.hash = '/foo/id3?subid=sid1&appended=true';
      scope.$eval(); //triggers initial route change - match the redirect route
      $browser.defer.flush(); //triger route change - match the route we redirected to
      expect($location.hash).toBe('custom');
      function customRedirectFn(routePathParams, hash, hashPath, hashSearch) {
        expect(routePathParams).toEqual({id: 'id3'});
        expect(hash).toEqual($location.hash);
        expect(hashPath).toEqual($location.hashPath);
        expect(hashSearch).toEqual($location.hashSearch);
        return 'custom';
      }
    });
  });
  describe('reloadOnSearch', function() {
    it('should reload a route when reloadOnSearch is enabled and hashSearch changes', function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $route = scope.$service('$route'),
          reloaded = jasmine.createSpy('route reload');
      $route.when('/foo', {controller: FooCtrl});
      $route.onChange(reloaded);
      function FooCtrl() {
        reloaded();
      }
      $location.updateHash('/foo');
      scope.$eval();
      expect(reloaded).toHaveBeenCalled();
      reloaded.reset();
      // trigger reload
      $location.hashSearch.foo = 'bar';
      scope.$eval();
      expect(reloaded).toHaveBeenCalled();
    });
    it('should not reload a route when reloadOnSearch is disabled and only hashSearch changes',
        function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $route = scope.$service('$route'),
          reloaded = jasmine.createSpy('route reload');
      $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false});
      $route.onChange(reloaded);
      function FooCtrl() {
        reloaded();
      }
      expect(reloaded).not.toHaveBeenCalled();
      $location.updateHash('/foo');
      scope.$eval();
      expect(reloaded).toHaveBeenCalled();
      reloaded.reset();
      // don't trigger reload
      $location.hashSearch.foo = 'bar';
      scope.$eval();
      expect(reloaded).not.toHaveBeenCalled();
    });
    it('should reload reloadOnSearch route when url differs only in route path param', function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $route = scope.$service('$route'),
          reloaded = jasmine.createSpy('routeReload'),
          onRouteChange = jasmine.createSpy('onRouteChange');
      $route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false});
      $route.onChange(onRouteChange);
      function FooCtrl() {
        reloaded();
      }
      expect(reloaded).not.toHaveBeenCalled();
      expect(onRouteChange).not.toHaveBeenCalled();
      $location.updateHash('/foo/aaa');
      scope.$eval();
      expect(reloaded).toHaveBeenCalled();
      expect(onRouteChange).toHaveBeenCalled();
      reloaded.reset();
      onRouteChange.reset();
      $location.updateHash('/foo/bbb');
      scope.$eval();
      expect(reloaded).toHaveBeenCalled();
      expect(onRouteChange).toHaveBeenCalled();
      reloaded.reset();
      onRouteChange.reset();
      $location.hashSearch.foo = 'bar';
      scope.$eval();
      expect(reloaded).not.toHaveBeenCalled();
      expect(onRouteChange).not.toHaveBeenCalled();
    });
    it('should update route params when reloadOnSearch is disabled and hashSearch', function() {
      var scope = angular.scope(),
          $location = scope.$service('$location'),
          $route = scope.$service('$route'),
          routeParams = jasmine.createSpy('routeParams');
      $route.when('/foo', {controller: FooCtrl});
      $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false});
      function FooCtrl() {
        this.$watch(function() {
          return $route.current.params;
        }, function(params) {
          routeParams(params);
        });
      }
      expect(routeParams).not.toHaveBeenCalled();
      $location.updateHash('/foo');
      scope.$eval();
      expect(routeParams).toHaveBeenCalledWith({});
      routeParams.reset();
      // trigger reload
      $location.hashSearch.foo = 'bar';
      scope.$eval();
      expect(routeParams).toHaveBeenCalledWith({foo: 'bar'});
      routeParams.reset();
      $location.updateHash('/bar/123');
      scope.$eval();
      expect(routeParams).toHaveBeenCalledWith({barId: '123'});
      routeParams.reset();
      // don't trigger reload
      $location.hashSearch.foo = 'bar';
      scope.$eval();
      $route.current.scope.$eval(); // ng:view propagates evals so we have to do it by hand here
      expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'});
    });
  });
});
 |