Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Melsumner/issue 378 #379

Merged
merged 12 commits into from
Aug 5, 2024
15 changes: 8 additions & 7 deletions addon/components/navigation-narrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ export default class NavigationNarratorComponent extends Component {
/*
* @param hasQueryParams
* @type {boolean}
* @description Detect if the `transition.from` or the `transition.to` has queryParams.
* @description Check for queryParams.
* @default false
*/
get hasQueryParams() {
const qps =
(this.transition.from && this.transition.from.queryParams) ||
(this.transition.to && this.transition.to.queryParams);

if (qps && Object.keys(qps).length > 0) {
if (
Object.keys(this.transition.from.queryParams || {}).length ||
Object.keys(this.transition.to.queryParams).length > 0
) {
return true;
} else {
return false;
Expand All @@ -108,7 +107,9 @@ export default class NavigationNarratorComponent extends Component {
let shouldFocus;
this.transition = transition; // We need to do this because we can't pass an argument to a getter

if (this.excludeAllQueryParams && this.hasQueryParams) {
let hasSameRoute = this.transition.from.name === this.transition.to.name;

if (this.excludeAllQueryParams && this.hasQueryParams && hasSameRoute) {
Comment on lines +111 to +113
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the failing test pass, but I also wonder if there's a more elegant way to do this. Suggestions welcome.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The this.hasQueryParams might not be needed with the hasSameRoute.

return;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"loader.js": "^4.7.0",
"npm-run-all": "^4.1.5",
"npm-run-all2": "^5.0.0",
"prettier": "^2.5.1",
"qunit": "^2.17.2",
Expand Down
88 changes: 88 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions tests/integration/components/navigation-narrator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,88 @@ module('Integration | Component | navigation-narrator', function (hooks) {

assert.dom('#ember-a11y-refocus-nav-message').isFocused();
});

test('excludeAllQueryParams is true, queryParams do not exist but are added, do not manage focus', async function (assert) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is meant to cover a scenario like this:

myurl.com/someRoute --> myurl.com/someRoute?someQueryParam=foo

let router = this.owner.lookup('service:router');

await render(hbs`<NavigationNarrator @excludeAllQueryParams={{true}} />`);
router.trigger(
'routeDidChange',
new MockTransition({
from: new MockRouteInfo({
name: 'biscuit',
params: { id: 'hobnob' },
}),
to: new MockRouteInfo({
name: 'biscuit',
params: { id: 'hobnob' },
queryParams: { region: 'apac' },
}),
})
);

await settled();

assert.dom('#ember-a11y-refocus-nav-message').isNotFocused();
});

test('excludeAllQueryParams is true, queryParams exist on the route but then are emptied, still do not manage focus', async function (assert) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is meant to cover a scenario like this:

myurl.com/someRoute?someQueryParam=foo --> myurl.com/someRoute

let router = this.owner.lookup('service:router');

await render(hbs`<NavigationNarrator @excludeAllQueryParams={{true}} />`);
router.trigger(
'routeDidChange',
new MockTransition({
from: new MockRouteInfo({
name: 'biscuit',
params: { id: 'hobnob' },
queryParams: { region: 'apac' },
}),
to: new MockRouteInfo({
name: 'biscuit',
params: { id: 'hobnob' },
}),
})
);

await settled();

assert.dom('#ember-a11y-refocus-nav-message').isNotFocused();
});

test('excludeAllQueryParams is true, queryParams exist on the route, user navigates to new route without any query params, manage focus', async function (assert) {
Copy link
Member Author

@MelSumner MelSumner Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is meant to cover a scenario like this:

myurl.com/someRoute?someQueryParam=foo --> myurl.com/someOtherRoute

let router = this.owner.lookup('service:router');

await render(hbs`<NavigationNarrator @excludeAllQueryParams={{true}} />`);
router.trigger(
'routeDidChange',
new MockTransition({
Copy link
Member Author

@MelSumner MelSumner Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same format found here, with a query param added to the from:

test('when routes differ', function (assert) {

from: new MockRouteInfo({
name: 'biscuit',
params: { id: 'hobnob' },
queryParams: { region: 'apac' },
parent: new MockRouteInfo({
name: 'biscuits',
parent: new MockRouteInfo({
name: 'application',
}),
}),
}),
to: new MockRouteInfo({
name: 'index',
parent: new MockRouteInfo({
name: 'cakes',
parent: new MockRouteInfo({
name: 'application',
}),
}),
}),
})
);

await settled();

assert.dom('#ember-a11y-refocus-nav-message').isFocused();
});
});
});
Loading