Skip to content

Commit

Permalink
Updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scalvert committed Sep 4, 2020
1 parent 0eae3e1 commit a6de773
Show file tree
Hide file tree
Showing 6 changed files with 3,434 additions and 12 deletions.
5 changes: 5 additions & 0 deletions addon-test-support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ export {
teardownGlobalA11yHooks,
} from './setup-global-a11y-hooks';
export { setCustomReporter } from './reporter';
export {
TEST_SUITE_RESULTS as _TEST_SUITE_RESULTS,
middlewareReporter as _middlewareReporter,
setupMiddlewareReporter,
} from './setup-middleware-reporter';

export { InvocationStrategy, A11yAuditReporter } from './types';
35 changes: 25 additions & 10 deletions addon-test-support/setup-middleware-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import QUnit from 'qunit';
import { getContext, getTestMetadata } from '@ember/test-helpers';
import { AxeResults } from 'axe-core';
import { setCustomReporter } from './reporter';
import { DEBUG } from '@glimmer/env';

const TEST_SUITE_RESULTS: {
export const TEST_SUITE_RESULTS: {
moduleName: string;
testName: string;
helperName: string;
stack: string;
axeResults: AxeResults;
}[] = [];

export function setupMiddlewareReporter() {
setCustomReporter(async (axeResults: AxeResults) => {
let { module, testName } = QUnit.config.current;
export function buildResult(axeResults: AxeResults) {
let { module, testName } = QUnit.config.current;
let testMetaData = getTestMetadata(getContext());

TEST_SUITE_RESULTS.push({
moduleName: module.name,
testName,
axeResults,
});
});
let stack = (!DEBUG && new Error().stack) || '';

return {
moduleName: module.name,
testName,
helperName: testMetaData.usedHelpers.pop() || '',
stack,
axeResults,
};
}

export async function middlewareReporter(axeResults: AxeResults) {
TEST_SUITE_RESULTS.push(buildResult(axeResults));
}

export function setupMiddlewareReporter() {
setCustomReporter(middlewareReporter);

QUnit.done(async function () {
let response = await fetch('/report-violations', {
Expand Down
18 changes: 16 additions & 2 deletions node-tests/setup-middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ function createTmpDir() {
return fs.realpathSync(tmp.dirSync({ unsafeCleanup: true }).name);
}

function buildResult(axeResults) {
let { module, testName } = QUnit.config.current;

return {
moduleName: module.name,
testName,
helperName: 'visit',
stack: 'STACK',
axeResults,
};
}

QUnit.module('setupMiddleware', function (hooks) {
let tmpDir;
let app;
Expand All @@ -32,14 +44,16 @@ QUnit.module('setupMiddleware', function (hooks) {
QUnit.test('can respond to requests to report violations', async function (
assert
) {
let data = buildResult(violationsFixture);

let json = await fetch('http://localhost:3000/report-violations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(violationsFixture),
body: JSON.stringify(data),
}).then((res) => res.json());

assert.deepEqual(readJSONSync(json.outputPath), violationsFixture);
assert.deepEqual(readJSONSync(json.outputPath), data);
});
});
39 changes: 39 additions & 0 deletions tests/acceptance/setup-middleware-reporter-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';
import {
setCustomReporter,
setEnableA11yAudit,
setupGlobalA11yHooks,
teardownGlobalA11yHooks,
_middlewareReporter,
_TEST_SUITE_RESULTS,
} from 'ember-a11y-testing/test-support';

module('setupMiddlewareReporter', function (hooks) {
setupApplicationTest(hooks);

function invokeAll(): boolean {
return true;
}

hooks.beforeEach(function () {
setCustomReporter(_middlewareReporter);
setupGlobalA11yHooks(invokeAll);
setEnableA11yAudit(true);
});

hooks.afterEach(function () {
setCustomReporter();
teardownGlobalA11yHooks();
setEnableA11yAudit();
});

test('gathers results from failed a11yAudit calls', async function (assert) {
assert.expect(1);

await visit('/');

assert.deepEqual(_TEST_SUITE_RESULTS[0].axeResults.violations.length, 3);
});
});
Loading

0 comments on commit a6de773

Please sign in to comment.