@@ -227,4 +227,140 @@ describe('$route', function() {
227227 }
228228 } ) ;
229229 } ) ;
230+
231+
232+ describe ( 'reloadOnSearch' , function ( ) {
233+ it ( 'should reload a route when reloadOnSearch is enabled and hashSearch changes' , function ( ) {
234+ var scope = angular . scope ( ) ,
235+ $location = scope . $service ( '$location' ) ,
236+ $route = scope . $service ( '$route' ) ,
237+ reloaded = jasmine . createSpy ( 'route reload' ) ;
238+
239+ $route . when ( '/foo' , { controller : FooCtrl } ) ;
240+ $route . onChange ( reloaded ) ;
241+
242+ function FooCtrl ( ) {
243+ reloaded ( ) ;
244+ }
245+
246+ $location . updateHash ( '/foo' ) ;
247+ scope . $eval ( ) ;
248+ expect ( reloaded ) . toHaveBeenCalled ( ) ;
249+ reloaded . reset ( ) ;
250+
251+ // trigger reload
252+ $location . hashSearch . foo = 'bar' ;
253+ scope . $eval ( ) ;
254+ expect ( reloaded ) . toHaveBeenCalled ( ) ;
255+ } ) ;
256+
257+
258+ it ( 'should not reload a route when reloadOnSearch is disabled and only hashSearch changes' ,
259+ function ( ) {
260+ var scope = angular . scope ( ) ,
261+ $location = scope . $service ( '$location' ) ,
262+ $route = scope . $service ( '$route' ) ,
263+ reloaded = jasmine . createSpy ( 'route reload' ) ;
264+
265+ $route . when ( '/foo' , { controller : FooCtrl , reloadOnSearch : false } ) ;
266+ $route . onChange ( reloaded ) ;
267+
268+ function FooCtrl ( ) {
269+ reloaded ( ) ;
270+ }
271+
272+ expect ( reloaded ) . not . toHaveBeenCalled ( ) ;
273+
274+ $location . updateHash ( '/foo' ) ;
275+ scope . $eval ( ) ;
276+ expect ( reloaded ) . toHaveBeenCalled ( ) ;
277+ reloaded . reset ( ) ;
278+
279+ // don't trigger reload
280+ $location . hashSearch . foo = 'bar' ;
281+ scope . $eval ( ) ;
282+ expect ( reloaded ) . not . toHaveBeenCalled ( ) ;
283+ } ) ;
284+
285+
286+ it ( 'should reload reloadOnSearch route when url differs only in route path param' , function ( ) {
287+ var scope = angular . scope ( ) ,
288+ $location = scope . $service ( '$location' ) ,
289+ $route = scope . $service ( '$route' ) ,
290+ reloaded = jasmine . createSpy ( 'routeReload' ) ,
291+ onRouteChange = jasmine . createSpy ( 'onRouteChange' ) ;
292+
293+ $route . when ( '/foo/:fooId' , { controller : FooCtrl , reloadOnSearch : false } ) ;
294+ $route . onChange ( onRouteChange ) ;
295+
296+ function FooCtrl ( ) {
297+ reloaded ( ) ;
298+ }
299+
300+ expect ( reloaded ) . not . toHaveBeenCalled ( ) ;
301+ expect ( onRouteChange ) . not . toHaveBeenCalled ( ) ;
302+
303+ $location . updateHash ( '/foo/aaa' ) ;
304+ scope . $eval ( ) ;
305+ expect ( reloaded ) . toHaveBeenCalled ( ) ;
306+ expect ( onRouteChange ) . toHaveBeenCalled ( ) ;
307+ reloaded . reset ( ) ;
308+ onRouteChange . reset ( ) ;
309+
310+ $location . updateHash ( '/foo/bbb' ) ;
311+ scope . $eval ( ) ;
312+ expect ( reloaded ) . toHaveBeenCalled ( ) ;
313+ expect ( onRouteChange ) . toHaveBeenCalled ( ) ;
314+ reloaded . reset ( ) ;
315+ onRouteChange . reset ( ) ;
316+
317+ $location . hashSearch . foo = 'bar' ;
318+ scope . $eval ( ) ;
319+ expect ( reloaded ) . not . toHaveBeenCalled ( ) ;
320+ expect ( onRouteChange ) . not . toHaveBeenCalled ( ) ;
321+ } ) ;
322+
323+
324+ it ( 'should update route params when reloadOnSearch is disabled and hashSearch' , function ( ) {
325+ var scope = angular . scope ( ) ,
326+ $location = scope . $service ( '$location' ) ,
327+ $route = scope . $service ( '$route' ) ,
328+ routeParams = jasmine . createSpy ( 'routeParams' ) ;
329+
330+ $route . when ( '/foo' , { controller : FooCtrl } ) ;
331+ $route . when ( '/bar/:barId' , { controller : FooCtrl , reloadOnSearch : false } ) ;
332+
333+ function FooCtrl ( ) {
334+ this . $watch ( function ( ) {
335+ return $route . current . params ;
336+ } , function ( params ) {
337+ routeParams ( params ) ;
338+ } ) ;
339+ }
340+
341+ expect ( routeParams ) . not . toHaveBeenCalled ( ) ;
342+
343+ $location . updateHash ( '/foo' ) ;
344+ scope . $eval ( ) ;
345+ expect ( routeParams ) . toHaveBeenCalledWith ( { } ) ;
346+ routeParams . reset ( ) ;
347+
348+ // trigger reload
349+ $location . hashSearch . foo = 'bar' ;
350+ scope . $eval ( ) ;
351+ expect ( routeParams ) . toHaveBeenCalledWith ( { foo : 'bar' } ) ;
352+ routeParams . reset ( ) ;
353+
354+ $location . updateHash ( '/bar/123' ) ;
355+ scope . $eval ( ) ;
356+ expect ( routeParams ) . toHaveBeenCalledWith ( { barId : '123' } ) ;
357+ routeParams . reset ( ) ;
358+
359+ // don't trigger reload
360+ $location . hashSearch . foo = 'bar' ;
361+ scope . $eval ( ) ;
362+ $route . current . scope . $eval ( ) ; // ng:view propagates evals so we have to do it by hand here
363+ expect ( routeParams ) . toHaveBeenCalledWith ( { barId : '123' , foo : 'bar' } ) ;
364+ } ) ;
365+ } ) ;
230366} ) ;
0 commit comments