Skip to content

Commit

Permalink
Merge pull request #29 from CrowdStrike/account-for-qp-only-transitions
Browse files Browse the repository at this point in the history
fix: intent is not always present, like when during qp-only transitions
  • Loading branch information
NullVoxPopuli authored May 26, 2021
2 parents 559dbd7 + fbc7c3c commit be68721
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
8 changes: 6 additions & 2 deletions addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type RouterService from '@ember/routing/router-service';

type Transition = Parameters<Route['beforeModel']>[0];
type TransitionWithPrivateAPIs = Transition & {
intent: {
intent?: {
url: string;
};
};
Expand Down Expand Up @@ -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);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/integration/hash-test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand Down Expand Up @@ -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`
<LinkTo id='foo' @query={{hash test='foo'}}>foo</LinkTo>
<LinkTo id='default' @query={{hash test=false}}>default</LinkTo>
{{outlet}}
`
);
this.owner.register(
'template:index',
hbs`
<out>
qp: {{this.router.currentRoute.queryParams.test}}
</out>
`
);

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',
Expand Down

0 comments on commit be68721

Please sign in to comment.