Skip to content

Commit

Permalink
Melsumner/issue 378 (#379)
Browse files Browse the repository at this point in the history
* add devdep
* add test
* update tests to cover additional combinations of scenarios
* Updating the hasQueryParams function
  • Loading branch information
MelSumner authored Aug 5, 2024
1 parent 7d20ca2 commit 7fe1789
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 7 deletions.
16 changes: 9 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,10 @@ 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) {
// add a check to see if it's the same route (question: what if the user refreshes the page?)
let hasSameRoute = this.transition.from.name === this.transition.to.name;

if (this.excludeAllQueryParams && this.hasQueryParams && 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) {
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) {
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) {
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' },
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();
});
});
});

0 comments on commit 7fe1789

Please sign in to comment.