|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +// Function to recursively find all .spec.js files |
| 5 | +function findSpecFiles(dir) { |
| 6 | + const files = []; |
| 7 | + const items = fs.readdirSync(dir); |
| 8 | + |
| 9 | + items.forEach((item) => { |
| 10 | + const fullPath = path.join(dir, item); |
| 11 | + const stat = fs.statSync(fullPath); |
| 12 | + |
| 13 | + if (stat.isDirectory()) { |
| 14 | + files.push(...findSpecFiles(fullPath)); |
| 15 | + } else if (item.endsWith('.spec.js')) { |
| 16 | + files.push(fullPath); |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + return files; |
| 21 | +} |
| 22 | + |
| 23 | +// Function to process a single file |
| 24 | +function processFile(filePath) { |
| 25 | + console.log(`Processing: ${filePath}`); |
| 26 | + |
| 27 | + let content = fs.readFileSync(filePath, 'utf8'); |
| 28 | + let modified = false; |
| 29 | + const originalContent = content; |
| 30 | + |
| 31 | + // Pattern 1: wrapper.contains(Component) -> wrapper.findComponent(Component).exists() |
| 32 | + // This handles Vue component references |
| 33 | + content = content.replace( |
| 34 | + /(\w+)\.contains\(([A-Z][a-zA-Z0-9_]*)\)/g, |
| 35 | + '$1.findComponent($2).exists()', |
| 36 | + ); |
| 37 | + |
| 38 | + // Pattern 2: wrapper.contains('selector') -> wrapper.find('selector').exists() |
| 39 | + // This handles CSS selectors and HTML tags |
| 40 | + content = content.replace( |
| 41 | + /(\w+)\.contains\((['"`][^'"`]+['"`])\)/g, |
| 42 | + '$1.find($2).exists()', |
| 43 | + ); |
| 44 | + |
| 45 | + // Pattern 3: wrapper.contains(variable) where variable is likely a selector string |
| 46 | + // This is trickier - we need to handle cases where the argument is a variable |
| 47 | + // that contains a selector string (not a component) |
| 48 | + content = content.replace( |
| 49 | + /(\w+)\.contains\(([a-z][a-zA-Z0-9_]*)\)/g, |
| 50 | + (match, wrapper, variable) => { |
| 51 | + // If the variable name suggests it's a component (starts with uppercase), |
| 52 | + // use findComponent, otherwise use find |
| 53 | + if (variable.match(/^[A-Z]/)) { |
| 54 | + return `${wrapper}.findComponent(${variable}).exists()`; |
| 55 | + } |
| 56 | + return `${wrapper}.find(${variable}).exists()`; |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + // Special case: Handle chained contains calls like wrapper.findComponent('.foo').contains(Bar) |
| 61 | + content = content.replace( |
| 62 | + /(\w+\.findComponent\([^)]+\))\.contains\(([A-Z][a-zA-Z0-9_]*)\)/g, |
| 63 | + '$1.findComponent($2).exists()', |
| 64 | + ); |
| 65 | + |
| 66 | + content = content.replace( |
| 67 | + /(\w+\.findComponent\([^)]+\))\.contains\((['"`][^'"`]+['"`])\)/g, |
| 68 | + '$1.find($2).exists()', |
| 69 | + ); |
| 70 | + |
| 71 | + // Special case: Handle DOM element contains (like document.activeElement.contains) |
| 72 | + // These should NOT be changed as they're native DOM methods |
| 73 | + content = content.replace( |
| 74 | + /document\.activeElement\.findComponent\(([^)]+)\)\.exists\(\)/g, |
| 75 | + 'document.activeElement.contains($1)', |
| 76 | + ); |
| 77 | + |
| 78 | + content = content.replace( |
| 79 | + /document\.activeElement\.find\(([^)]+)\)\.exists\(\)/g, |
| 80 | + 'document.activeElement.contains($1)', |
| 81 | + ); |
| 82 | + |
| 83 | + // Handle classList.contains - this should not be changed |
| 84 | + content = content.replace( |
| 85 | + /classList\.find\(([^)]+)\)\.exists\(\)/g, |
| 86 | + 'classList.contains($1)', |
| 87 | + ); |
| 88 | + |
| 89 | + // Handle other native DOM contains methods that got incorrectly replaced |
| 90 | + content = content.replace( |
| 91 | + /focusedElement\.find\(([^)]+)\)\.exists\(\)/g, |
| 92 | + 'focusedElement.contains($1)', |
| 93 | + ); |
| 94 | + |
| 95 | + // Pattern for expect().toBe(true/false) - these are boolean assertions |
| 96 | + // We need to handle the boolean return value correctly |
| 97 | + |
| 98 | + if (content !== originalContent) { |
| 99 | + modified = true; |
| 100 | + console.log(' - Replaced .contains() with .find().exists() or .findComponent().exists()'); |
| 101 | + } |
| 102 | + |
| 103 | + // Write back the modified content |
| 104 | + if (modified) { |
| 105 | + fs.writeFileSync(filePath, content, 'utf8'); |
| 106 | + console.log(' ✓ File updated'); |
| 107 | + } else { |
| 108 | + console.log(' - No changes needed'); |
| 109 | + } |
| 110 | + |
| 111 | + return modified; |
| 112 | +} |
| 113 | + |
| 114 | +// Function to validate the changes by checking for common issues |
| 115 | +function validateFile(filePath) { |
| 116 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 117 | + const issues = []; |
| 118 | + |
| 119 | + // Check for any remaining .contains() that might have been missed |
| 120 | + const remainingContains = content.match(/\w+\.contains\(/g); |
| 121 | + if (remainingContains) { |
| 122 | + const validContains = remainingContains.filter(match => match.includes('classList.contains(') |
| 123 | + || match.includes('activeElement.contains(') |
| 124 | + || match.includes('focusedElement.contains(')); |
| 125 | + |
| 126 | + if (remainingContains.length !== validContains.length) { |
| 127 | + issues.push(`Still has ${remainingContains.length - validContains.length} .contains() calls that might need fixing`); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Check for potential syntax issues |
| 132 | + if (content.includes('.find().exists()') || content.includes('.findComponent().exists()')) { |
| 133 | + issues.push('Found empty .find() or .findComponent() calls'); |
| 134 | + } |
| 135 | + |
| 136 | + return issues; |
| 137 | +} |
| 138 | + |
| 139 | +// Main execution |
| 140 | +function main() { |
| 141 | + const testsDir = path.join(__dirname, 'tests', 'unit'); |
| 142 | + |
| 143 | + if (!fs.existsSync(testsDir)) { |
| 144 | + console.error('Tests directory not found:', testsDir); |
| 145 | + process.exit(1); |
| 146 | + } |
| 147 | + |
| 148 | + console.log('Finding all .spec.js files...'); |
| 149 | + const specFiles = findSpecFiles(testsDir); |
| 150 | + |
| 151 | + console.log(`Found ${specFiles.length} spec files`); |
| 152 | + console.log(''); |
| 153 | + |
| 154 | + let totalModified = 0; |
| 155 | + let totalIssues = 0; |
| 156 | + |
| 157 | + specFiles.forEach((file) => { |
| 158 | + if (processFile(file)) { |
| 159 | + totalModified++; |
| 160 | + } |
| 161 | + |
| 162 | + // Validate the changes |
| 163 | + const issues = validateFile(file); |
| 164 | + if (issues.length > 0) { |
| 165 | + console.log(' ⚠️ Validation issues:'); |
| 166 | + issues.forEach(issue => console.log(` - ${issue}`)); |
| 167 | + totalIssues++; |
| 168 | + } |
| 169 | + |
| 170 | + console.log(''); |
| 171 | + }); |
| 172 | + |
| 173 | + console.log(`Completed! Modified ${totalModified} out of ${specFiles.length} files.`); |
| 174 | + if (totalIssues > 0) { |
| 175 | + console.log(`⚠️ ${totalIssues} files have validation issues that may need manual review.`); |
| 176 | + } |
| 177 | + console.log(''); |
| 178 | + console.log('Summary of replacements:'); |
| 179 | + console.log('- wrapper.contains(Component) → wrapper.findComponent(Component).exists()'); |
| 180 | + console.log('- wrapper.contains("selector") → wrapper.find("selector").exists()'); |
| 181 | + console.log('- Preserved native DOM .contains() methods (classList, activeElement, etc.)'); |
| 182 | + console.log('- Handled chained calls like wrapper.findComponent().contains()'); |
| 183 | +} |
| 184 | + |
| 185 | +// Run the script |
| 186 | +main(); |
0 commit comments