test-a11y-js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,45 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Marlon Maniti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ## API Reference
24
+
25
+ ### A11yChecker
26
+
27
+ #### `check(element: Element): Promise<A11yResults>`
28
+ Performs all accessibility checks on the given element.
29
+
30
+ #### `checkImageAlt(element: Element): A11yViolation[]`
31
+ Checks images for proper alt attributes.
32
+
33
+ #### `checkLinkText(element: Element): A11yViolation[]`
34
+ Validates link text for accessibility.
35
+
36
+ #### `checkButtonLabel(element: Element): A11yViolation[]`
37
+ Ensures buttons have proper labels.
38
+
39
+ #### `checkFormLabels(element: Element): A11yViolation[]`
40
+ Validates form control label associations.
41
+
42
+ #### `checkHeadingOrder(element: Element): A11yViolation[]`
43
+ Checks heading hierarchy.
44
+
45
+ ### Types
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # test-a11y-js
2
+
3
+ A JavaScript library for testing component accessibility across multiple testing frameworks.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install test-a11y-js
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { A11yChecker } from 'test-a11y-js'
15
+ // Test a DOM element for accessibility violations
16
+ const results = await A11yChecker.check(element)
17
+ // Individual checks
18
+ const imageViolations = A11yChecker.checkImageAlt(element)
19
+ const linkViolations = A11yChecker.checkLinkText(element)
20
+ const buttonViolations = A11yChecker.checkButtonLabel(element)
21
+ const formViolations = A11yChecker.checkFormLabels(element)
22
+ const headingViolations = A11yChecker.checkHeadingOrder(element)
23
+ ```
24
+
25
+ ## API
26
+
27
+ ### A11yChecker
28
+
29
+ #### `check(element: Element): Promise<A11yResults>`
30
+ Performs all accessibility checks on the given element.
31
+
32
+ #### `checkImageAlt(element: Element): A11yViolation[]`
33
+ Checks images for proper alt attributes.
34
+
35
+ #### `checkLinkText(element: Element): A11yViolation[]`
36
+ Validates link text for accessibility.
37
+
38
+ #### `checkButtonLabel(element: Element): A11yViolation[]`
39
+ Ensures buttons have proper labels.
40
+
41
+ #### `checkFormLabels(element: Element): A11yViolation[]`
42
+ Validates form control label associations.
43
+
44
+ #### `checkHeadingOrder(element: Element): A11yViolation[]`
45
+ Checks heading hierarchy.
46
+
47
+ ### Types
48
+
49
+ ```typescript
50
+ interface A11yViolation {
51
+ id: string
52
+ description: string
53
+ element: Element
54
+ impact: 'critical' | 'serious' | 'moderate' | 'minor'
55
+ }
56
+
57
+ interface A11yResults {
58
+ violations: A11yViolation[]
59
+ }
60
+ ```
61
+
62
+ ## Features
63
+
64
+ - Image alt text validation
65
+ - Link text accessibility checks
66
+ - Button label validation
67
+ - Form label association checks
68
+ - Heading order validation
69
+
70
+ ## Author
71
+
72
+ Marlon Maniti (https://github.com/nolrm)
73
+
74
+ ## License
75
+
76
+ MIT
@@ -0,0 +1,19 @@
1
+ interface A11yViolation {
2
+ id: string;
3
+ description: string;
4
+ element: Element;
5
+ impact: 'critical' | 'serious' | 'moderate' | 'minor';
6
+ }
7
+ interface A11yResults {
8
+ violations: A11yViolation[];
9
+ }
10
+ declare class A11yChecker {
11
+ static checkImageAlt(element: Element): A11yViolation[];
12
+ static checkButtonLabel(element: Element): A11yViolation[];
13
+ static checkFormLabels(element: Element): A11yViolation[];
14
+ static checkHeadingOrder(element: Element): A11yViolation[];
15
+ static checkLinkText(element: Element): A11yViolation[];
16
+ static check(element: Element): Promise<A11yResults>;
17
+ }
18
+
19
+ export { A11yChecker, A11yResults, A11yViolation };
@@ -0,0 +1,19 @@
1
+ interface A11yViolation {
2
+ id: string;
3
+ description: string;
4
+ element: Element;
5
+ impact: 'critical' | 'serious' | 'moderate' | 'minor';
6
+ }
7
+ interface A11yResults {
8
+ violations: A11yViolation[];
9
+ }
10
+ declare class A11yChecker {
11
+ static checkImageAlt(element: Element): A11yViolation[];
12
+ static checkButtonLabel(element: Element): A11yViolation[];
13
+ static checkFormLabels(element: Element): A11yViolation[];
14
+ static checkHeadingOrder(element: Element): A11yViolation[];
15
+ static checkLinkText(element: Element): A11yViolation[];
16
+ static check(element: Element): Promise<A11yResults>;
17
+ }
18
+
19
+ export { A11yChecker, A11yResults, A11yViolation };
package/dist/index.js ADDED
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ A11yChecker: () => A11yChecker
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/core/a11y-checker.ts
28
+ var A11yChecker = class {
29
+ static checkImageAlt(element) {
30
+ const violations = [];
31
+ const images = element.getElementsByTagName("img");
32
+ for (const img of Array.from(images)) {
33
+ if (!img.hasAttribute("alt")) {
34
+ violations.push({
35
+ id: "image-alt",
36
+ description: "Image must have an alt attribute",
37
+ element: img,
38
+ impact: "serious"
39
+ });
40
+ } else if (img.getAttribute("alt")?.trim() === "") {
41
+ violations.push({
42
+ id: "image-alt",
43
+ description: "Image alt attribute must not be empty",
44
+ element: img,
45
+ impact: "serious"
46
+ });
47
+ }
48
+ }
49
+ return violations;
50
+ }
51
+ static checkButtonLabel(element) {
52
+ const violations = [];
53
+ const buttons = element.getElementsByTagName("button");
54
+ for (const button of Array.from(buttons)) {
55
+ if (!button.textContent?.trim() && !button.getAttribute("aria-label")) {
56
+ violations.push({
57
+ id: "button-label",
58
+ description: "Button must have a label or aria-label",
59
+ element: button,
60
+ impact: "critical"
61
+ });
62
+ }
63
+ }
64
+ return violations;
65
+ }
66
+ static checkFormLabels(element) {
67
+ const violations = [];
68
+ const inputs = element.querySelectorAll("input, select, textarea");
69
+ for (const input of Array.from(inputs)) {
70
+ const hasLabel = input.hasAttribute("id") && element.querySelector(`label[for="${input.getAttribute("id")}"]`);
71
+ const hasAriaLabel = input.hasAttribute("aria-label");
72
+ const hasAriaLabelledBy = input.hasAttribute("aria-labelledby") && document.getElementById(input.getAttribute("aria-labelledby") || "");
73
+ if (!hasLabel && !hasAriaLabel && !hasAriaLabelledBy) {
74
+ violations.push({
75
+ id: "form-label",
76
+ description: "Form control must have an associated label",
77
+ element: input,
78
+ impact: "critical"
79
+ });
80
+ }
81
+ }
82
+ return violations;
83
+ }
84
+ static checkHeadingOrder(element) {
85
+ const violations = [];
86
+ const headings = element.querySelectorAll("h1, h2, h3, h4, h5, h6");
87
+ let previousLevel = 0;
88
+ for (const heading of Array.from(headings)) {
89
+ const currentLevel = parseInt(heading.tagName[1]);
90
+ if (previousLevel > 0 && currentLevel - previousLevel > 1) {
91
+ violations.push({
92
+ id: "heading-order",
93
+ description: `Heading level skipped from h${previousLevel} to h${currentLevel}`,
94
+ element: heading,
95
+ impact: "moderate"
96
+ });
97
+ }
98
+ previousLevel = currentLevel;
99
+ }
100
+ return violations;
101
+ }
102
+ static checkLinkText(element) {
103
+ const violations = [];
104
+ const links = element.getElementsByTagName("a");
105
+ for (const link of Array.from(links)) {
106
+ const text = link.textContent?.trim().toLowerCase() || "";
107
+ const ariaLabel = link.getAttribute("aria-label")?.toLowerCase();
108
+ if (!text && !ariaLabel) {
109
+ violations.push({
110
+ id: "link-text",
111
+ description: "Link must have descriptive text",
112
+ element: link,
113
+ impact: "serious"
114
+ });
115
+ } else if (["click here", "read more", "more"].includes(text) && !ariaLabel) {
116
+ violations.push({
117
+ id: "link-text-descriptive",
118
+ description: "Link text should be more descriptive",
119
+ element: link,
120
+ impact: "moderate"
121
+ });
122
+ }
123
+ }
124
+ return violations;
125
+ }
126
+ static async check(element) {
127
+ const violations = [
128
+ ...this.checkImageAlt(element),
129
+ ...this.checkLinkText(element),
130
+ ...this.checkButtonLabel(element),
131
+ ...this.checkFormLabels(element),
132
+ ...this.checkHeadingOrder(element)
133
+ ];
134
+ if (violations.length > 0) {
135
+ console.warn("\nAccessibility Violations Found:");
136
+ violations.forEach((violation, index) => {
137
+ console.warn(`
138
+ ${index + 1}. ${violation.id} (${violation.impact})`);
139
+ console.warn(` Description: ${violation.description}`);
140
+ console.warn(` Element: ${violation.element.outerHTML}`);
141
+ });
142
+ console.warn("\n");
143
+ }
144
+ return { violations };
145
+ }
146
+ };
147
+ // Annotate the CommonJS export names for ESM import in node:
148
+ 0 && (module.exports = {
149
+ A11yChecker
150
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,123 @@
1
+ // src/core/a11y-checker.ts
2
+ var A11yChecker = class {
3
+ static checkImageAlt(element) {
4
+ const violations = [];
5
+ const images = element.getElementsByTagName("img");
6
+ for (const img of Array.from(images)) {
7
+ if (!img.hasAttribute("alt")) {
8
+ violations.push({
9
+ id: "image-alt",
10
+ description: "Image must have an alt attribute",
11
+ element: img,
12
+ impact: "serious"
13
+ });
14
+ } else if (img.getAttribute("alt")?.trim() === "") {
15
+ violations.push({
16
+ id: "image-alt",
17
+ description: "Image alt attribute must not be empty",
18
+ element: img,
19
+ impact: "serious"
20
+ });
21
+ }
22
+ }
23
+ return violations;
24
+ }
25
+ static checkButtonLabel(element) {
26
+ const violations = [];
27
+ const buttons = element.getElementsByTagName("button");
28
+ for (const button of Array.from(buttons)) {
29
+ if (!button.textContent?.trim() && !button.getAttribute("aria-label")) {
30
+ violations.push({
31
+ id: "button-label",
32
+ description: "Button must have a label or aria-label",
33
+ element: button,
34
+ impact: "critical"
35
+ });
36
+ }
37
+ }
38
+ return violations;
39
+ }
40
+ static checkFormLabels(element) {
41
+ const violations = [];
42
+ const inputs = element.querySelectorAll("input, select, textarea");
43
+ for (const input of Array.from(inputs)) {
44
+ const hasLabel = input.hasAttribute("id") && element.querySelector(`label[for="${input.getAttribute("id")}"]`);
45
+ const hasAriaLabel = input.hasAttribute("aria-label");
46
+ const hasAriaLabelledBy = input.hasAttribute("aria-labelledby") && document.getElementById(input.getAttribute("aria-labelledby") || "");
47
+ if (!hasLabel && !hasAriaLabel && !hasAriaLabelledBy) {
48
+ violations.push({
49
+ id: "form-label",
50
+ description: "Form control must have an associated label",
51
+ element: input,
52
+ impact: "critical"
53
+ });
54
+ }
55
+ }
56
+ return violations;
57
+ }
58
+ static checkHeadingOrder(element) {
59
+ const violations = [];
60
+ const headings = element.querySelectorAll("h1, h2, h3, h4, h5, h6");
61
+ let previousLevel = 0;
62
+ for (const heading of Array.from(headings)) {
63
+ const currentLevel = parseInt(heading.tagName[1]);
64
+ if (previousLevel > 0 && currentLevel - previousLevel > 1) {
65
+ violations.push({
66
+ id: "heading-order",
67
+ description: `Heading level skipped from h${previousLevel} to h${currentLevel}`,
68
+ element: heading,
69
+ impact: "moderate"
70
+ });
71
+ }
72
+ previousLevel = currentLevel;
73
+ }
74
+ return violations;
75
+ }
76
+ static checkLinkText(element) {
77
+ const violations = [];
78
+ const links = element.getElementsByTagName("a");
79
+ for (const link of Array.from(links)) {
80
+ const text = link.textContent?.trim().toLowerCase() || "";
81
+ const ariaLabel = link.getAttribute("aria-label")?.toLowerCase();
82
+ if (!text && !ariaLabel) {
83
+ violations.push({
84
+ id: "link-text",
85
+ description: "Link must have descriptive text",
86
+ element: link,
87
+ impact: "serious"
88
+ });
89
+ } else if (["click here", "read more", "more"].includes(text) && !ariaLabel) {
90
+ violations.push({
91
+ id: "link-text-descriptive",
92
+ description: "Link text should be more descriptive",
93
+ element: link,
94
+ impact: "moderate"
95
+ });
96
+ }
97
+ }
98
+ return violations;
99
+ }
100
+ static async check(element) {
101
+ const violations = [
102
+ ...this.checkImageAlt(element),
103
+ ...this.checkLinkText(element),
104
+ ...this.checkButtonLabel(element),
105
+ ...this.checkFormLabels(element),
106
+ ...this.checkHeadingOrder(element)
107
+ ];
108
+ if (violations.length > 0) {
109
+ console.warn("\nAccessibility Violations Found:");
110
+ violations.forEach((violation, index) => {
111
+ console.warn(`
112
+ ${index + 1}. ${violation.id} (${violation.impact})`);
113
+ console.warn(` Description: ${violation.description}`);
114
+ console.warn(` Element: ${violation.element.outerHTML}`);
115
+ });
116
+ console.warn("\n");
117
+ }
118
+ return { violations };
119
+ }
120
+ };
121
+ export {
122
+ A11yChecker
123
+ };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "test-a11y-js",
3
+ "version": "0.1.0",
4
+ "description": "A JavaScript library for testing component accessibility across multiple testing frameworks",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "scripts": {
14
+ "build": "tsup src/index.ts --format cjs,esm --dts",
15
+ "test": "vitest run",
16
+ "test:watch": "vitest watch",
17
+ "test:coverage": "vitest run --coverage",
18
+ "lint": "eslint src/**/*.ts",
19
+ "prepare": "npm run build"
20
+ },
21
+ "keywords": [
22
+ "accessibility",
23
+ "a11y",
24
+ "testing",
25
+ "components",
26
+ "vitest",
27
+ "unit-testing",
28
+ "wcag"
29
+ ],
30
+ "author": "Marlon Maniti (https://github.com/nolrm)",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/nolrm/test-a11y-js.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/nolrm/test-a11y-js/issues"
38
+ },
39
+ "homepage": "https://github.com/nolrm/test-a11y-js#readme",
40
+ "dependencies": {},
41
+ "peerDependencies": {
42
+ "vitest": ">=0.30.0"
43
+ },
44
+ "devDependencies": {
45
+ "@testing-library/dom": "^9.0.0",
46
+ "@types/node": "^18.15.0",
47
+ "@typescript-eslint/eslint-plugin": "^5.57.0",
48
+ "@typescript-eslint/parser": "^5.57.0",
49
+ "@vitejs/plugin-vue": "^4.0.0",
50
+ "@vitest/coverage-c8": "^0.30.0",
51
+ "@vue/test-utils": "^2.4.0",
52
+ "@vue/compiler-sfc": "^3.3.0",
53
+ "@types/vue": "^2.0.0",
54
+ "vue": "^3.3.0",
55
+ "eslint": "^8.37.0",
56
+ "happy-dom": "^9.10.9",
57
+ "typescript": "^5.3.0",
58
+ "vitest": "^0.30.0",
59
+ "tsup": "^7.0.0"
60
+ },
61
+ "engines": {
62
+ "node": ">=14.0.0"
63
+ }
64
+ }