|
14 | 14 |
|
15 | 15 | import 'dart:convert'; |
16 | 16 |
|
| 17 | +import 'package:analyzer/dart/analysis/results.dart'; |
| 18 | +import 'package:analyzer/dart/element/element.dart'; |
| 19 | +import 'package:collection/collection.dart'; |
17 | 20 | import 'package:over_react_codemod/src/mui_suggestors/system_props_to_sx_migrator.dart'; |
18 | 21 | import 'package:test/test.dart'; |
19 | 22 |
|
@@ -57,6 +60,36 @@ void main() { |
57 | 60 | resolvedContext: resolvedContext, |
58 | 61 | ); |
59 | 62 |
|
| 63 | + group('hasSxAndSomeSystemProps returns as expected for test props:', () { |
| 64 | + late ResolvedUnitResult unit; |
| 65 | + |
| 66 | + setUpAll(() async { |
| 67 | + final file = |
| 68 | + await resolvedContext.resolvedFileContextForTest(withHeader('')); |
| 69 | + unit = (await file.getResolvedUnit())!; |
| 70 | + }); |
| 71 | + |
| 72 | + InterfaceElement getProps(String propsName) => |
| 73 | + getImportedInterfaceElement(unit, propsName); |
| 74 | + |
| 75 | + test('with system props', () async { |
| 76 | + expect( |
| 77 | + hasSxAndSomeSystemProps(getProps('BoxProps')), |
| 78 | + isTrue, |
| 79 | + ); |
| 80 | + expect(hasSxAndSomeSystemProps(getProps('GridProps')), isTrue); |
| 81 | + expect(hasSxAndSomeSystemProps(getProps('StackProps')), isTrue); |
| 82 | + expect(hasSxAndSomeSystemProps(getProps('TypographyProps')), isTrue); |
| 83 | + }); |
| 84 | + |
| 85 | + test('without system props', () async { |
| 86 | + // Test props with sx and a prop named like a system prop. |
| 87 | + expect(hasSxAndSomeSystemProps(getProps('TextFieldProps')), isFalse); |
| 88 | + // Some other props from over_react. |
| 89 | + expect(hasSxAndSomeSystemProps(getProps('DomProps')), isFalse); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
60 | 93 | test('migrates single system prop to sx', () async { |
61 | 94 | await testSuggestor( |
62 | 95 | input: withHeader(''' |
@@ -1156,8 +1189,8 @@ void main() { |
1156 | 1189 | String getStubMuiLibrarySource({required String filenameWithoutExtension}) { |
1157 | 1190 | final systemPropComponentsSource = [ |
1158 | 1191 | 'Box', |
1159 | | - 'Stack', |
1160 | 1192 | 'Grid', |
| 1193 | + 'Stack', |
1161 | 1194 | 'Typography', |
1162 | 1195 | ].map((componentName) { |
1163 | 1196 | return ''' |
@@ -1195,3 +1228,17 @@ String getStubMuiLibrarySource({required String filenameWithoutExtension}) { |
1195 | 1228 | } |
1196 | 1229 | '''; |
1197 | 1230 | } |
| 1231 | + |
| 1232 | +// Borrowed from https://github.com/Workiva/over_react/blob/5.6.0/tools/analyzer_plugin/test/unit/util/prop_declaration/util.dart#L73-L78 |
| 1233 | + |
| 1234 | +InterfaceElement getInterfaceElement(ResolvedUnitResult result, String name) => |
| 1235 | + result.libraryElement.topLevelElements |
| 1236 | + .whereType<InterfaceElement>() |
| 1237 | + .singleWhere((e) => e.name == name); |
| 1238 | + |
| 1239 | +InterfaceElement getImportedInterfaceElement( |
| 1240 | + ResolvedUnitResult result, String name) => |
| 1241 | + result.libraryElement.importedLibraries |
| 1242 | + .map((l) => l.exportNamespace.get(name)) |
| 1243 | + .whereNotNull() |
| 1244 | + .single as InterfaceElement; |
0 commit comments