Skip to content

Commit

Permalink
Merge pull request #491 from ember-a11y/drewlee/test-helpers-fix
Browse files Browse the repository at this point in the history
Fixes currentRouteName middleware reporter test failures
  • Loading branch information
Andrew A Lee authored Feb 2, 2023
2 parents cace71a + 0d30079 commit 6cce3ec
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
26 changes: 25 additions & 1 deletion addon-test-support/setup-middleware-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ let currentViolationsMap: Map<string, Result> | undefined = undefined;
let currentUrls: Set<string> | undefined;
let currentRouteNames: Set<string> | undefined;

/**
* Utility to retrieve the route name corresponding to the current test. Absorbs the emitted
* assertion error if route name is `null`, resulting in an empty string return value.
*
* @param getFn Function to use to derive the route name.
* @returns Route name or empty string.
*/
export function _getCurrentRouteName(getFn = currentRouteName): string {
let routeName = '';

try {
routeName = getFn();
} catch (error: unknown) {
if (
error instanceof Error &&
!/currentRouteName (\w|\s)+ string/.test(error.message)
) {
throw error;
}
}

return routeName;
}

/**
* A custom reporter that is invoked once per failed a11yAudit call. This can be called
* multiple times per test, and the results are accumulated until testDone.
Expand Down Expand Up @@ -73,7 +97,7 @@ export async function middlewareReporter(axeResults: AxeResults) {
}

currentUrls!.add(currentURL());
currentRouteNames!.add(currentRouteName());
currentRouteNames!.add(_getCurrentRouteName());

axeResults.violations.forEach((violation) => {
let rule = currentViolationsMap!.get(violation.id);
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/get-current-route-name-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { _getCurrentRouteName } from 'ember-a11y-testing/test-support/setup-middleware-reporter';

module('Unit | Utils | _getCurrentRouteName', function (hooks) {
setupTest(hooks);

test('gets the route name for the current test', function (assert) {
function mockCurrentRouteName(): string {
return 'index';
}

const result = _getCurrentRouteName(mockCurrentRouteName);

assert.strictEqual(result, 'index');
});

test('absorbs `currentRouteName` error when route name is null', function (assert) {
function currentRouteNameMock(): string {
throw new Error('currentRouteName shoudl be a string');
}

const result = _getCurrentRouteName(currentRouteNameMock);

assert.strictEqual(result, '');
});

test('bubbles up all other emitted errors', function (assert) {
function mockCurrentRouteName(): string {
throw new Error('Catastrophic error!');
}

assert.throws(() => {
_getCurrentRouteName(mockCurrentRouteName);
}, /Catastrophic error!/);
});
});

0 comments on commit 6cce3ec

Please sign in to comment.