Skip to content

Commit cc01001

Browse files
committed
test: add test for RouterService#transitionTo with query params from service during beforeModel
1 parent 2af952a commit cc01001

1 file changed

Lines changed: 67 additions & 1 deletion

File tree

packages/ember/tests/routing/router_service_test/transitionTo_test.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { service } from '@ember/service';
1+
import Service, { service } from '@ember/service';
22
import { Component } from '@ember/-internals/glimmer';
33
import Route from '@ember/routing/route';
44
import NoneLocation from '@ember/routing/none-location';
@@ -439,5 +439,71 @@ moduleFor(
439439
assert.equal(this.routerService.get('currentURL'), '/?url_sort=a');
440440
});
441441
}
442+
443+
async ['@test RouterService#transitionTo supports query params from a service during beforeModel'](
444+
assert
445+
) {
446+
assert.expect(3);
447+
448+
let router = this.router;
449+
router.dslCallbacks.length = 0;
450+
router.map(function () {
451+
this.route('parent');
452+
453+
this.route('child', function () {});
454+
});
455+
456+
this.add(
457+
'controller:parent',
458+
class extends Controller {
459+
queryParams = ['foo'];
460+
foo = null;
461+
}
462+
);
463+
464+
this.add(
465+
'service:parent',
466+
class extends Service {
467+
@service
468+
router;
469+
470+
transitionToParent() {
471+
return this.router.transitionTo('parent', {
472+
queryParams: {
473+
foo: 'bar',
474+
},
475+
});
476+
}
477+
}
478+
);
479+
480+
this.add(
481+
'route:child.index',
482+
class extends Route {
483+
@service
484+
parent;
485+
486+
beforeModel() {
487+
return this.parent.transitionToParent();
488+
}
489+
}
490+
);
491+
492+
await this.visit('/child');
493+
494+
assert.equal(
495+
this.routerService.get('currentRouteName'),
496+
'parent',
497+
'redirects to the target route'
498+
);
499+
500+
let currentURL = this.routerService.get('currentURL');
501+
502+
assert.equal(currentURL, '/parent?foo=bar', 'uses the query param in the final URL');
503+
504+
let controller = this.controllerFor('parent');
505+
506+
assert.equal(controller.get('foo'), 'bar', 'sets the controller query param');
507+
}
442508
}
443509
);

0 commit comments

Comments
 (0)