From 4e097e6f5abe652dd0cdfc74c4c747b8e00e4d5a Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Thu, 26 Aug 2021 16:45:30 -0600 Subject: [PATCH] [BUGFIX release] fix router test regression in urlFor and recognize As part of the improvements made between 3.24 and 3.28, the router microlib is now lazily loaded. When these changes were made, there were a couple cases where it *should* be possible to access router state in a non-application test (integration etc.) but it currently is not because the router is not necessarily set up. Since `setupRouter` is idempotent, call it in those functions so that if it is *not* set up, it gets set up, and otherwise it will continue working as expected. (cherry picked from commit 9f61c7a8520e01856e94893bf2049cc69f788397) --- packages/@ember/-internals/routing/lib/services/router.ts | 3 +++ .../router_service_test/non_application_test_test.js | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/@ember/-internals/routing/lib/services/router.ts b/packages/@ember/-internals/routing/lib/services/router.ts index c675b9e8087..68f2de42293 100644 --- a/packages/@ember/-internals/routing/lib/services/router.ts +++ b/packages/@ember/-internals/routing/lib/services/router.ts @@ -240,6 +240,7 @@ export default class RouterService extends Service { @public */ urlFor(routeName: string, ...args: any[]) { + this._router.setupRouter(); return this._router.generate(routeName, ...args); } @@ -375,6 +376,7 @@ export default class RouterService extends Service { `You must pass a url that begins with the application's rootURL "${this.rootURL}"`, url.indexOf(this.rootURL) === 0 ); + this._router.setupRouter(); let internalURL = cleanURL(url, this.rootURL); return this._router._routerMicrolib.recognize(internalURL); } @@ -395,6 +397,7 @@ export default class RouterService extends Service { `You must pass a url that begins with the application's rootURL "${this.rootURL}"`, url.indexOf(this.rootURL) === 0 ); + this._router.setupRouter(); let internalURL = cleanURL(url, this.rootURL); return this._router._routerMicrolib.recognizeAndLoad(internalURL); } diff --git a/packages/ember/tests/routing/router_service_test/non_application_test_test.js b/packages/ember/tests/routing/router_service_test/non_application_test_test.js index add81994bdd..f24f24b8e00 100644 --- a/packages/ember/tests/routing/router_service_test/non_application_test_test.js +++ b/packages/ember/tests/routing/router_service_test/non_application_test_test.js @@ -66,14 +66,15 @@ moduleFor( } ['@test RouterService#urlFor returns url'](assert) { - let router = this.owner.lookup('router:main'); - router.setupRouter(); assert.equal(this.routerService.urlFor('parent.child'), '/child'); } ['@test RouterService#transitionTo with basic route'](assert) { assert.expect(2); + // Callers who want to actually execute a transition in a non-application + // test are doing something weird and therefore should do + // `owner.setupRouter()` explicitly in their tests. let componentInstance; let router = this.owner.lookup('router:main'); router.setupRouter(); @@ -107,8 +108,6 @@ moduleFor( } ['@test RouterService#recognize recognize returns routeInfo'](assert) { - let router = this.owner.lookup('router:main'); - router.setupRouter(); let routeInfo = this.routerService.recognize('/dynamic-with-child/123/1?a=b'); assert.ok(routeInfo); let { name, localName, parent, child, params, queryParams, paramNames } = routeInfo;