diff --git a/packages/ember/tests/routing/router_service_test/replaceWith_test.js b/packages/ember/tests/routing/router_service_test/replaceWith_test.js index cd98bf46ed0..b688b317196 100644 --- a/packages/ember/tests/routing/router_service_test/replaceWith_test.js +++ b/packages/ember/tests/routing/router_service_test/replaceWith_test.js @@ -1,7 +1,8 @@ import NoneLocation from '@ember/routing/none-location'; -import { RouterTestCase, moduleFor } from 'internal-test-helpers'; +import { RouterTestCase, moduleFor, runLoopSettled } from 'internal-test-helpers'; import { InternalTransition as Transition } from 'router_js'; import Controller from '@ember/controller'; +import Route from '@ember/routing/route'; moduleFor( 'Router Service - replaceWith', @@ -136,5 +137,45 @@ moduleFor( assert.deepEqual(this.state, ['/', '/child']); }); } + + async ['@test RouterService#replaceWith with `refreshModel: true` query param does not always refresh']( + assert + ) { + assert.expect(3); + + let parentBeforeModelCount = 0; + + this.add( + 'route:parent', + Route.extend({ + beforeModel() { + parentBeforeModelCount++; + }, + queryParams: { + foo: { + refreshModel: true, + }, + }, + }) + ); + + this.add( + 'controller:parent', + Controller.extend({ + queryParams: ['foo'], + foo: 'default', + }) + ); + + await this.visit('/child'); + + assert.equal(parentBeforeModelCount, 1, 'parent.beforeModel called once'); + + this.routerService.replaceWith('parent.sister'); + await runLoopSettled(); + + assert.equal(this.routerService.get('currentURL'), '/sister', 'transitioned'); + assert.equal(parentBeforeModelCount, 1, 'parent.beforeModel still called only once'); + } } ); diff --git a/packages/ember/tests/routing/router_service_test/transitionTo_test.js b/packages/ember/tests/routing/router_service_test/transitionTo_test.js index 71409cbe433..5c0d6a8d8fc 100644 --- a/packages/ember/tests/routing/router_service_test/transitionTo_test.js +++ b/packages/ember/tests/routing/router_service_test/transitionTo_test.js @@ -5,7 +5,7 @@ import NoneLocation from '@ember/routing/none-location'; import Controller from '@ember/controller'; import { run } from '@ember/runloop'; import { get } from '@ember/object'; -import { RouterTestCase, moduleFor } from 'internal-test-helpers'; +import { RouterTestCase, moduleFor, runLoopSettled } from 'internal-test-helpers'; import { InternalTransition as Transition } from 'router_js'; moduleFor( @@ -423,5 +423,146 @@ moduleFor( assert.equal(this.routerService.get('currentURL'), '/?url_sort=a'); }); } + + async ['@test RouterService#transitionTo with `refreshModel: true` query param does not always refresh']( + assert + ) { + assert.expect(3); + + let parentBeforeModelCount = 0; + + this.add( + 'route:parent', + Route.extend({ + beforeModel() { + parentBeforeModelCount++; + }, + queryParams: { + foo: { + refreshModel: true, + }, + }, + }) + ); + + this.add( + 'controller:parent', + Controller.extend({ + queryParams: ['foo'], + foo: 'default', + }) + ); + + await this.visit('/child'); + + assert.equal(parentBeforeModelCount, 1, 'parent.beforeModel called once'); + + this.routerService.transitionTo('parent.sister'); + await runLoopSettled(); + + assert.equal(this.routerService.get('currentURL'), '/sister', 'transitioned'); + assert.equal(parentBeforeModelCount, 1, 'parent.beforeModel still called only once'); + } + + async ['@test RouterService#transitionTo query param transition during initial transition']( + assert + ) { + assert.expect(1); + + let initialTransition = true; + + this.add( + 'route:parent', + Route.extend({ + queryParams: { + foo: { + refreshModel: true, + }, + }, + }) + ); + + this.add( + 'controller:parent', + Controller.extend({ + queryParams: ['foo'], + foo: 'default', + }) + ); + + this.add( + 'route:parent.child', + Route.extend({ + routerService: service('router'), + afterModel() { + if (initialTransition) { + initialTransition = false; + this.routerService.transitionTo('parent.child', { + queryParams: { + foo: 'bar', + }, + }); + } + }, + }) + ); + + await this.visit('/child'); + + assert.equal(this.routerService.get('currentURL'), '/child?foo=bar'); + } + + async ['@test RouterService#transitionTo query param transition during later transition']( + assert + ) { + assert.expect(2); + + let initialTransition = true; + + this.add( + 'route:parent', + Route.extend({ + queryParams: { + foo: { + refreshModel: true, + }, + }, + }) + ); + + this.add( + 'controller:parent', + Controller.extend({ + queryParams: ['foo'], + foo: 'default', + }) + ); + + this.add( + 'route:parent.child', + Route.extend({ + routerService: service('router'), + afterModel() { + if (initialTransition) { + initialTransition = false; + this.routerService.transitionTo('parent.child', { + queryParams: { + foo: 'bar', + }, + }); + } + }, + }) + ); + + await this.visit('/'); + + assert.equal(this.routerService.get('currentURL'), '/'); + + this.routerService.transitionTo('parent.child'); + await runLoopSettled(); + + assert.equal(this.routerService.get('currentURL'), '/child?foo=bar'); + } } );