|
1 | | -import { service } from '@ember/service'; |
| 1 | +import Service, { service } from '@ember/service'; |
2 | 2 | import { Component } from '@ember/-internals/glimmer'; |
3 | 3 | import Route from '@ember/routing/route'; |
4 | 4 | import NoneLocation from '@ember/routing/none-location'; |
@@ -439,5 +439,72 @@ moduleFor( |
439 | 439 | assert.equal(this.routerService.get('currentURL'), '/?url_sort=a'); |
440 | 440 | }); |
441 | 441 | } |
| 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 | + await this.followRedirects(); |
| 494 | + |
| 495 | + assert.equal( |
| 496 | + this.routerService.get('currentRouteName'), |
| 497 | + 'parent', |
| 498 | + 'redirects to the target route' |
| 499 | + ); |
| 500 | + |
| 501 | + let currentURL = this.routerService.get('currentURL'); |
| 502 | + |
| 503 | + assert.equal(currentURL, '/parent?foo=bar', 'uses the query param in the final URL'); |
| 504 | + |
| 505 | + let controller = this.controllerFor('parent'); |
| 506 | + |
| 507 | + assert.equal(controller.get('foo'), 'bar', 'sets the controller query param'); |
| 508 | + } |
442 | 509 | } |
443 | 510 | ); |
0 commit comments