|
| 1 | +/* |
| 2 | + * axe-sort.test.ts |
| 3 | + * |
| 4 | + * Tests the sort order our axe document reporter applies to violations: |
| 5 | + * impact (critical → serious → moderate → minor → null), then conformance |
| 6 | + * standard (WCAG A → AA → AAA → Best Practice → obsolete → none), then rule |
| 7 | + * id for deterministic output. |
| 8 | + * |
| 9 | + * These exercise our own ranking and comparison logic, not axe-core's rule |
| 10 | + * data: every input is a hand-written violation fixture (the contract axe |
| 11 | + * hands us). An axe-core upgrade that re-tags or re-ranks a rule therefore |
| 12 | + * cannot break these tests. |
| 13 | + * |
| 14 | + * Copyright (C) 2020-2025 Posit Software, PBC |
| 15 | + */ |
| 16 | + |
| 17 | +import { unitTest } from "../test.ts"; |
| 18 | +import { assertEquals } from "testing/asserts"; |
| 19 | +import { |
| 20 | + compareViolations, |
| 21 | + impactRank, |
| 22 | + standardRank, |
| 23 | +} from "../../src/resources/formats/html/axe/axe-check.js"; |
| 24 | + |
| 25 | +unitTest( |
| 26 | + "impactRank - orders critical → serious → moderate → minor → null", |
| 27 | + // deno-lint-ignore require-await |
| 28 | + async () => { |
| 29 | + assertEquals(impactRank("critical"), 0); |
| 30 | + assertEquals(impactRank("serious"), 1); |
| 31 | + assertEquals(impactRank("moderate"), 2); |
| 32 | + assertEquals(impactRank("minor"), 3); |
| 33 | + assertEquals(impactRank(null), 4); |
| 34 | + assertEquals(impactRank("unheard-of"), 4); |
| 35 | + }, |
| 36 | +); |
| 37 | + |
| 38 | +unitTest( |
| 39 | + "standardRank - orders A → AA → AAA → Best Practice → obsolete → none", |
| 40 | + // deno-lint-ignore require-await |
| 41 | + async () => { |
| 42 | + assertEquals(standardRank(["wcag2a", "wcag111"]), 0); |
| 43 | + assertEquals(standardRank(["wcag21aa", "wcag143"]), 1); |
| 44 | + assertEquals(standardRank(["wcag2aaa"]), 2); |
| 45 | + assertEquals(standardRank(["best-practice", "cat.color"]), 3); |
| 46 | + assertEquals(standardRank(["wcag2a-obsolete", "wcag411"]), 4); |
| 47 | + assertEquals(standardRank(["cat.color"]), 5); |
| 48 | + }, |
| 49 | +); |
| 50 | + |
| 51 | +unitTest( |
| 52 | + "standardRank - best-practice takes precedence over any WCAG tags", |
| 53 | + // deno-lint-ignore require-await |
| 54 | + async () => { |
| 55 | + // Mirrors axeConformanceLevel: axe tags best-practice rules with related |
| 56 | + // WCAG criteria, but they're still recommendations, not requirements. |
| 57 | + assertEquals(standardRank(["best-practice", "wcag2a", "wcag111"]), 3); |
| 58 | + }, |
| 59 | +); |
| 60 | + |
| 61 | +unitTest( |
| 62 | + "compareViolations - impact ordering, null impact last", |
| 63 | + // deno-lint-ignore require-await |
| 64 | + async () => { |
| 65 | + const violations = [ |
| 66 | + { id: "r-minor", impact: "minor", tags: ["wcag2a"] }, |
| 67 | + { id: "r-null", impact: null, tags: ["wcag2a"] }, |
| 68 | + { id: "r-critical", impact: "critical", tags: ["wcag2a"] }, |
| 69 | + { id: "r-moderate", impact: "moderate", tags: ["wcag2a"] }, |
| 70 | + { id: "r-serious", impact: "serious", tags: ["wcag2a"] }, |
| 71 | + ]; |
| 72 | + assertEquals( |
| 73 | + violations.sort(compareViolations).map((v) => v.id), |
| 74 | + ["r-critical", "r-serious", "r-moderate", "r-minor", "r-null"], |
| 75 | + ); |
| 76 | + }, |
| 77 | +); |
| 78 | + |
| 79 | +unitTest( |
| 80 | + "compareViolations - standard breaks ties within an impact", |
| 81 | + // deno-lint-ignore require-await |
| 82 | + async () => { |
| 83 | + const violations = [ |
| 84 | + { id: "r-none", impact: "serious", tags: ["cat.color"] }, |
| 85 | + { id: "r-obsolete", impact: "serious", tags: ["wcag2a-obsolete"] }, |
| 86 | + { id: "r-bp", impact: "serious", tags: ["best-practice"] }, |
| 87 | + { id: "r-aa", impact: "serious", tags: ["wcag21aa", "wcag143"] }, |
| 88 | + { id: "r-a", impact: "serious", tags: ["wcag2a", "wcag111"] }, |
| 89 | + ]; |
| 90 | + assertEquals( |
| 91 | + violations.sort(compareViolations).map((v) => v.id), |
| 92 | + ["r-a", "r-aa", "r-bp", "r-obsolete", "r-none"], |
| 93 | + ); |
| 94 | + }, |
| 95 | +); |
| 96 | + |
| 97 | +unitTest( |
| 98 | + "compareViolations - impact outranks standard", |
| 99 | + // deno-lint-ignore require-await |
| 100 | + async () => { |
| 101 | + // A critical best-practice violation precedes a minor WCAG A one. |
| 102 | + const violations = [ |
| 103 | + { id: "r-minor-a", impact: "minor", tags: ["wcag2a"] }, |
| 104 | + { id: "r-critical-bp", impact: "critical", tags: ["best-practice"] }, |
| 105 | + ]; |
| 106 | + assertEquals( |
| 107 | + violations.sort(compareViolations).map((v) => v.id), |
| 108 | + ["r-critical-bp", "r-minor-a"], |
| 109 | + ); |
| 110 | + }, |
| 111 | +); |
| 112 | + |
| 113 | +unitTest( |
| 114 | + "compareViolations - rule id breaks full ties deterministically", |
| 115 | + // deno-lint-ignore require-await |
| 116 | + async () => { |
| 117 | + const violations = [ |
| 118 | + { id: "image-alt", impact: "critical", tags: ["wcag2a"] }, |
| 119 | + { id: "area-alt", impact: "critical", tags: ["wcag2a"] }, |
| 120 | + { id: "button-name", impact: "critical", tags: ["wcag2a"] }, |
| 121 | + ]; |
| 122 | + assertEquals( |
| 123 | + violations.sort(compareViolations).map((v) => v.id), |
| 124 | + ["area-alt", "button-name", "image-alt"], |
| 125 | + ); |
| 126 | + }, |
| 127 | +); |
| 128 | + |
| 129 | +unitTest( |
| 130 | + "compareViolations - sorting a copy leaves the input array untouched", |
| 131 | + // deno-lint-ignore require-await |
| 132 | + async () => { |
| 133 | + // The document reporter sorts a copy so axeResult keeps axe-core's raw |
| 134 | + // order for the json reporter. |
| 135 | + const violations = [ |
| 136 | + { id: "r-minor", impact: "minor", tags: ["wcag2a"] }, |
| 137 | + { id: "r-critical", impact: "critical", tags: ["wcag2a"] }, |
| 138 | + ]; |
| 139 | + const sorted = [...violations].sort(compareViolations); |
| 140 | + assertEquals(sorted.map((v) => v.id), ["r-critical", "r-minor"]); |
| 141 | + assertEquals(violations.map((v) => v.id), ["r-minor", "r-critical"]); |
| 142 | + }, |
| 143 | +); |
0 commit comments