Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

[RFC] feat(accordion): add initial rules #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .yarn-offline-mirror/@babel-generator-7.3.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/@babel-parser-7.3.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/@babel-types-7.3.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/acorn-6.0.6.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/ajv-6.6.2.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/ajv-6.7.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/ansi-escapes-3.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/ansi-escapes-3.2.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/ansi-regex-4.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/append-transform-0.4.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/append-transform-1.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-code-frame-6.26.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-core-6.26.3.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-generator-6.26.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-helpers-6.24.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-jest-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/babel-jest-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-messages-6.23.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-plugin-istanbul-4.1.6.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-preset-jest-23.2.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/babel-preset-jest-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-register-6.26.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-template-6.26.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-traverse-6.26.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babel-types-6.26.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/babylon-6.18.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/callsites-3.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/camelcase-5.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/ci-info-1.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/compare-versions-3.4.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/core-js-2.5.7.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/core-js-2.6.3.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/cross-spawn-5.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/debug-3.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/debug-3.2.6.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/detect-indent-4.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/diff-3.5.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/diff-sequences-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/execa-0.7.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/expect-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/expect-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/find-up-1.1.2.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/fsevents-1.2.4.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/fsevents-1.2.7.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/get-stream-3.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/globals-9.18.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/has-flag-1.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/home-or-tmp-2.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/htm-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/import-local-1.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/import-local-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/invert-kv-1.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/invert-kv-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/is-ci-1.2.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/is-finite-1.0.2.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/is-generator-fn-1.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/is-generator-fn-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/is-utf8-0.2.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/istanbul-api-1.3.7.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/istanbul-api-2.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/istanbul-lib-coverage-1.2.1.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/istanbul-lib-hook-1.2.2.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/istanbul-lib-hook-2.0.3.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/istanbul-lib-report-1.1.5.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/istanbul-reports-1.5.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/istanbul-reports-2.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-changed-files-23.4.2.tgz
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-circus-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-circus-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-cli-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-cli-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-config-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-config-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-diff-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-diff-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-docblock-23.2.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-docblock-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-each-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-each-24.0.0.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-get-type-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-haste-map-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-haste-map-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-jasmine2-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-jasmine2-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-junit-5.2.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-junit-6.2.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-leak-detector-23.6.0.tgz
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-matcher-utils-23.6.0.tgz
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-message-util-23.4.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-message-util-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-mock-23.2.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-mock-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-regex-util-23.3.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-regex-util-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-resolve-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-resolve-24.0.0.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-runner-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-runner-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-runtime-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-runtime-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-serializer-23.0.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-serializer-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-snapshot-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-snapshot-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-util-23.4.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-util-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-validate-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-validate-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-watcher-23.4.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-watcher-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jest-worker-23.2.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/jest-worker-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/js-tokens-3.0.2.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/jsesc-1.3.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/json5-0.5.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/kleur-2.0.2.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/kleur-3.0.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/lcid-1.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/lcid-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/load-json-file-1.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/lru-cache-4.1.5.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/make-dir-1.3.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/map-age-cleaner-0.1.3.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/math-random-1.0.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/math-random-1.0.4.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/mem-1.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/mem-4.1.0.tgz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/os-locale-2.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/os-locale-3.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/p-defer-1.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/p-each-series-1.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/p-is-promise-2.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/p-reduce-1.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/parse-json-2.2.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/path-exists-2.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/path-type-1.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/pirates-4.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/pkg-dir-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/pretty-format-23.6.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/pretty-format-24.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/prompts-0.1.14.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/prompts-2.0.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/pseudomap-1.0.2.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/read-pkg-1.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/read-pkg-up-1.0.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/read-pkg-up-4.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/repeating-2.0.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/sane-2.5.2.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/sane-3.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/semver-5.5.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/sisteransi-0.1.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/sisteransi-1.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/slash-1.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/source-map-support-0.4.18.tgz
Binary file not shown.
Binary file not shown.
Binary file removed .yarn-offline-mirror/source-map-support-0.5.9.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/spdx-correct-3.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/spdx-correct-3.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/spdx-exceptions-2.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/spdx-exceptions-2.2.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/spdx-license-ids-3.0.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/spdx-license-ids-3.0.3.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/sshpk-1.16.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/sshpk-1.16.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/strip-ansi-5.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/strip-bom-2.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/supports-color-3.2.3.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/supports-color-6.1.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/test-exclude-4.2.3.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/test-exclude-5.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/to-fast-properties-1.0.3.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/write-file-atomic-2.3.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/write-file-atomic-2.4.2.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/y18n-3.2.1.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/y18n-4.0.0.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/yallist-2.1.2.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/yargs-11.1.0.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/yargs-12.0.5.tgz
Binary file not shown.
Binary file added .yarn-offline-mirror/yargs-parser-11.1.1.tgz
Binary file not shown.
Binary file removed .yarn-offline-mirror/yargs-parser-9.0.2.tgz
Binary file not shown.
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"clean": "rimraf es lib umd junit.xml",
"format": "prettier --write \"**/*.{scss,css,js,md}\"",
"format:diff": "prettier --list-different \"**/*.{scss,css,js,md}\"",
"format:staged": "prettier --write",
"test": "jest"
},
"devDependencies": {
Expand All @@ -33,11 +34,11 @@
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"babel-jest": "^24.0.0",
"husky": "^1.3.1",
"jest": "^23.6.0",
"jest-circus": "^23.6.0",
"jest-junit": "^5.2.0",
"jest": "^24.0.0",
"jest-circus": "^24.0.0",
"jest-junit": "^6.2.1",
"lint-staged": "^8.1.0",
"prettier": "^1.15.3",
"rimraf": "^2.6.3",
Expand All @@ -59,7 +60,7 @@
},
"lint-staged": {
"*.{js,md}": [
"yarn format",
"yarn format:staged",
"git add"
]
},
Expand Down Expand Up @@ -105,5 +106,9 @@
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
],
"watchPathIgnorePatterns": []
},
"dependencies": {
"htm": "^2.0.0",
"pretty-format": "^24.0.0"
}
}
18 changes: 17 additions & 1 deletion src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@
* LICENSE file in the root directory of this source tree.
*/

export class ValidationError extends Error {}
import format from 'pretty-format';

export class ValidationError extends Error {
constructor([expectation, expected, actual]) {
super(expectation);

if (!expected && !actual) {
this.message = expectation;
} else if (actual) {
this.message =
`${expectation}. Expected \`${format(expected)}\`, ` +
`instead received: \`${format(actual)}\``;
} else {
this.message = `${expectation}. Expected \`${format(expected)}\``;
}
}
}
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
* LICENSE file in the root directory of this source tree.
*/

export * from './error';
export * from './rules';
export * from './runner';
149 changes: 149 additions & 0 deletions src/rules/__tests__/accordion-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Copyright IBM Corp. 2018, 2019
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment jsdom
*/

import { rules } from '../accordion';
import { createRunner } from '../../runner';
import { html } from '../../tools/html';

describe('accordion', () => {
describe('accordion.html', () => {
let runner;

beforeEach(() => {
runner = createRunner(rules, {
only: ['accordion.html'],
});
runner.beforeEach(context => {
const children = context.children.map(
(child, i) => html`
<li class="bx--accordion__item">
<button
class="bx--accordion__heading"
aria-expanded="false"
aria-controls="${`item-${i}`}"
>
<svg class="bx--accordion__arrow"></svg>
<div class="bx--accordion__title">${child.header}</div>
</button>
<div id="${`item-${i}`}" class="bx--accordion__content">
${child.panel}
</div>
</li>
`
);
const node = html`
<ul class="bx--accordion">
${children}
</ul>
`;
document.body.appendChild(node);
return node;
});
runner.afterEach(node => {
node.parentNode.removeChild(node);
});
});

it('should warn about an HTML mismatch', async () => {
runner.beforeEach(() => {
const node = html`
<ul class="bx--accordion"></ul>
`;
document.body.appendChild(node);
return node;
});

const { violations } = await runner.run();
expect(violations.length > 0).toBe(true);
});

it('should validate ids correctly', async () => {
runner.beforeEach(context => {
const children = context.children.map(
(child, i) => html`
<li class="bx--accordion__item">
<button
class="bx--accordion__heading"
aria-expanded="false"
aria-controls="${`item-${i}`}"
>
<svg class="bx--accordion__arrow"></svg>
<div class="bx--accordion__title">${child.header}</div>
</button>
<div class="bx--accordion__content">${child.panel}</div>
</li>
`
);
const node = html`
<ul class="bx--accordion">
${children}
</ul>
`;
document.body.appendChild(node);
return node;
});
const { violations } = await runner.run();
expect(violations.length > 0).toBe(true);
expect(violations).toMatchInlineSnapshot(`
Array [
[Error: Expected the value for \`aria-controls\` to match the id of the panel. Expected \`"item-0"\`],
]
`);
});
});

describe('accordion.header.mouse', () => {
let runner;

beforeEach(() => {
runner = createRunner(rules, {
only: ['accordion.header.mouse'],
});
runner.beforeEach(context => {
const children = context.children.map(
(child, i) => html`
<li class="bx--accordion__item">
<button
class="bx--accordion__heading"
aria-expanded="false"
aria-controls="${`item-${i}`}"
>
<svg class="bx--accordion__arrow"></svg>
<div class="bx--accordion__title">${child.header}</div>
</button>
<div id="${`item-${i}`}" class="bx--accordion__content">
${child.panel}
</div>
</li>
`
);
const node = html`
<ul class="bx--accordion">
${children}
</ul>
`;
document.body.appendChild(node);
return node;
});
runner.afterEach(node => {
node.parentNode.removeChild(node);
});
});

it('should verify aria attributes', async () => {
const { violations } = await runner.run();
expect(violations.length > 0).toBe(true);
expect(violations).toMatchInlineSnapshot(`
Array [
[Error: Expected aria-expanded to be true after the heading is clicked],
]
`);
});
});
});
123 changes: 115 additions & 8 deletions src/rules/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,122 @@
* LICENSE file in the root directory of this source tree.
*/

const rules = [
import { ValidationError } from '../error';
import { diff } from '../tools/diff';
import { createExpected } from '../tools/createElement';

const defaultContext = {
children: [
{
header: 'header-1',
panel: 'panel-1',
},
{
header: 'header-2',
panel: 'panel-2',
},
{
header: 'header-3',
panel: 'panel-3',
},
],
};

export const rules = [
{
id: 'a.rule.identifier',
description: 'A rule description',
origin: 'some.location',
validate(node) {
//
id: 'accordion.html',
description:
'The accordion default state matches at least the expected HTML structure',
level: 'error',
context: defaultContext,
validate(root, context) {
const expected = createExpected(
'ul',
{
class: 'bx--accordion',
},
...context.children.map(child =>
createExpected(
'li',
{
class: 'bx--accordion__item',
},
createExpected(
'button',
{
class: 'bx--accordion__heading',
'aria-expanded': 'false',
'aria-controls': node => {
const attribute = node.getAttribute('aria-controls');
const id = node.parentNode.childNodes[1].id;
if (attribute !== id) {
return [
[
'Expected the value for `aria-controls` to match the id of the panel',
attribute,
id,
],
];
}
},
},
createExpected('svg', {
class: 'bx--accordion__arrow',
})
),
createExpected(
'div',
{
class: 'bx--accordion__content',
},
child.panel
)
)
)
);

const differences = diff(expected, root);
if (differences.length > 0) {
return differences.map(difference => new ValidationError(difference));
}
},
},
];

export default rules;
// https://www.w3.org/TR/wai-aria-practices/#accordion
{
id: 'accordion.header.mouse',
description:
'The accordion header expands its associated panel on mouse click',
reference: 'https://www.w3.org/TR/wai-aria-practices/#accordion',
level: 'error',
context: defaultContext,
validate(root, context) {
const headers = root.querySelectorAll('.bx--accordion__heading');

for (let i = 0; i < context.children.length; i++) {
const header = headers[i];
header.click();

if (header.getAttribute('aria-expanded') === 'false') {
return new ValidationError([
'Expected aria-expanded to be true after the heading is clicked',
]);
}
}
},
},

// Keyboard
// One of [Enter, Space]:
// When focus is on the accordion header for a collapsed panel,
// expands the associated panel.

// Tab: moves focus to next focusable element. All focusable elements in the
// accordion are included in the page Tab sequence

// Shift + tab: moves focus to the previous focusable element; all focusable
// elements in the accordoin are included the page Tab sequence

// Optional
// Down Arrow: if focus is on an accordion header
];
4 changes: 3 additions & 1 deletion src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

import accordion from './accordion';

export const rules = [...accordion];
export const rules = {
accordion,
};
Loading