1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ import 'dart:convert' ;
16+
1517import 'package:over_react_codemod/src/mui_suggestors/system_props_to_sx_migrator.dart' ;
1618import 'package:test/test.dart' ;
1719
@@ -20,13 +22,28 @@ import '../util.dart';
2022
2123void 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+ }
0 commit comments