Skip to content

Commit 65d1d4d

Browse files
Set up MUI stubs in test file
1 parent b67d7d3 commit 65d1d4d

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

lib/src/mui_suggestors/system_props_to_sx_migrator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class SystemPropsToSxMigrator extends ComponentUsageMigrator {
111111
final propName = prop.name.name;
112112
if (propName == 'sx') {
113113
existingSxProp = prop;
114-
} else if (_systemPropNames.contains(propName)) {
114+
} else if (systemPropNames.contains(propName)) {
115115
final propElement = prop.staticElement?.nonSynthetic;
116116
final declaringPropsElement = propElement?.enclosingElement;
117117
if (propElement != null && declaringPropsElement != null) {
@@ -363,7 +363,7 @@ const _componentsWithDeprecatedSystemProps = {
363363
'Typography',
364364
};
365365

366-
const _systemPropNames = {
366+
const systemPropNames = {
367367
'm',
368368
'mt',
369369
'mr',

test/mui_suggestors/system_props_to_sx_migrator_test.dart

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import 'dart:convert';
16+
1517
import 'package:over_react_codemod/src/mui_suggestors/system_props_to_sx_migrator.dart';
1618
import 'package:test/test.dart';
1719

@@ -20,13 +22,28 @@ import '../util.dart';
2022

2123
void main() {
2224
group('SystemPropsToSxMigrator', () {
23-
final resolvedContext = SharedAnalysisContext.muiStub;
24-
setUpAll(resolvedContext.warmUpAnalysis);
25+
final resolvedContext = SharedAnalysisContext.overReact;
2526

26-
final testSuggestor = getSuggestorTester(
27-
SystemPropsToSxMigrator(),
28-
resolvedContext: resolvedContext,
29-
);
27+
late String muiUri;
28+
29+
setUpAll(() async {
30+
await resolvedContext.warmUpAnalysis();
31+
32+
// Set up a file with stubbed MUI components with system props, for the
33+
// test inputs to import and the suggestor to detect.
34+
final muiComponentsFile = await resolvedContext.resolvedFileContextForTest(
35+
getStubMuiComponentSource(filenameWithoutExtension: 'mui_components'),
36+
filename: 'mui_components.dart',
37+
);
38+
muiUri = Uri.file(muiComponentsFile.path).toString();
39+
});
40+
41+
String withHeader(String source) => '''
42+
//@dart=2.19
43+
import 'package:over_react/over_react.dart';
44+
import ${jsonEncode(muiUri.toString())};
45+
$source
46+
''';
3047

3148
const sxPrecedenceFixme =
3249
'// FIXME(mui_system_props_migration) - Previously, it was possible for forwarded system props to overwrite these migrated styles, but not anymore since sx takes precedence over any system props.'
@@ -35,6 +52,11 @@ void main() {
3552
const sxMergeFixme =
3653
'// FIXME(mui_system_props_migration) - spread in any sx prop forwarded to this component above, if needed (spread should go at the end of this map to preserve behavior)';
3754

55+
final testSuggestor = getSuggestorTester(
56+
SystemPropsToSxMigrator(),
57+
resolvedContext: resolvedContext,
58+
);
59+
3860
test('migrates single system prop to sx', () async {
3961
await testSuggestor(
4062
input: withHeader('''
@@ -1131,9 +1153,49 @@ void main() {
11311153
});
11321154
}
11331155

1134-
String withHeader(String source) => '''
1135-
import 'package:over_react/over_react.dart';
1136-
import 'package:mui_stub/components.dart';
1137-
1138-
$source
1139-
''';
1156+
String getStubMuiComponentSource({required String filenameWithoutExtension}) {
1157+
const componentsWithSystemProps = [
1158+
'Box',
1159+
'Stack',
1160+
'Grid',
1161+
'Typography',
1162+
];
1163+
return '''
1164+
//@dart=2.19
1165+
import 'package:over_react/over_react.dart';
1166+
import 'package:over_react/js_component.dart';
1167+
1168+
// ignore: uri_has_not_been_generated
1169+
part '$filenameWithoutExtension.over_react.g.dart';
1170+
1171+
${componentsWithSystemProps.map(systemPropsComponentSource).join('\n\n')}
1172+
1173+
UiFactory<TextFieldProps> TextField = uiFunction((_) {}, _\$TextFieldConfig);
1174+
1175+
@Props(keyNamespace: '')
1176+
mixin TextFieldProps on UiProps {
1177+
@convertJsMapProp
1178+
Map? sx;
1179+
1180+
@Deprecated('Deprecated, but not the same as the system props color')
1181+
dynamic color;
1182+
}
1183+
''';
1184+
}
1185+
1186+
String systemPropsComponentSource(String componentName) {
1187+
final systemProps = systemPropNames
1188+
.map((propName) => " @Deprecated('Use sx.') dynamic ${propName};")
1189+
.join('\n');
1190+
return '''
1191+
UiFactory<${componentName}Props> $componentName = uiFunction((_) {}, _\$${componentName}Config);
1192+
1193+
@Props(keyNamespace: '')
1194+
mixin ${componentName}Props on UiProps {
1195+
@convertJsMapProp
1196+
Map? sx;
1197+
1198+
${systemProps}
1199+
}
1200+
''';
1201+
}

test/test_fixtures/over_react_project/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ name: over_react_project
22
environment:
33
sdk: '>=2.11.0 <3.0.0'
44
dependencies:
5-
over_react: ^5.0.0
5+
over_react: ^5.6.0

0 commit comments

Comments
 (0)