Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

export.test.ts

209 lines6.0 KB

1import { describe, expect, it } from 'vitest'
2import {
3 exportToCsv,
4 exportToJson,
5 generateSummaryReport,
6} from '../src/core/utils/export-audit.uitls'
7​
8// types
9import type { A11yAuditResult } from '../src/core/types/types'
10​
11const createMockResult = (): A11yAuditResult => ({
12 issues: [
13 {
14 id: 'issue-1',
15 ruleId: 'image-alt',
16 impact: 'critical',
17 message: 'Images must have alternate text',
18 help: 'Images must have alternate text',
19 helpUrl: 'https://dequeuniversity.com/rules/axe/4.0/image-alt',
20 wcagTags: ['wcag2a', 'wcag111'],
21 nodes: [
22 {
23 selector: 'img.logo',
24 html: '<img class="logo" src="logo.png">',
25 failureSummary: 'Fix this issue by adding an alt attribute',
26 },
27 ],
28 meetsThreshold: true,
29 timestamp: 1704067200000,
30 },
31 {
32 id: 'issue-2',
33 ruleId: 'button-name',
34 impact: 'serious',
35 message: 'Buttons must have discernible text',
36 help: 'Buttons must have discernible text',
37 helpUrl: 'https://dequeuniversity.com/rules/axe/4.0/button-name',
38 wcagTags: ['wcag2a', 'wcag412'],
39 nodes: [
40 {
41 selector: 'button.submit',
42 html: '<button class="submit"></button>',
43 failureSummary: 'Add text or aria-label to the button',
44 },
45 {
46 selector: 'button.cancel',
47 html: '<button class="cancel"></button>',
48 failureSummary: 'Add text or aria-label to the button',
49 },
50 ],
51 meetsThreshold: true,
52 timestamp: 1704067200000,
53 },
54 ],
55 summary: {
56 total: 2,
57 critical: 1,
58 serious: 1,
59 moderate: 0,
60 minor: 0,
61 passes: 50,
62 incomplete: 3,
63 },
64 timestamp: 1704067200000,
65 url: 'http://localhost:3000/',
66 context: 'document',
67 duration: 123.45,
68})
69​
70describe('export', () => {
71 describe('exportToJson', () => {
72 it('should export audit result to JSON format', () => {
73 const result = createMockResult()
74 const json = exportToJson(result)
75​
76 const parsed = JSON.parse(json)
77 expect(parsed.meta).toBeDefined()
78 expect(parsed.meta.url).toBe('http://localhost:3000/')
79 expect(parsed.meta.context).toBe('document')
80 expect(parsed.summary).toBeDefined()
81 expect(parsed.summary.total).toBe(2)
82 expect(parsed.issues).toHaveLength(2)
83 })
84​
85 it('should include all issue details', () => {
86 const result = createMockResult()
87 const json = exportToJson(result)
88​
89 const parsed = JSON.parse(json)
90 const firstIssue = parsed.issues[0]
91​
92 expect(firstIssue.ruleId).toBe('image-alt')
93 expect(firstIssue.impact).toBe('critical')
94 expect(firstIssue.helpUrl).toContain('dequeuniversity.com')
95 expect(firstIssue.nodes).toHaveLength(1)
96 })
97​
98 it('should include node details', () => {
99 const result = createMockResult()
100 const json = exportToJson(result)
101​
102 const parsed = JSON.parse(json)
103 const node = parsed.issues[0].nodes[0]
104​
105 expect(node.selector).toBe('img.logo')
106 expect(node.html).toContain('<img')
107 expect(node.failureSummary).toBeDefined()
108 })
109 })
110​
111 describe('exportToCsv', () => {
112 it('should export audit result to CSV format', () => {
113 const result = createMockResult()
114 const csv = exportToCsv(result)
115​
116 expect(csv).toContain('Rule ID')
117 expect(csv).toContain('Impact')
118 expect(csv).toContain('Message')
119 expect(csv).toContain('Help URL')
120 expect(csv).toContain('WCAG Tags')
121 expect(csv).toContain('Selector')
122 expect(csv).toContain('HTML')
123 })
124​
125 it('should include one row per affected node', () => {
126 const result = createMockResult()
127 const csv = exportToCsv(result)
128​
129 const lines = csv.split('\n')
130 // Header + 3 nodes (1 from issue 1 + 2 from issue 2)
131 expect(lines).toHaveLength(4)
132 })
133​
134 it('should escape quotes in content', () => {
135 const result = createMockResult()
136 const firstIssue = result.issues[0]
137 if (firstIssue) {
138 firstIssue.message = 'Message with "quotes" inside'
139 }
140 const csv = exportToCsv(result)
141​
142 expect(csv).toContain('""quotes""')
143 })
144​
145 it('should join WCAG tags with semicolons', () => {
146 const result = createMockResult()
147 const csv = exportToCsv(result)
148​
149 expect(csv).toContain('wcag2a; wcag111')
150 })
151 })
152​
153 describe('generateSummaryReport', () => {
154 it('should generate a human-readable summary', () => {
155 const result = createMockResult()
156 const report = generateSummaryReport(result)
157​
158 expect(report).toContain('ACCESSIBILITY AUDIT REPORT')
159 expect(report).toContain('URL: http://localhost:3000/')
160 expect(report).toContain('Total Issues: 2')
161 expect(report).toContain('Critical: 1')
162 expect(report).toContain('Serious: 1')
163 expect(report).toContain('Passing Rules: 50')
164 })
165​
166 it('should group issues by impact', () => {
167 const result = createMockResult()
168 const report = generateSummaryReport(result)
169​
170 expect(report).toContain('[CRITICAL]')
171 expect(report).toContain('[SERIOUS]')
172 expect(report).toContain('image-alt')
173 expect(report).toContain('button-name')
174 })
175​
176 it('should include selector and help URL', () => {
177 const result = createMockResult()
178 const report = generateSummaryReport(result)
179​
180 expect(report).toContain('Selector: img.logo')
181 expect(report).toContain('Learn more: https://dequeuniversity.com')
182 })
183​
184 it('should handle result with no issues', () => {
185 const result: A11yAuditResult = {
186 issues: [],
187 summary: {
188 total: 0,
189 critical: 0,
190 serious: 0,
191 moderate: 0,
192 minor: 0,
193 passes: 50,
194 incomplete: 0,
195 },
196 timestamp: 1704067200000,
197 url: 'http://localhost:3000/',
198 context: 'document',
199 duration: 50,
200 }
201​
202 const report = generateSummaryReport(result)
203​
204 expect(report).toContain('Total Issues: 0')
205 expect(report).not.toContain('[CRITICAL]')
206 expect(report).not.toContain('[SERIOUS]')
207 })
208 })
209})