|
1 | 1 | import type { RuleMetadata } from 'axe-core'; |
2 | 2 | import { describe, expect, it } from 'vitest'; |
| 3 | +import type { Group } from '@code-pushup/models'; |
3 | 4 | import { loadAxeRules, transformRulesToGroups } from './transform.js'; |
4 | 5 |
|
5 | 6 | describe('transformRulesToGroups', () => { |
6 | | - describe('wcag21aa preset', () => { |
7 | | - it('should create WCAG 2.1 Level A and AA groups', () => { |
8 | | - const groups = transformRulesToGroups( |
9 | | - loadAxeRules('wcag21aa'), |
10 | | - 'wcag21aa', |
11 | | - ); |
12 | | - |
13 | | - expect(groups.map(({ slug }) => slug)).toEqual([ |
14 | | - 'wcag21-level-a', |
15 | | - 'wcag21-level-aa', |
16 | | - ]); |
17 | | - expect(groups.map(({ title }) => title)).toEqual([ |
18 | | - 'WCAG 2.1 Level A', |
19 | | - 'WCAG 2.1 Level AA', |
20 | | - ]); |
21 | | - }); |
| 7 | + it('should create WCAG 2.1 Level A and AA groups for "wcag21aa" preset', () => { |
| 8 | + const groups = transformRulesToGroups(loadAxeRules('wcag21aa'), 'wcag21aa'); |
22 | 9 |
|
23 | | - it('should have refs in WCAG groups', () => { |
24 | | - transformRulesToGroups(loadAxeRules('wcag21aa'), 'wcag21aa').forEach( |
25 | | - ({ refs }) => { |
26 | | - expect(refs.length).toBeGreaterThan(0); |
27 | | - }, |
28 | | - ); |
| 10 | + expect(groups).toBeArrayOfSize(2); |
| 11 | + expect(groups).toPartiallyContain({ |
| 12 | + slug: 'wcag21-level-a', |
| 13 | + title: 'WCAG 2.1 Level A', |
| 14 | + }); |
| 15 | + expect(groups).toPartiallyContain({ |
| 16 | + slug: 'wcag21-level-aa', |
| 17 | + title: 'WCAG 2.1 Level AA', |
29 | 18 | }); |
30 | 19 | }); |
31 | 20 |
|
32 | | - describe('wcag22aa preset', () => { |
33 | | - it('should create WCAG 2.2 Level A and AA groups', () => { |
34 | | - const groups = transformRulesToGroups( |
35 | | - loadAxeRules('wcag22aa'), |
36 | | - 'wcag22aa', |
37 | | - ); |
38 | | - |
39 | | - expect(groups.map(({ slug }) => slug)).toEqual([ |
40 | | - 'wcag22-level-a', |
41 | | - 'wcag22-level-aa', |
42 | | - ]); |
43 | | - expect(groups.map(({ title }) => title)).toEqual([ |
44 | | - 'WCAG 2.2 Level A', |
45 | | - 'WCAG 2.2 Level AA', |
46 | | - ]); |
47 | | - }); |
| 21 | + it('should populate group refs with audit references for "wcag21aa" preset', () => { |
| 22 | + const groups = transformRulesToGroups(loadAxeRules('wcag21aa'), 'wcag21aa'); |
| 23 | + |
| 24 | + expect(groups[0]!.refs).not.toBeEmpty(); |
| 25 | + expect(groups[1]!.refs).not.toBeEmpty(); |
48 | 26 | }); |
49 | 27 |
|
50 | | - describe('best-practice preset', () => { |
51 | | - it('should create multiple category groups', () => { |
52 | | - expect( |
53 | | - transformRulesToGroups(loadAxeRules('best-practice'), 'best-practice') |
54 | | - .length, |
55 | | - ).toBeGreaterThan(5); |
| 28 | + it('should create WCAG 2.2 Level A and AA groups for "wcag22aa" preset', () => { |
| 29 | + const groups = transformRulesToGroups(loadAxeRules('wcag22aa'), 'wcag22aa'); |
| 30 | + |
| 31 | + expect(groups).toBeArrayOfSize(2); |
| 32 | + expect(groups).toPartiallyContain({ |
| 33 | + slug: 'wcag22-level-a', |
| 34 | + title: 'WCAG 2.2 Level A', |
56 | 35 | }); |
| 36 | + expect(groups).toPartiallyContain({ |
| 37 | + slug: 'wcag22-level-aa', |
| 38 | + title: 'WCAG 2.2 Level AA', |
| 39 | + }); |
| 40 | + }); |
57 | 41 |
|
58 | | - it('should format category titles correctly', () => { |
59 | | - const groups = transformRulesToGroups( |
60 | | - loadAxeRules('best-practice'), |
61 | | - 'best-practice', |
62 | | - ); |
| 42 | + it('should create multiple category groups for "best-practice" preset', () => { |
| 43 | + expect( |
| 44 | + transformRulesToGroups(loadAxeRules('best-practice'), 'best-practice') |
| 45 | + .length, |
| 46 | + ).toBeGreaterThan(5); |
| 47 | + }); |
63 | 48 |
|
64 | | - expect(groups.find(({ slug }) => slug === 'aria')?.title).toBe('ARIA'); |
65 | | - expect(groups.find(({ slug }) => slug === 'name-role-value')?.title).toBe( |
66 | | - 'Names & Labels', |
67 | | - ); |
| 49 | + it('should format category titles using display names', () => { |
| 50 | + const groups = transformRulesToGroups( |
| 51 | + loadAxeRules('best-practice'), |
| 52 | + 'best-practice', |
| 53 | + ); |
| 54 | + |
| 55 | + expect(groups).toPartiallyContain({ slug: 'aria', title: 'ARIA' }); |
| 56 | + expect(groups).toPartiallyContain({ |
| 57 | + slug: 'name-role-value', |
| 58 | + title: 'Names & Labels', |
68 | 59 | }); |
| 60 | + }); |
69 | 61 |
|
70 | | - it('should format unknown category titles with title case', () => { |
71 | | - const groups = transformRulesToGroups( |
72 | | - [{ tags: ['cat.some-new-category', 'best-practice'] } as RuleMetadata], |
73 | | - 'best-practice', |
74 | | - ); |
| 62 | + it('should format unknown category titles with title case', () => { |
| 63 | + const groups = transformRulesToGroups( |
| 64 | + [{ tags: ['cat.some-new-category', 'best-practice'] } as RuleMetadata], |
| 65 | + 'best-practice', |
| 66 | + ); |
75 | 67 |
|
76 | | - expect( |
77 | | - groups.find(({ slug }) => slug === 'some-new-category')?.title, |
78 | | - ).toBe('Some New Category'); |
| 68 | + expect(groups).toPartiallyContain({ |
| 69 | + slug: 'some-new-category', |
| 70 | + title: 'Some New Category', |
79 | 71 | }); |
| 72 | + }); |
80 | 73 |
|
81 | | - it('should remove "cat." prefix from category slugs', () => { |
82 | | - transformRulesToGroups( |
83 | | - loadAxeRules('best-practice'), |
84 | | - 'best-practice', |
85 | | - ).forEach(({ slug }) => { |
86 | | - expect(slug).not.toMatch(/^cat\./); |
87 | | - }); |
88 | | - }); |
| 74 | + it('should remove "cat." prefix from category slugs', () => { |
| 75 | + const groups = transformRulesToGroups( |
| 76 | + loadAxeRules('best-practice'), |
| 77 | + 'best-practice', |
| 78 | + ); |
| 79 | + |
| 80 | + expect(groups).toSatisfyAll<Group>(({ slug }) => !slug.match(/^cat\./)); |
89 | 81 | }); |
90 | 82 |
|
91 | | - describe('all preset', () => { |
92 | | - it('should combine WCAG and category groups', () => { |
93 | | - const groups = transformRulesToGroups(loadAxeRules('all'), 'all'); |
| 83 | + it('should include both WCAG 2.2 and category groups for "all" preset', () => { |
| 84 | + const groups = transformRulesToGroups(loadAxeRules('all'), 'all'); |
94 | 85 |
|
95 | | - expect(groups.filter(({ slug }) => slug.startsWith('wcag'))).toHaveLength( |
96 | | - 2, |
97 | | - ); |
98 | | - expect( |
99 | | - groups.filter(({ slug }) => !slug.startsWith('wcag')).length, |
100 | | - ).toBeGreaterThan(5); |
101 | | - }); |
| 86 | + expect(groups).toPartiallyContain({ slug: 'wcag22-level-a' }); |
| 87 | + expect(groups).toPartiallyContain({ slug: 'wcag22-level-aa' }); |
102 | 88 |
|
103 | | - it('should use WCAG 2.2 for all preset', () => { |
104 | | - const groups = transformRulesToGroups(loadAxeRules('all'), 'all'); |
| 89 | + expect(groups).toSatisfyAny(({ slug }) => !slug.startsWith('wcag')); |
| 90 | + }); |
105 | 91 |
|
106 | | - expect(groups.some(({ slug }) => slug === 'wcag22-level-a')).toBe(true); |
107 | | - expect(groups.some(({ slug }) => slug === 'wcag22-level-aa')).toBe(true); |
108 | | - }); |
| 92 | + it('should assign equal weight to all audit references within groups', () => { |
| 93 | + const groups = transformRulesToGroups(loadAxeRules('wcag21aa'), 'wcag21aa'); |
| 94 | + |
| 95 | + expect(groups).toSatisfyAll<Group>(({ refs }) => |
| 96 | + refs.every(({ weight }) => weight === 1), |
| 97 | + ); |
109 | 98 | }); |
110 | 99 |
|
111 | | - describe('group structure', () => { |
112 | | - it('should have all refs with weight 1', () => { |
113 | | - transformRulesToGroups(loadAxeRules('wcag21aa'), 'wcag21aa').forEach( |
114 | | - ({ refs }) => { |
115 | | - refs.forEach(({ weight }) => { |
116 | | - expect(weight).toBe(1); |
117 | | - }); |
118 | | - }, |
119 | | - ); |
120 | | - }); |
| 100 | + it('should filter out empty groups', () => { |
| 101 | + const groups = transformRulesToGroups(loadAxeRules('all'), 'all'); |
121 | 102 |
|
122 | | - it('should filter out empty groups', () => { |
123 | | - transformRulesToGroups(loadAxeRules('all'), 'all').forEach(({ refs }) => { |
124 | | - expect(refs.length).toBeGreaterThan(0); |
125 | | - }); |
126 | | - }); |
| 103 | + expect(groups).toSatisfyAll<Group>(({ refs }) => refs.length > 0); |
127 | 104 | }); |
128 | 105 | }); |
0 commit comments