|
1 | 1 | const { expect } = require('chai') |
2 | 2 |
|
3 | | -describe('htmlReporter plugin', () => { |
4 | | - describe('escapeHtml function', () => { |
5 | | - // Helper function to simulate the escapeHtml behavior from htmlReporter.js |
6 | | - function escapeHtml(text) { |
7 | | - if (!text) return '' |
8 | | - // Convert non-string values to strings before escaping |
9 | | - if (typeof text !== 'string') { |
10 | | - // Handle arrays by joining with commas |
11 | | - if (Array.isArray(text)) { |
12 | | - text = text.map(item => { |
13 | | - // Recursively flatten nested arrays |
| 3 | +// Helper function to simulate the escapeHtml behavior from htmlReporter.js |
| 4 | +function escapeHtml(text) { |
| 5 | + if (!text) return '' |
| 6 | + // Convert non-string values to strings before escaping |
| 7 | + if (typeof text !== 'string') { |
| 8 | + // Handle arrays by recursively flattening and joining with commas |
| 9 | + if (Array.isArray(text)) { |
| 10 | + // Recursive helper to flatten deeply nested arrays |
| 11 | + const flattenArray = arr => { |
| 12 | + return arr |
| 13 | + .map(item => { |
14 | 14 | if (Array.isArray(item)) { |
15 | | - return item.join(', ') |
| 15 | + return flattenArray(item) |
16 | 16 | } |
17 | 17 | return String(item) |
18 | | - }).join(', ') |
19 | | - } else { |
20 | | - text = String(text) |
21 | | - } |
| 18 | + }) |
| 19 | + .join(', ') |
22 | 20 | } |
23 | | - return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') |
| 21 | + text = flattenArray(text) |
| 22 | + } else { |
| 23 | + text = String(text) |
24 | 24 | } |
| 25 | + } |
| 26 | + return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') |
| 27 | +} |
25 | 28 |
|
| 29 | +describe('htmlReporter plugin', () => { |
| 30 | + describe('escapeHtml function', () => { |
26 | 31 | it('should escape HTML special characters in strings', () => { |
27 | 32 | const result = escapeHtml('<script>alert("xss")</script>') |
28 | 33 | expect(result).to.include('<script>') |
@@ -59,7 +64,7 @@ describe('htmlReporter plugin', () => { |
59 | 64 | it('should handle null and undefined inputs', () => { |
60 | 65 | const resultNull = escapeHtml(null) |
61 | 66 | expect(resultNull).to.equal('') |
62 | | - |
| 67 | + |
63 | 68 | const resultUndefined = escapeHtml(undefined) |
64 | 69 | expect(resultUndefined).to.equal('') |
65 | 70 | }) |
@@ -89,24 +94,6 @@ describe('htmlReporter plugin', () => { |
89 | 94 | }) |
90 | 95 |
|
91 | 96 | describe('generateSystemInfoHtml function', () => { |
92 | | - // Helper function to simulate escapeHtml |
93 | | - function escapeHtml(text) { |
94 | | - if (!text) return '' |
95 | | - if (typeof text !== 'string') { |
96 | | - if (Array.isArray(text)) { |
97 | | - text = text.map(item => { |
98 | | - if (Array.isArray(item)) { |
99 | | - return item.join(', ') |
100 | | - } |
101 | | - return String(item) |
102 | | - }).join(', ') |
103 | | - } else { |
104 | | - text = String(text) |
105 | | - } |
106 | | - } |
107 | | - return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') |
108 | | - } |
109 | | - |
110 | 97 | it('should handle system info with nested arrays', () => { |
111 | 98 | // This tests the real-world scenario from the issue |
112 | 99 | const systemInfo = { |
@@ -137,12 +124,12 @@ describe('htmlReporter plugin', () => { |
137 | 124 | expect(formatValue(systemInfo.osInfo)).to.include('Windows 10') |
138 | 125 | expect(formatValue(systemInfo.cpuInfo)).to.include('12th Gen') |
139 | 126 | expect(formatValue(systemInfo.chromeInfo)).to.include('142.0.7444.163') |
140 | | - |
| 127 | + |
141 | 128 | // The critical test: edgeInfo with nested array should not crash |
142 | 129 | const edgeResult = formatValue(systemInfo.edgeInfo) |
143 | 130 | expect(edgeResult).to.include('Chromium') |
144 | 131 | expect(edgeResult).to.include('140.0.3485.54') |
145 | | - |
| 132 | + |
146 | 133 | expect(formatValue(systemInfo.safariInfo)).to.equal('N/A') |
147 | 134 | }) |
148 | 135 |
|
@@ -180,24 +167,6 @@ describe('htmlReporter plugin', () => { |
180 | 167 | }) |
181 | 168 |
|
182 | 169 | describe('edge cases', () => { |
183 | | - // Helper function to simulate escapeHtml |
184 | | - function escapeHtml(text) { |
185 | | - if (!text) return '' |
186 | | - if (typeof text !== 'string') { |
187 | | - if (Array.isArray(text)) { |
188 | | - text = text.map(item => { |
189 | | - if (Array.isArray(item)) { |
190 | | - return item.join(', ') |
191 | | - } |
192 | | - return String(item) |
193 | | - }).join(', ') |
194 | | - } else { |
195 | | - text = String(text) |
196 | | - } |
197 | | - } |
198 | | - return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') |
199 | | - } |
200 | | - |
201 | 170 | it('should handle arrays with HTML content', () => { |
202 | 171 | const result = escapeHtml(['<script>', ['alert("xss")'], '</script>']) |
203 | 172 | expect(result).to.include('<script>') |
|
0 commit comments