@openrewrite/recipes-react 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/dist/index.d.ts.map +1 -1
  2. package/dist/index.js +1 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/migration/cra-to-vite/add-vite-config.d.ts +7 -0
  5. package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -1
  6. package/dist/migration/cra-to-vite/add-vite-config.js +58 -1
  7. package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -1
  8. package/dist/migration/cra-to-vite/cra-file-gate.d.ts +3 -0
  9. package/dist/migration/cra-to-vite/cra-file-gate.d.ts.map +1 -0
  10. package/dist/migration/cra-to-vite/cra-file-gate.js +10 -0
  11. package/dist/migration/cra-to-vite/cra-file-gate.js.map +1 -0
  12. package/dist/migration/cra-to-vite/cra-runtime.d.ts +2 -0
  13. package/dist/migration/cra-to-vite/cra-runtime.d.ts.map +1 -0
  14. package/dist/migration/cra-to-vite/cra-runtime.js +24 -0
  15. package/dist/migration/cra-to-vite/cra-runtime.js.map +1 -0
  16. package/dist/migration/cra-to-vite/cra-script.d.ts +8 -0
  17. package/dist/migration/cra-to-vite/cra-script.d.ts.map +1 -0
  18. package/dist/migration/cra-to-vite/cra-script.js +82 -0
  19. package/dist/migration/cra-to-vite/cra-script.js.map +1 -0
  20. package/dist/migration/cra-to-vite/cra-to-vite.d.ts +2 -1
  21. package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -1
  22. package/dist/migration/cra-to-vite/cra-to-vite.js +23 -16
  23. package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -1
  24. package/dist/migration/cra-to-vite/cra-typecheck.d.ts +4 -0
  25. package/dist/migration/cra-to-vite/cra-typecheck.d.ts.map +1 -0
  26. package/dist/migration/cra-to-vite/cra-typecheck.js +35 -0
  27. package/dist/migration/cra-to-vite/cra-typecheck.js.map +1 -0
  28. package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
  29. package/dist/migration/cra-to-vite/find-cra-config-blockers.js +2 -1
  30. package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
  31. package/dist/migration/cra-to-vite/index.d.ts +8 -2
  32. package/dist/migration/cra-to-vite/index.d.ts.map +1 -1
  33. package/dist/migration/cra-to-vite/index.js +17 -2
  34. package/dist/migration/cra-to-vite/index.js.map +1 -1
  35. package/dist/migration/cra-to-vite/migrate-cra-package.d.ts +17 -0
  36. package/dist/migration/cra-to-vite/migrate-cra-package.d.ts.map +1 -0
  37. package/dist/migration/cra-to-vite/migrate-cra-package.js +181 -0
  38. package/dist/migration/cra-to-vite/migrate-cra-package.js.map +1 -0
  39. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts +9 -0
  40. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts.map +1 -0
  41. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js +66 -0
  42. package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js.map +1 -0
  43. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts +49 -0
  44. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts.map +1 -0
  45. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js +506 -0
  46. package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js.map +1 -0
  47. package/dist/migration/cra-to-vite/move-public-index-html.d.ts +2 -0
  48. package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -1
  49. package/dist/migration/cra-to-vite/move-public-index-html.js +12 -7
  50. package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -1
  51. package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts +30 -0
  52. package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts.map +1 -0
  53. package/dist/migration/cra-to-vite/plan-cra-to-vite.js +428 -0
  54. package/dist/migration/cra-to-vite/plan-cra-to-vite.js.map +1 -0
  55. package/dist/migration/cra-to-vite/prepare-cra-package.d.ts +15 -0
  56. package/dist/migration/cra-to-vite/prepare-cra-package.d.ts.map +1 -0
  57. package/dist/migration/cra-to-vite/prepare-cra-package.js +100 -0
  58. package/dist/migration/cra-to-vite/prepare-cra-package.js.map +1 -0
  59. package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts +12 -0
  60. package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts.map +1 -0
  61. package/dist/migration/cra-to-vite/remove-cra-configuration.js +85 -0
  62. package/dist/migration/cra-to-vite/remove-cra-configuration.js.map +1 -0
  63. package/dist/migration/cra-to-vite/rename-jsx-files.d.ts +20 -0
  64. package/dist/migration/cra-to-vite/rename-jsx-files.d.ts.map +1 -0
  65. package/dist/migration/cra-to-vite/rename-jsx-files.js +220 -0
  66. package/dist/migration/cra-to-vite/rename-jsx-files.js.map +1 -0
  67. package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -1
  68. package/dist/migration/cra-to-vite/rename-process-env-react-app.js +6 -3
  69. package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -1
  70. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +1 -1
  71. package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -1
  72. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +11 -4
  73. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
  74. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +47 -30
  75. package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
  76. package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts +3 -0
  77. package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts.map +1 -0
  78. package/dist/migration/cra-to-vite/resolved-cra-dependency.js +42 -0
  79. package/dist/migration/cra-to-vite/resolved-cra-dependency.js.map +1 -0
  80. package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +13 -0
  81. package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -1
  82. package/dist/migration/cra-to-vite/scan-cra-projects.js +52 -4
  83. package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -1
  84. package/dist/migration/cra-to-vite/shared.d.ts +1 -1
  85. package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
  86. package/dist/migration/cra-to-vite/shared.js +19 -4
  87. package/dist/migration/cra-to-vite/shared.js.map +1 -1
  88. package/dist/migration/cra-to-vite/svg-component.d.ts +5 -0
  89. package/dist/migration/cra-to-vite/svg-component.d.ts.map +1 -0
  90. package/dist/migration/cra-to-vite/svg-component.js +60 -0
  91. package/dist/migration/cra-to-vite/svg-component.js.map +1 -0
  92. package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
  93. package/dist/migration/cra-to-vite/update-react-app-env-types.js +8 -2
  94. package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
  95. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +3 -2
  96. package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
  97. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +18 -14
  98. package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
  99. package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
  100. package/dist/react18/replace-reactdom-render.js +16 -8
  101. package/dist/react18/replace-reactdom-render.js.map +1 -1
  102. package/dist/testing/enzyme-constant-strings.d.ts +12 -0
  103. package/dist/testing/enzyme-constant-strings.d.ts.map +1 -0
  104. package/dist/testing/enzyme-constant-strings.js +101 -0
  105. package/dist/testing/enzyme-constant-strings.js.map +1 -0
  106. package/dist/testing/enzyme-host-components.d.ts.map +1 -1
  107. package/dist/testing/enzyme-host-components.js +5 -2
  108. package/dist/testing/enzyme-host-components.js.map +1 -1
  109. package/dist/testing/enzyme-portals.d.ts +38 -0
  110. package/dist/testing/enzyme-portals.d.ts.map +1 -0
  111. package/dist/testing/enzyme-portals.js +226 -0
  112. package/dist/testing/enzyme-portals.js.map +1 -0
  113. package/dist/testing/enzyme-selector-names.d.ts +9 -0
  114. package/dist/testing/enzyme-selector-names.d.ts.map +1 -0
  115. package/dist/testing/enzyme-selector-names.js +50 -0
  116. package/dist/testing/enzyme-selector-names.js.map +1 -0
  117. package/dist/testing/enzyme-to-react-testing-library.d.ts +71 -8
  118. package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
  119. package/dist/testing/enzyme-to-react-testing-library.js +321 -34
  120. package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
  121. package/dist/testing/enzyme-usage.d.ts.map +1 -1
  122. package/dist/testing/enzyme-usage.js +27 -4
  123. package/dist/testing/enzyme-usage.js.map +1 -1
  124. package/dist/testing/enzyme-wrapper-scope.d.ts +12 -6
  125. package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
  126. package/dist/testing/enzyme-wrapper-scope.js +140 -16
  127. package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
  128. package/dist/util/java-recipe-proxy.d.ts +16 -0
  129. package/dist/util/java-recipe-proxy.d.ts.map +1 -0
  130. package/dist/util/java-recipe-proxy.js +85 -0
  131. package/dist/util/java-recipe-proxy.js.map +1 -0
  132. package/dist/util/module-imports.d.ts +1 -0
  133. package/dist/util/module-imports.d.ts.map +1 -1
  134. package/dist/util/module-imports.js +5 -1
  135. package/dist/util/module-imports.js.map +1 -1
  136. package/package.json +2 -2
  137. package/src/index.ts +2 -1
  138. package/src/migration/cra-to-vite/add-vite-config.ts +53 -1
  139. package/src/migration/cra-to-vite/cra-file-gate.ts +8 -0
  140. package/src/migration/cra-to-vite/cra-runtime.ts +16 -0
  141. package/src/migration/cra-to-vite/cra-script.ts +61 -0
  142. package/src/migration/cra-to-vite/cra-to-vite.ts +25 -36
  143. package/src/migration/cra-to-vite/cra-typecheck.ts +33 -0
  144. package/src/migration/cra-to-vite/find-cra-config-blockers.ts +2 -1
  145. package/src/migration/cra-to-vite/index.ts +10 -2
  146. package/src/migration/cra-to-vite/migrate-cra-package.ts +127 -0
  147. package/src/migration/cra-to-vite/migrate-cra-svg-imports.ts +41 -0
  148. package/src/migration/cra-to-vite/migrate-cra-tests-to-vitest.ts +376 -0
  149. package/src/migration/cra-to-vite/move-public-index-html.ts +12 -7
  150. package/src/migration/cra-to-vite/plan-cra-to-vite.ts +283 -0
  151. package/src/migration/cra-to-vite/prepare-cra-package.ts +86 -0
  152. package/src/migration/cra-to-vite/remove-cra-configuration.ts +64 -0
  153. package/src/migration/cra-to-vite/rename-jsx-files.ts +155 -0
  154. package/src/migration/cra-to-vite/rename-process-env-react-app.ts +6 -3
  155. package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +1 -1
  156. package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +52 -33
  157. package/src/migration/cra-to-vite/resolved-cra-dependency.ts +41 -0
  158. package/src/migration/cra-to-vite/scan-cra-projects.ts +57 -6
  159. package/src/migration/cra-to-vite/shared.ts +20 -10
  160. package/src/migration/cra-to-vite/svg-component.ts +39 -0
  161. package/src/migration/cra-to-vite/update-react-app-env-types.ts +11 -4
  162. package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +22 -28
  163. package/src/react18/replace-reactdom-render.ts +17 -8
  164. package/src/testing/enzyme-constant-strings.ts +95 -0
  165. package/src/testing/enzyme-host-components.ts +6 -2
  166. package/src/testing/enzyme-portals.ts +279 -0
  167. package/src/testing/enzyme-selector-names.ts +59 -0
  168. package/src/testing/enzyme-to-react-testing-library.ts +425 -50
  169. package/src/testing/enzyme-usage.ts +29 -4
  170. package/src/testing/enzyme-wrapper-scope.ts +181 -30
  171. package/src/util/java-recipe-proxy.ts +81 -0
  172. package/src/util/module-imports.ts +6 -1
@@ -15,9 +15,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15
15
  });
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.EnzymeToReactTestingLibrary = void 0;
18
+ exports.EnzymeSnapshotsToReactTestingLibrary = exports.EnzymeToReactTestingLibrary = exports.AddTestingLibraryDom18 = exports.AddTestingLibraryReact18 = exports.AddTestingLibraryReact = void 0;
19
+ exports.lockfileScopes = lockfileScopes;
20
+ exports.rtlLanded = rtlLanded;
19
21
  exports.supportsResolvingGate = supportsResolvingGate;
20
22
  exports.rewriteVersion = rewriteVersion;
23
+ exports.injectsRootStyles = injectsRootStyles;
21
24
  exports.reactMajor = reactMajor;
22
25
  exports.simulatesEvents = simulatesEvents;
23
26
  const rewrite_1 = require("@openrewrite/rewrite");
@@ -29,15 +32,152 @@ const fs_1 = require("fs");
29
32
  const path_1 = require("path");
30
33
  const semver_1 = require("semver");
31
34
  const find_enzyme_usage_1 = require("../search/find-enzyme-usage");
35
+ const java_recipe_proxy_1 = require("../util/java-recipe-proxy");
36
+ const module_imports_1 = require("../util/module-imports");
32
37
  const enzyme_usage_1 = require("./enzyme-usage");
33
38
  const enzyme_host_components_1 = require("./enzyme-host-components");
39
+ const enzyme_portals_1 = require("./enzyme-portals");
40
+ const enzyme_constant_strings_1 = require("./enzyme-constant-strings");
41
+ const enzyme_selector_names_1 = require("./enzyme-selector-names");
34
42
  const enzyme_wrapper_scope_1 = require("./enzyme-wrapper-scope");
35
43
  const RTL = "@testing-library/react";
36
44
  const REACT_RANGE = ">=16.9.0 <18";
45
+ const REACT_18_RANGE = ">=18.0.0 <19";
46
+ const resolvingReact = (range) => [`react@${range}`, `react-dom@${range}`];
37
47
  const RTL_ADDITION = {
38
48
  packageName: RTL, version: "^12.1.5", scope: "devDependencies",
39
- onlyIfDeclaring: ["enzyme"], onlyIfResolving: [`react@${REACT_RANGE}`, `react-dom@${REACT_RANGE}`]
49
+ onlyIfDeclaring: ["enzyme"], onlyIfResolving: resolvingReact(REACT_RANGE)
40
50
  };
51
+ const RTL_18_ADDITION = Object.assign(Object.assign({}, RTL_ADDITION), { version: "^16.3.0", onlyIfResolving: resolvingReact(REACT_18_RANGE) });
52
+ const DOM = "@testing-library/dom";
53
+ const DOM_18_ADDITION = Object.assign(Object.assign({}, RTL_18_ADDITION), { packageName: DOM, version: "^10.4.0" });
54
+ function packageRoot(manifestPath) {
55
+ return manifestPath.slice(0, -"package.json".length);
56
+ }
57
+ function lockfileScopes(manifests) {
58
+ var _a, _b;
59
+ const scopes = new Map();
60
+ for (const manifest of manifests) {
61
+ const root = packageRoot(manifest.sourcePath);
62
+ if (!scopes.has(manifest.sourcePath))
63
+ scopes.set(manifest.sourcePath, root);
64
+ for (const member of (_b = (_a = (0, javascript_1.findNodeResolutionResult)(manifest)) === null || _a === void 0 ? void 0 : _a.workspacePackagePaths) !== null && _b !== void 0 ? _b : []) {
65
+ scopes.set(member.replace(/^\.\//, ""), root);
66
+ }
67
+ }
68
+ return scopes;
69
+ }
70
+ class EnzymeTestDependency extends java_recipe_proxy_1.JavaDependencyRecipe {
71
+ constructor(addition, forecast) {
72
+ super("org.openrewrite.javascript.AddDependency", addition);
73
+ this.addition = addition;
74
+ this.forecast = forecast;
75
+ this.landed = new Set();
76
+ this.manifests = [];
77
+ this.held = [];
78
+ }
79
+ initialValue() {
80
+ this.landed.clear();
81
+ this.manifests.length = 0;
82
+ this.held.length = 0;
83
+ return super.initialValue();
84
+ }
85
+ candidates() {
86
+ return new Set(this.manifests.map(manifest => packageRoot(manifest.sourcePath)));
87
+ }
88
+ scanned(manifest) {
89
+ if (manifest.kind !== json_1.Json.Kind.Document || !/(^|\/)package\.json$/.test(manifest.sourcePath))
90
+ return true;
91
+ if (!this.admits(manifest))
92
+ return false;
93
+ if (this.addition.onlyIfDeclaring.some(name => declares(manifest, name)))
94
+ this.manifests.push(manifest);
95
+ this.held.push(manifest);
96
+ return false;
97
+ }
98
+ admits(_manifest) {
99
+ return true;
100
+ }
101
+ generate(acc, ctx) {
102
+ const _super = Object.create(null, {
103
+ generate: { get: () => super.generate }
104
+ });
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ var _a, _b;
107
+ const converting = yield ((_b = (_a = this.forecast).converting) === null || _b === void 0 ? void 0 : _b.call(_a));
108
+ const scopes = lockfileScopes(this.held);
109
+ const forwarded = new Set(this.held.filter(manifest => {
110
+ var _a;
111
+ const scope = (_a = scopes.get(manifest.sourcePath)) !== null && _a !== void 0 ? _a : packageRoot(manifest.sourcePath);
112
+ return !converting || [...converting].some(source => source.startsWith(scope));
113
+ }));
114
+ const scanner = yield this.java.scanner(acc);
115
+ for (const manifest of forwarded)
116
+ yield scanner.visit(manifest, ctx);
117
+ const kept = this.manifests.filter(manifest => forwarded.has(manifest));
118
+ this.manifests.length = 0;
119
+ this.manifests.push(...kept);
120
+ const generated = yield _super.generate.call(this, acc, ctx);
121
+ const editor = yield this.java.editorWithData(acc);
122
+ for (const manifest of this.manifests) {
123
+ const edited = yield editor.visit(manifest, ctx);
124
+ if (edited && edited !== manifest && declares(edited, this.addition.packageName))
125
+ this.landed.add(manifest.sourcePath.slice(0, -"package.json".length));
126
+ }
127
+ return generated;
128
+ });
129
+ }
130
+ }
131
+ class AddTestingLibraryReact extends EnzymeTestDependency {
132
+ constructor(forecast = {}) {
133
+ super(RTL_ADDITION, forecast);
134
+ this.name = "org.openrewrite.react.migrate.add-testing-library-react";
135
+ this.displayName = "Add `@testing-library/react` where Enzyme tests are migrated";
136
+ this.description = "Adds `@testing-library/react` 12 as a dev dependency to packages that declare `enzyme` and resolve React and React DOM 16.9 to 17.";
137
+ }
138
+ }
139
+ exports.AddTestingLibraryReact = AddTestingLibraryReact;
140
+ class AddTestingLibraryReact18 extends EnzymeTestDependency {
141
+ constructor(forecast = {}) {
142
+ super(RTL_18_ADDITION, forecast);
143
+ this.name = "org.openrewrite.react.migrate.add-testing-library-react-18";
144
+ this.displayName = "Add `@testing-library/react` 16 where Enzyme tests on React 18 are migrated";
145
+ this.description = "Adds `@testing-library/react` 16 as a dev dependency to packages that declare `enzyme` and resolve React and React DOM 18.";
146
+ }
147
+ }
148
+ exports.AddTestingLibraryReact18 = AddTestingLibraryReact18;
149
+ class AddTestingLibraryDom18 extends EnzymeTestDependency {
150
+ constructor(forecast = {}) {
151
+ super(DOM_18_ADDITION, forecast);
152
+ this.name = "org.openrewrite.react.migrate.add-testing-library-dom-18";
153
+ this.displayName = "Add `@testing-library/dom` where Enzyme tests on React 18 are migrated";
154
+ this.description = "Adds `@testing-library/dom` 10, the peer of `@testing-library/react` 16, as a dev dependency to packages that declare `enzyme` and resolve React and React DOM 18.";
155
+ }
156
+ admits(manifest) {
157
+ var _a, _b;
158
+ const rtl = declaration(manifest, RTL);
159
+ if (rtl === undefined || !declares(manifest, enzyme_usage_1.ENZYME) || declares(manifest, DOM))
160
+ return true;
161
+ const resolved = (_b = (_a = rtl.resolved) === null || _a === void 0 ? void 0 : _a.version.match(/^(\d+)\./)) === null || _b === void 0 ? void 0 : _b[1];
162
+ if (resolved !== undefined)
163
+ return Number(resolved) === 16;
164
+ const version = declaredVersion(rtl.versionConstraint);
165
+ return version !== undefined && version !== "unsupported" && version.major === 16;
166
+ }
167
+ }
168
+ exports.AddTestingLibraryDom18 = AddTestingLibraryDom18;
169
+ function rtlLanded(rtl, rtl18, dom18) {
170
+ return new Set([...rtl, ...[...rtl18].filter(root => dom18.has(root))]);
171
+ }
172
+ function declaration(manifest, name) {
173
+ var _a, _b, _c, _d;
174
+ const resolution = (0, javascript_1.findNodeResolutionResult)(manifest);
175
+ return [...(_a = resolution === null || resolution === void 0 ? void 0 : resolution.dependencies) !== null && _a !== void 0 ? _a : [], ...(_b = resolution === null || resolution === void 0 ? void 0 : resolution.devDependencies) !== null && _b !== void 0 ? _b : [],
176
+ ...(_c = resolution === null || resolution === void 0 ? void 0 : resolution.optionalDependencies) !== null && _c !== void 0 ? _c : [], ...(_d = resolution === null || resolution === void 0 ? void 0 : resolution.peerDependencies) !== null && _d !== void 0 ? _d : []].find(dep => dep.name === name);
177
+ }
178
+ function declares(manifest, name) {
179
+ return declaration(manifest, name) !== undefined;
180
+ }
41
181
  const ADDS_RTL = (() => {
42
182
  try {
43
183
  return supportsResolvingGate(rewriteVersion(require.resolve("@openrewrite/rewrite"), require.cache));
@@ -74,37 +214,62 @@ function documentAt(cursor) {
74
214
  return "document";
75
215
  return scope.declares("globalThis", "value") ? undefined : "globalThis.document";
76
216
  }
217
+ function formattedBy(cu, style) {
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ const detector = javascript_1.Autodetect.detector();
220
+ yield detector.sampleJavaScript(cu);
221
+ const detected = detector.getTabsAndIndentsStyle();
222
+ const useTabs = style.config.useTabs === true;
223
+ const tabWidth = typeof style.config.tabWidth === "number" ? style.config.tabWidth : 2;
224
+ return detected.useTabCharacter === useTabs && (useTabs || detected.indentSize === tabWidth);
225
+ });
226
+ }
77
227
  function renderOptions(document) {
78
228
  return `{container: ${document}.createElement('div')}`;
79
229
  }
230
+ const ROOT_STYLES = new Map([["@ant-design/icons", ">=5.1.1"]]);
231
+ function injectsRootStyles(packages, sourcePath) {
232
+ return [...packages.entries()].some(([root, declarations]) => sourcePath.startsWith(root) && declarations.rootStyles);
233
+ }
234
+ const CONTAINER_READS = new Set(["text", "rootHtml", "snapshot"]);
80
235
  const READS = {
81
236
  mount: { simulate: true, html: true, props: () => true },
82
237
  shallow: { simulate: false, html: false, props: enzyme_host_components_1.propsRenderHostElements }
83
238
  };
84
- function reactMajor(packages, sourcePath, addsRtl = false) {
85
- return compatibility(packages, sourcePath, addsRtl).react;
239
+ function reactMajor(packages, sourcePath, landed = new Set()) {
240
+ return compatibility(packages, sourcePath, landed).react;
86
241
  }
87
- function simulatesEvents(packages, sourcePath, addsRtl = false) {
88
- const { react, rtl } = compatibility(packages, sourcePath, addsRtl);
89
- return react === 17 && rtl >= 11;
242
+ const HOVER_EVENTS = new Set(["mouseEnter", "mouseLeave", "mouseOver", "mouseOut"]);
243
+ function simulatesEvents(packages, sourcePath, landed = new Set()) {
244
+ const { react, rtl } = compatibility(packages, sourcePath, landed);
245
+ if (react >= 17 && rtl >= 11)
246
+ return { events: enzyme_wrapper_scope_1.SIMULATED_EVENTS, attached: false };
247
+ if (react === 16 && rtl >= 10)
248
+ return { events: new Set([...enzyme_wrapper_scope_1.SIMULATED_EVENTS].filter(event => !HOVER_EVENTS.has(event))), attached: true };
249
+ return { events: new Set(), attached: false };
90
250
  }
91
- function compatibility(packages, sourcePath, addsRtl) {
251
+ function compatibility(packages, sourcePath, landed) {
92
252
  var _a;
93
253
  const roots = [...packages.keys()].filter(root => sourcePath.startsWith(root)).sort((a, b) => b.length - a.length);
94
254
  const declared = (name, from = roots) => from.map(root => packages.get(root)[name]).find(version => version !== undefined);
95
255
  const supportedReact = (version) => typeof version === "object" &&
96
- (version.major === 17 || (version.major === 16 && version.minor !== undefined && version.minor >= 9));
256
+ (version.major === 17 || version.major === 18 || (version.major === 16 && version.minor !== undefined && version.minor >= 9));
97
257
  const react = declared("react");
98
258
  const rtl = declared("rtl");
99
259
  const enzyme = roots.findIndex(root => packages.get(root).enzyme);
100
- const added = addsRtl && rtl === undefined && enzyme >= 0 && packages.get(roots[enzyme]).lockfileRegenerated &&
101
- supportedReact(declared("react", roots.slice(enzyme))) && supportedReact(declared("reactDom", roots.slice(enzyme)));
260
+ const enzymeReact = enzyme >= 0 ? declared("react", roots.slice(enzyme)) : undefined;
261
+ const added = rtl === undefined && enzyme >= 0 && landed.has(roots[enzyme]) &&
262
+ supportedReact(enzymeReact) && supportedReact(declared("reactDom", roots.slice(enzyme)));
102
263
  const rtlRoot = added ? enzyme : roots.findIndex(root => packages.get(root).rtl !== undefined);
103
264
  const reactDom = (_a = declared("reactDom", roots.slice(Math.max(rtlRoot, 0)))) !== null && _a !== void 0 ? _a : declared("reactDom");
104
- const rtlMajor = added ? 12 : typeof rtl === "object" && rtl.major >= 10 && rtl.major <= 12 ? rtl.major : 0;
105
- const compatible = supportedReact(react) && supportedReact(reactDom) && react.major === reactDom.major && rtlMajor > 0;
265
+ const rtlMajor = added ? (enzymeReact.major >= 18 ? 16 : 12) : typeof rtl === "object" && rtl.major >= 10 && rtl.major <= 16 ? rtl.major : 0;
266
+ const compatible = supportedReact(react) && supportedReact(reactDom) && react.major === reactDom.major &&
267
+ rtlMajor > 0 && rtlSupports(rtlMajor, react.major);
106
268
  return { react: compatible ? react.major : 0, rtl: rtlMajor };
107
269
  }
270
+ function rtlSupports(rtl, react) {
271
+ return react <= 17 ? rtl <= 12 : rtl === 16;
272
+ }
108
273
  function declaredVersion(version) {
109
274
  var _a;
110
275
  if (version === undefined)
@@ -119,18 +284,32 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
119
284
  super(options);
120
285
  this.name = "org.openrewrite.react.migrate.enzyme-to-react-testing-library";
121
286
  this.displayName = "Migrate Enzyme to React Testing Library";
122
- this.description = "Migrates Enzyme `mount` wrappers whose every use is a supported DOM read or event (`find(selector).length`, `.exists()`, `.text()`, `.html()`, `.hasClass()`, positions through `.at(n)`, `.first()`, `.last()`, the wrapper's `text()` and `exists(selector)`, found nodes as a `toHaveLength` subject, `setProps` as a rerender of the cloned element, and on React 17 `simulate` with common mouse, keyboard, focus, form, and touch events, including `nativeEvent` mocks) and bare `mount` statements to React Testing Library container queries, `rerender`, and `fireEvent` in packages using React 16.9+/17 with RTL 10 to 12, adding RTL 12 where it is missing. Converts `shallow` wrappers the same way, without `simulate` or `html()`, where the component is imported from a source in the repository that creates host elements only and runs no effects, so a full render produces the DOM the shallow render does. Selectors are limited to HTML or SVG tag, class, ID, and pass-through attribute compounds and their descendant combinations. Reports remaining Enzyme usages for manual review. Requires an existing DOM test environment; shared wrappers, props, snapshots, and shallow rendering of other components are left for review.";
287
+ this.description = "Migrates Enzyme `mount` wrappers whose every use is a supported DOM read or event (`find(selector).length`, `.exists()`, `.text()`, `.html()`, `.hasClass()`, positions through `.at(n)`, `.first()`, `.last()`, the wrapper's `text()` and `exists(selector)`, found nodes as a `toHaveLength` subject, `setProps` as a rerender of the cloned element, and `simulate` with common mouse, keyboard, focus, form, and touch events, including `nativeEvent` mocks, on React 17 and 18 and, without the hover events and in a render attached to the document, on React 16.9+) and bare `mount` statements to React Testing Library container queries, `rerender`, and `fireEvent` in packages using React 16.9+/17 with RTL 10 to 12 or React 18 with RTL 16, adding RTL 12, or RTL 16 with its peer `@testing-library/dom` 10, where it is missing. Converts `shallow` wrappers the same way, without `simulate` or `html()`, where the component is imported from a source in the repository that creates host elements only and runs no effects, so a full render produces the DOM the shallow render does. Selectors are limited to HTML or SVG tag, class, ID, and pass-through attribute compounds and their descendant combinations. Reports remaining Enzyme usages for manual review. Requires an existing DOM test environment; shared wrappers, props, snapshots, and shallow rendering of other components are left for review.";
288
+ this.forecast = {};
289
+ this.tests = [];
290
+ this.dependencySteps = ADDS_RTL ? { rtl: new AddTestingLibraryReact(this.forecast), rtl18: new AddTestingLibraryReact18(this.forecast),
291
+ dom18: new AddTestingLibraryDom18(this.forecast) } : undefined;
292
+ this.snapshots = false;
123
293
  this.usages = new rewrite_1.DataTable(enzyme_usage_1.ENZYME_USAGE_TABLE, "Enzyme migration inventory", "Remaining Enzyme usages and migration actions. Module rows include references whose call types may be unavailable; they are not counts of test cases.", enzyme_usage_1.EnzymeUsageRow);
124
294
  }
125
295
  initialValue() {
126
- return { packages: new Map(), components: new Map() };
296
+ this.tests.length = 0;
297
+ return { packages: new Map(), components: new Map(), modules: new Map(), names: new Map() };
127
298
  }
128
299
  scanner(scanned) {
129
300
  return __awaiter(this, void 0, void 0, function* () {
301
+ const steps = this.dependencySteps;
302
+ const tests = this.tests;
303
+ let converting;
304
+ this.forecast.converting = steps && (() => converting !== null && converting !== void 0 ? converting : (converting = this.dryRun(scanned, rtlLanded(steps.rtl.candidates(), steps.rtl18.candidates(), steps.dom18.candidates()))));
130
305
  const sources = new class extends javascript_1.JavaScriptVisitor {
131
306
  visitJsCompilationUnit(cu) {
132
307
  return __awaiter(this, void 0, void 0, function* () {
133
308
  scanned.components.set(cu.sourcePath, (0, enzyme_host_components_1.hostComponentExports)(cu));
309
+ scanned.modules.set(cu.sourcePath, (0, enzyme_portals_1.moduleFacts)(cu));
310
+ scanned.names.set(cu.sourcePath, (0, enzyme_selector_names_1.selectorNames)(cu));
311
+ if ((0, module_imports_1.importBindings)(cu).some(({ module }) => module === enzyme_usage_1.ENZYME))
312
+ tests.push(cu);
134
313
  return cu;
135
314
  });
136
315
  }
@@ -138,7 +317,7 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
138
317
  const manifests = new class extends json_1.JsonVisitor {
139
318
  visitDocument(doc, ctx) {
140
319
  return __awaiter(this, void 0, void 0, function* () {
141
- var _a, _b, _c, _d;
320
+ var _a, _b, _c, _d, _e;
142
321
  if (!/(^|\/)package\.json$/.test(doc.sourcePath))
143
322
  return doc;
144
323
  const resolution = (0, javascript_1.findNodeResolutionResult)(doc);
@@ -158,13 +337,22 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
158
337
  reactDom: declaredVersion(version("react-dom")),
159
338
  rtl: declaredVersion(version(RTL)),
160
339
  enzyme: dependencies.some(dep => dep.name === "enzyme"),
161
- lockfileRegenerated: (resolution === null || resolution === void 0 ? void 0 : resolution.packageManager) !== "Pnpm" && (resolution === null || resolution === void 0 ? void 0 : resolution.packageManager) !== "Bun"
340
+ dependencies: new Set(dependencies.map(dep => dep.name)),
341
+ rootStyles: ((_e = resolution === null || resolution === void 0 ? void 0 : resolution.resolvedDependencies) !== null && _e !== void 0 ? _e : []).some(dep => {
342
+ const range = ROOT_STYLES.get(dep.name);
343
+ return range !== undefined && (0, semver_1.valid)(dep.version) !== null && (0, semver_1.satisfies)(dep.version, range);
344
+ })
162
345
  });
163
346
  return doc;
164
347
  });
165
348
  }
166
349
  }();
167
- return new class extends rewrite_1.TreeVisitor {
350
+ const scan = new class extends rewrite_1.TreeVisitor {
351
+ isAcceptable(sourceFile) {
352
+ return __awaiter(this, void 0, void 0, function* () {
353
+ return (yield manifests.isAcceptable(sourceFile)) || (yield sources.isAcceptable(sourceFile));
354
+ });
355
+ }
168
356
  visit(tree, ctx, parent) {
169
357
  return __awaiter(this, void 0, void 0, function* () {
170
358
  if (tree.kind === json_1.Json.Kind.Document)
@@ -173,12 +361,30 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
173
361
  });
174
362
  }
175
363
  }();
364
+ return (0, rewrite_1.check)((0, rewrite_1.or)((0, java_recipe_proxy_1.treesOfType)("org.openrewrite.json.tree.Json"), (0, java_recipe_proxy_1.treesOfType)("org.openrewrite.javascript.tree.JS")), scan);
176
365
  });
177
366
  }
178
367
  editorWithData(scanned) {
368
+ return __awaiter(this, void 0, void 0, function* () {
369
+ return this.conversionEditor(scanned);
370
+ });
371
+ }
372
+ dryRun(scanned, landed) {
373
+ return __awaiter(this, void 0, void 0, function* () {
374
+ const dry = { landed, converted: new Set() };
375
+ const editor = yield this.conversionEditor(scanned, dry);
376
+ const ctx = new rewrite_1.ExecutionContext();
377
+ for (const cu of this.tests)
378
+ yield editor.visit(cu, ctx);
379
+ this.tests.length = 0;
380
+ return dry.converted;
381
+ });
382
+ }
383
+ conversionEditor(scanned, dry) {
179
384
  return __awaiter(this, void 0, void 0, function* () {
180
385
  const jsx = (0, javascript_1.capture)({ name: "element", constraint: node => node.kind !== javascript_1.JS.Kind.Spread });
181
- const selector = (0, javascript_1.capture)({ name: "selector", constraint: node => (0, enzyme_wrapper_scope_1.selectorLiteral)(node) });
386
+ const hostNames = (names) => (0, enzyme_selector_names_1.hostOnlyNames)(names, scanned.names);
387
+ const selector = (0, javascript_1.capture)({ name: "selector", constraint: node => (0, enzyme_wrapper_scope_1.selectorLiteral)(node, hostNames) });
182
388
  const after = { context: [`import {render} from "${RTL}";`], dependencies: { [RTL]: "12.1.5", "@types/react": "16.14.62" } };
183
389
  const patterns = (api) => {
184
390
  const before = { lenientTypeMatching: false, context: [`import {${api}} from "enzyme";`], dependencies: { "@types/enzyme": "3.10.19", "@types/react": "16.14.62" } };
@@ -204,6 +410,10 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
204
410
  mount: (0, javascript_1.rewrite)(() => ({
205
411
  before: from.render,
206
412
  after: (0, javascript_1.template) `render(${jsx}, ${options})`.configure(after)
413
+ })),
414
+ attachedMount: (0, javascript_1.rewrite)(() => ({
415
+ before: from.render,
416
+ after: (0, javascript_1.template) `render(${jsx})`.configure(after)
207
417
  }))
208
418
  };
209
419
  };
@@ -212,13 +422,16 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
212
422
  "globalThis.document": { mount: renderRules("globalThis.document", "mount"), shallow: renderRules("globalThis.document", "shallow") }
213
423
  };
214
424
  const usages = this.usages;
425
+ const steps = this.dependencySteps;
215
426
  const untaggedPositions = this.untaggedSelectorPositions !== false;
427
+ const snapshots = this.snapshots;
216
428
  return (0, rewrite_1.check)((0, rewrite_1.and)((0, rewrite_1.not)((0, javascript_1.isVendoredOrBundled)()), (0, rewrite_1.or)((0, javascript_1.usesMethod)("enzyme mount(..)"), (0, javascript_1.usesMethod)("enzyme shallow(..)"))), new class extends javascript_1.JavaScriptVisitor {
217
429
  constructor() {
218
430
  super(...arguments);
219
431
  this.enabled = false;
220
432
  this.reactMajor = 0;
221
- this.events = false;
433
+ this.events = { events: new Set(), attached: false };
434
+ this.rootStyles = false;
222
435
  this.typescript = false;
223
436
  this.sourcePath = "";
224
437
  this.mounts = new Map();
@@ -233,6 +446,7 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
233
446
  this.hoists = new Map();
234
447
  this.hoistedMounts = new Map();
235
448
  this.names = new Set();
449
+ this.portals = new Set();
236
450
  this.rows = [];
237
451
  this.prettier = false;
238
452
  this.printWidth = 80;
@@ -249,19 +463,38 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
249
463
  this.rows.length = 0;
250
464
  this.names = (0, javascript_1.namesUsedIn)(cu);
251
465
  const prettierStyle = (0, rewrite_1.findMarker)(cu, javascript_1.StyleKind.PrettierStyle);
252
- this.prettier = prettierStyle !== undefined;
466
+ this.prettier = prettierStyle !== undefined && !prettierStyle.ignored && (yield formattedBy(cu, prettierStyle));
253
467
  this.printWidth = typeof (prettierStyle === null || prettierStyle === void 0 ? void 0 : prettierStyle.config.printWidth) === "number" ? prettierStyle.config.printWidth : 80;
254
- this.reactMajor = reactMajor(scanned.packages, cu.sourcePath, ADDS_RTL);
255
- this.events = simulatesEvents(scanned.packages, cu.sourcePath, ADDS_RTL);
468
+ const landed = dry ? dry.landed : steps ? rtlLanded(steps.rtl.landed, steps.rtl18.landed, steps.dom18.landed) : undefined;
469
+ this.reactMajor = reactMajor(scanned.packages, cu.sourcePath, landed);
470
+ this.events = simulatesEvents(scanned.packages, cu.sourcePath, landed);
471
+ this.rootStyles = injectsRootStyles(scanned.packages, cu.sourcePath);
256
472
  this.typescript = (0, javascript_1.isTypeScript)(cu);
257
473
  this.enabled = this.reactMajor > 0 && (0, javascript_1.moduleBindings)(this).moduleSystem === "esm";
258
474
  if (!this.enabled)
259
475
  return cu;
260
476
  this.imports = (0, enzyme_host_components_1.relativeImports)(cu, scanned.components);
261
477
  this.importedNames = (0, enzyme_host_components_1.importedNames)(cu);
262
- const visited = yield _super.visitJsCompilationUnit.call(this, cu, ctx);
263
- this.rows.forEach(row => usages.insertRow(ctx, row));
264
- return visited;
478
+ const foreign = prettierStyle !== undefined && !this.prettier;
479
+ const visiting = foreign ? (0, mutative_1.create)(cu, draft => {
480
+ draft.markers.markers = draft.markers.markers.filter(marker => marker.kind !== javascript_1.StyleKind.PrettierStyle);
481
+ }) : cu;
482
+ if (foreign)
483
+ this.cursor = new rewrite_1.Cursor(visiting, this.cursor.parent);
484
+ this.cu = cu;
485
+ this.portals.clear();
486
+ const visited = yield _super.visitJsCompilationUnit.call(this, visiting, ctx);
487
+ if (dry) {
488
+ if (this.rows.some(row => row.status === "CONVERTED"))
489
+ dry.converted.add(cu.sourcePath);
490
+ }
491
+ else
492
+ this.rows.forEach(row => usages.insertRow(ctx, row));
493
+ if (!foreign || !visited)
494
+ return visited;
495
+ return visited === visiting ? cu : (0, mutative_1.create)(visited, draft => {
496
+ draft.markers.markers.push(prettierStyle);
497
+ });
265
498
  });
266
499
  }
267
500
  visitBlock(block, ctx) {
@@ -277,7 +510,8 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
277
510
  continue;
278
511
  const api = candidate.render;
279
512
  const reads = READS[api];
280
- const scope = (0, enzyme_wrapper_scope_1.analyzeWrapperScope)(this.cursor, candidate, { events: this.events && reads.simulate, html: reads.html, untaggedPositions });
513
+ const scope = (0, enzyme_wrapper_scope_1.analyzeWrapperScope)(this.cursor, candidate, { events: reads.simulate ? this.events.events : new Set(), html: reads.html, untaggedPositions, hostNames, snapshots,
514
+ constant: node => this.constantString(node) });
281
515
  if (!scope)
282
516
  continue;
283
517
  if ([...scope.uses.values()].some(use => use.use === "setProps" && !reads.props(use.props, this.importedNames)))
@@ -286,7 +520,10 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
286
520
  const document = documentAt(mount);
287
521
  if (!document || !(yield renderPatterns[api].render.match(candidate.mount, mount)))
288
522
  continue;
289
- this.mounts.set(candidate.mount.id, { document, api });
523
+ const attached = this.events.attached && [...scope.uses.values()].some(use => use.use === "simulate");
524
+ if (!attached && this.rootStyles && [...scope.uses.values()].some(use => CONTAINER_READS.has(use.use)))
525
+ continue;
526
+ this.mounts.set(candidate.mount.id, { document, api, attached });
290
527
  apis.push(api);
291
528
  this.declarations.add(candidate.declaration.id);
292
529
  scope.uses.forEach((use, id) => this.uses.set(id, use));
@@ -415,14 +652,14 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
415
652
  if (mounted) {
416
653
  const hoisted = this.hoistedMounts.get(method.id);
417
654
  const result = hoisted ?
418
- yield this.applyTemplate(javascript_1.Template.builder().code(`render(${hoisted}, ${renderOptions(mounted.document)})`).build().configure(after), visited) :
419
- yield rules[mounted.document][mounted.api].mount.tryOn(this.cursor, visited, { visitor: this });
655
+ yield this.applyTemplate(javascript_1.Template.builder().code(mounted.attached ? `render(${hoisted})` : `render(${hoisted}, ${renderOptions(mounted.document)})`).build().configure(after), visited) :
656
+ yield rules[mounted.document][mounted.api][mounted.attached ? "attachedMount" : "mount"].tryOn(this.cursor, visited, { visitor: this });
420
657
  if (!result)
421
658
  return visited;
422
659
  this.pending.delete(method.id);
423
660
  return (0, enzyme_wrapper_scope_1.withRenderOptionsOnElementLine)(result, ctx);
424
661
  }
425
- const statementOf = (0, enzyme_wrapper_scope_1.mountedElement)(visited) && (0, enzyme_wrapper_scope_1.statementPosition)(this.cursor) ? this.renderOf(visited) : undefined;
662
+ const statementOf = (0, enzyme_wrapper_scope_1.mountedElement)(visited) && (0, enzyme_wrapper_scope_1.statementPosition)(this.cursor) && !(0, enzyme_wrapper_scope_1.passesRef)(visited) ? this.renderOf(visited, false) : undefined;
426
663
  const statement = statementOf && (yield this.tryInline(statementOf, "mount", visited, statementOf, (0, enzyme_usage_1.enclosingScope)(this.cursor), "MOUNT_STATEMENT", ctx));
427
664
  if (statement)
428
665
  return statement;
@@ -501,8 +738,14 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
501
738
  return element(false).code("?.textContent").build();
502
739
  case "html":
503
740
  return element(!!read.unasserted).code(read.unasserted ? ".outerHTML" : "?.outerHTML").build();
741
+ case "rootHtml":
742
+ return code.param(read.wrapper).code(".container.innerHTML").build();
743
+ case "node":
744
+ return element(true).build();
504
745
  case "hasClass":
505
746
  return element(true).code(".classList.contains(").param(read.className).code(")").build();
747
+ case "snapshot":
748
+ return code.code("expect(").param(read.wrapper).code(".asFragment())").build();
506
749
  case "setProps":
507
750
  code.param(read.wrapper).code(`.rerender(${read.reassigned ? `(${read.element} = ` : ""}cloneElement(${read.element}, `);
508
751
  return code.param(read.props).code(read.reassigned ? ")))" : "))").build().configure({
@@ -532,23 +775,56 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
532
775
  });
533
776
  }
534
777
  }
535
- renderOf(node) {
778
+ renderOf(node, read = true) {
536
779
  var _a;
537
780
  if ((node === null || node === void 0 ? void 0 : node.kind) !== java_1.J.Kind.MethodInvocation)
538
781
  return undefined;
539
782
  const call = node;
540
783
  const api = (0, enzyme_usage_1.enzymeMethod)(call);
541
784
  if (api === "mount")
542
- return api;
785
+ return read && this.rendersPortal(call) ? undefined : api;
543
786
  const element = (_a = call.arguments.elements[0]) === null || _a === void 0 ? void 0 : _a.element;
544
787
  return api === "shallow" && call.arguments.elements.length === 1 && (0, enzyme_host_components_1.rendersHostElements)(element, this.imports, scanned.components, this.importedNames) ? api : undefined;
545
788
  }
789
+ rendersPortal(call) {
790
+ var _a;
791
+ const element = (_a = call.arguments.elements[0]) === null || _a === void 0 ? void 0 : _a.element;
792
+ const { roots, declares, resolve } = this.moduleResolution();
793
+ const portal = !!element && !!this.cu && (0, enzyme_portals_1.rendersPortal)(element, this.cu, {
794
+ sourcePath: this.sourcePath,
795
+ modules: scanned.modules,
796
+ resolve,
797
+ declares,
798
+ packagePortals: () => [...scanned.modules].some(([path, facts]) => {
799
+ var _a;
800
+ return facts.portal &&
801
+ path.startsWith((_a = roots[0]) !== null && _a !== void 0 ? _a : "") && !/(^|\/)__tests?__\/|\.(test|spec)\.[cm]?[jt]sx?$/.test(path);
802
+ })
803
+ });
804
+ if (portal && !this.portals.has(call.id)) {
805
+ this.portals.add(call.id);
806
+ this.rows.push({ sourcePath: this.sourcePath, scope: (0, enzyme_usage_1.enclosingScope)(this.cursor), api: "mount", status: "REVIEW", reason: "PORTAL",
807
+ suggestedAction: "A component it renders may render into a portal, which RTL renders outside the container: query `screen` or `baseElement` for that content." });
808
+ }
809
+ return portal;
810
+ }
811
+ moduleResolution() {
812
+ const roots = [...scanned.packages.keys()].filter(root => this.sourcePath.startsWith(root)).sort((a, b) => b.length - a.length);
813
+ const declares = (name) => roots.some(root => scanned.packages.get(root).dependencies.has(name));
814
+ return { roots, declares, resolve: (0, enzyme_portals_1.resolver)(scanned.modules, [...new Set([...roots, ""])], declares) };
815
+ }
816
+ constantString(node) {
817
+ const parts = (0, enzyme_constant_strings_1.stringParts)(node);
818
+ return parts && (0, enzyme_constant_strings_1.evaluate)(parts, this.sourcePath, scanned.modules, this.moduleResolution().resolve);
819
+ }
546
820
  laidOutByPrettier(node) {
547
821
  if (this.prettier)
548
822
  this.reformat.add(node.id);
549
823
  }
550
824
  converted(api, scope, reason) {
551
825
  (0, javascript_1.maybeRemoveImport)(this, enzyme_usage_1.ENZYME);
826
+ if (snapshots)
827
+ (0, javascript_1.maybeRemoveImport)(this, "enzyme-to-json");
552
828
  this.rows.push({ sourcePath: this.sourcePath, scope, api, status: "CONVERTED", reason,
553
829
  suggestedAction: "Run the migrated test and review whether an accessible RTL query would better express its intent." });
554
830
  }
@@ -557,7 +833,8 @@ class EnzymeToReactTestingLibrary extends rewrite_1.ScanningRecipe {
557
833
  }
558
834
  recipeList() {
559
835
  return __awaiter(this, void 0, void 0, function* () {
560
- return [...ADDS_RTL ? [yield (0, javascript_1.addDependency)(RTL_ADDITION)] : [], new find_enzyme_usage_1.FindEnzymeUsage()];
836
+ const steps = this.dependencySteps;
837
+ return [...steps ? [steps.rtl, steps.rtl18, steps.dom18] : [], new find_enzyme_usage_1.FindEnzymeUsage()];
561
838
  });
562
839
  }
563
840
  }
@@ -569,4 +846,14 @@ __decorate([
569
846
  required: false
570
847
  })
571
848
  ], EnzymeToReactTestingLibrary.prototype, "untaggedSelectorPositions", void 0);
849
+ class EnzymeSnapshotsToReactTestingLibrary extends EnzymeToReactTestingLibrary {
850
+ constructor() {
851
+ super(...arguments);
852
+ this.name = "org.openrewrite.react.migrate.enzyme-snapshots-to-react-testing-library";
853
+ this.displayName = "Migrate Enzyme to React Testing Library, snapshots included";
854
+ this.description = "Migrates Enzyme wrappers as `org.openrewrite.react.migrate.enzyme-to-react-testing-library` does, and also converts `expect(wrapper).toMatchSnapshot()` and `expect(toJson(wrapper)).toMatchSnapshot()` of those wrappers to `expect(wrapper.asFragment()).toMatchSnapshot()`, removing the `enzyme-to-json` import where nothing else uses it. RTL snapshots the rendered DOM where Enzyme's serializer recorded a component tree, so the stored snapshots of converted tests no longer match: run the tests with `--updateSnapshot` once and review the new snapshots. Inline snapshots and snapshots of shallow renders that are not converted are left for review.";
855
+ this.snapshots = true;
856
+ }
857
+ }
858
+ exports.EnzymeSnapshotsToReactTestingLibrary = EnzymeSnapshotsToReactTestingLibrary;
572
859
  //# sourceMappingURL=enzyme-to-react-testing-library.js.map