diff --git a/addon/index.ts b/addon/index.ts index e9d8466..290368e 100644 --- a/addon/index.ts +++ b/addon/index.ts @@ -11,7 +11,7 @@ import type RouterService from '@ember/routing/router-service'; type Transition = Parameters[0]; type TransitionWithPrivateAPIs = Transition & { - intent: { + intent?: { url: string; }; }; @@ -97,7 +97,11 @@ async function setupHashSupport(router: EmberRouter) { let routerService = owner.lookup('service:router') as RouterService; function handleHashIntent(transition: TransitionWithPrivateAPIs) { - let { url } = transition.intent; + let { url } = transition.intent || {}; + + if (!url) { + return; + } eventuallyTryScrollingTo(owner, url); } diff --git a/tests/integration/hash-test.ts b/tests/integration/hash-test.ts index 133bbad..7fa6d9a 100644 --- a/tests/integration/hash-test.ts +++ b/tests/integration/hash-test.ts @@ -1,5 +1,6 @@ import Controller from '@ember/controller'; import { assert as debugAssert } from '@ember/debug'; +import { inject as service } from '@ember/service'; import { click, find, settled, visit } from '@ember/test-helpers'; import { hbs } from 'ember-cli-htmlbars'; import { module, test } from 'qunit'; @@ -9,6 +10,8 @@ import { scrollToHash, uiSettled } from 'ember-url-hash-polyfill'; import { setupRouter } from './-helpers'; +import type RouterService from '@ember/routing/router-service'; + module('Hash', function (hooks) { setupApplicationTest(hooks); @@ -143,6 +146,54 @@ module('Hash', function (hooks) { }, }); + test('transitioning only via query params does not break things', async function (assert) { + class TestApplication extends Controller { + queryParams = ['test']; + test = false; + } + class Index extends Controller { + @service declare router: RouterService; + } + + this.owner.register('controller:application', TestApplication); + this.owner.register('controller:index', Index); + this.owner.register( + 'template:application', + hbs` + foo + default + {{outlet}} + ` + ); + this.owner.register( + 'template:index', + hbs` + + qp: {{this.router.currentRoute.queryParams.test}} + + ` + ); + + let router = this.owner.lookup('service:router'); + + await visit('/'); + assert.dom('out').hasText('qp:'); + + await click('#foo'); + assert.dom('out').hasText('qp: foo'); + + await click('#default'); + assert.dom('out').hasText('qp:'); + + router.transitionTo({ queryParams: { test: 'foo' } }); + await settled(); + assert.dom('out').hasText('qp: foo'); + + router.transitionTo({ queryParams: { test: false } }); + await settled(); + assert.dom('out').hasText('qp: false'); + }); + test('cross-page-Llinks are properly scrolled to', async function (assert) { this.owner.register( 'template:foo',