@openrewrite/recipes-react 0.15.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.
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.d.ts +7 -0
- package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.js +58 -1
- package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts +3 -0
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js +10 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts +2 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.js +24 -0
- package/dist/migration/cra-to-vite/cra-runtime.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts +8 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.js +82 -0
- package/dist/migration/cra-to-vite/cra-script.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts +2 -1
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/cra-to-vite.js +23 -16
- package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts +4 -0
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js +35 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +22 -29
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/index.d.ts +8 -2
- package/dist/migration/cra-to-vite/index.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/index.js +17 -2
- package/dist/migration/cra-to-vite/index.js.map +1 -1
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts +17 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js +181 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts +9 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js +66 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts +49 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js +506 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts +2 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/move-public-index-html.js +12 -7
- package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -1
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts +30 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js +428 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts +15 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js +100 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts +12 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js +85 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts +20 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js +220 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js +6 -3
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +11 -4
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +47 -50
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts +3 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js +42 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +13 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/scan-cra-projects.js +52 -4
- package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +2 -15
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +32 -130
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/migration/cra-to-vite/svg-component.d.ts +5 -0
- package/dist/migration/cra-to-vite/svg-component.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/svg-component.js +60 -0
- package/dist/migration/cra-to-vite/svg-component.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +8 -2
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +3 -2
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +28 -29
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +4 -13
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -1
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -1
- package/dist/react17/use-new-jsx-transform.js +30 -78
- package/dist/react17/use-new-jsx-transform.js.map +1 -1
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +16 -8
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.js +62 -20
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +10 -13
- package/dist/react19/replace-default-props.js.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.js +1 -6
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +4 -7
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
- package/dist/refactoring/create-element-to-jsx.js +3 -4
- package/dist/refactoring/create-element-to-jsx.js.map +1 -1
- package/dist/refactoring/prop-types-to-typescript.js +2 -3
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +3 -6
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/dist/testing/enzyme-constant-strings.d.ts +12 -0
- package/dist/testing/enzyme-constant-strings.d.ts.map +1 -0
- package/dist/testing/enzyme-constant-strings.js +101 -0
- package/dist/testing/enzyme-constant-strings.js.map +1 -0
- package/dist/testing/enzyme-host-components.d.ts +14 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -0
- package/dist/testing/enzyme-host-components.js +248 -0
- package/dist/testing/enzyme-host-components.js.map +1 -0
- package/dist/testing/enzyme-portals.d.ts +38 -0
- package/dist/testing/enzyme-portals.d.ts.map +1 -0
- package/dist/testing/enzyme-portals.js +226 -0
- package/dist/testing/enzyme-portals.js.map +1 -0
- package/dist/testing/enzyme-selector-names.d.ts +9 -0
- package/dist/testing/enzyme-selector-names.d.ts.map +1 -0
- package/dist/testing/enzyme-selector-names.js +50 -0
- package/dist/testing/enzyme-selector-names.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +80 -12
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +447 -135
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +27 -4
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +23 -8
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +291 -169
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +16 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -0
- package/dist/util/java-recipe-proxy.js +85 -0
- package/dist/util/java-recipe-proxy.js.map +1 -0
- package/dist/util/module-imports.d.ts +13 -0
- package/dist/util/module-imports.d.ts.map +1 -0
- package/dist/util/module-imports.js +60 -0
- package/dist/util/module-imports.js.map +1 -0
- package/dist/util/react-class.d.ts +1 -0
- package/dist/util/react-class.d.ts.map +1 -1
- package/dist/util/react-class.js +3 -2
- package/dist/util/react-class.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +2 -1
- package/src/migration/cra-to-vite/add-vite-config.ts +53 -1
- package/src/migration/cra-to-vite/cra-file-gate.ts +8 -0
- package/src/migration/cra-to-vite/cra-runtime.ts +16 -0
- package/src/migration/cra-to-vite/cra-script.ts +61 -0
- package/src/migration/cra-to-vite/cra-to-vite.ts +25 -36
- package/src/migration/cra-to-vite/cra-typecheck.ts +33 -0
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +23 -28
- package/src/migration/cra-to-vite/index.ts +10 -2
- package/src/migration/cra-to-vite/migrate-cra-package.ts +127 -0
- package/src/migration/cra-to-vite/migrate-cra-svg-imports.ts +41 -0
- package/src/migration/cra-to-vite/migrate-cra-tests-to-vitest.ts +376 -0
- package/src/migration/cra-to-vite/move-public-index-html.ts +12 -7
- package/src/migration/cra-to-vite/plan-cra-to-vite.ts +283 -0
- package/src/migration/cra-to-vite/prepare-cra-package.ts +86 -0
- package/src/migration/cra-to-vite/remove-cra-configuration.ts +64 -0
- package/src/migration/cra-to-vite/rename-jsx-files.ts +155 -0
- package/src/migration/cra-to-vite/rename-process-env-react-app.ts +6 -3
- package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +1 -1
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +48 -54
- package/src/migration/cra-to-vite/resolved-cra-dependency.ts +41 -0
- package/src/migration/cra-to-vite/scan-cra-projects.ts +57 -6
- package/src/migration/cra-to-vite/shared.ts +35 -146
- package/src/migration/cra-to-vite/svg-component.ts +39 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +11 -4
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +30 -41
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +6 -13
- package/src/react17/migrate-deprecated-lifecycles.ts +2 -2
- package/src/react17/use-new-jsx-transform.ts +30 -72
- package/src/react18/replace-reactdom-render.ts +17 -8
- package/src/react18/replace-unmount-component-at-node.ts +78 -32
- package/src/react19/replace-default-props.ts +11 -15
- package/src/react19/replace-react-shallow-renderer.ts +2 -7
- package/src/refactoring/create-class-to-es6.ts +6 -9
- package/src/refactoring/create-element-to-jsx.ts +4 -6
- package/src/refactoring/prop-types-to-typescript.ts +4 -4
- package/src/refactoring/pure-render-mixin.ts +4 -7
- package/src/testing/enzyme-constant-strings.ts +95 -0
- package/src/testing/enzyme-host-components.ts +287 -0
- package/src/testing/enzyme-portals.ts +279 -0
- package/src/testing/enzyme-selector-names.ts +59 -0
- package/src/testing/enzyme-to-react-testing-library.ts +584 -140
- package/src/testing/enzyme-usage.ts +29 -4
- package/src/testing/enzyme-wrapper-scope.ts +309 -151
- package/src/util/java-recipe-proxy.ts +81 -0
- package/src/util/module-imports.ts +60 -0
- package/src/util/react-class.ts +2 -1
|
@@ -47,9 +47,8 @@ export function migrationReason(api: string): {reason: string, suggestedAction:
|
|
|
47
47
|
if (api === "shallow" || api === "dive") {
|
|
48
48
|
return {reason: "SHALLOW_RENDERING", suggestedAction: "Review child rendering and effects before replacing shallow rendering with RTL render."};
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
50
|
+
const internal = INTERNAL_STATE_ACTIONS[api];
|
|
51
|
+
if (internal) return {reason: "INTERNAL_STATE", suggestedAction: internal};
|
|
53
52
|
if (api === "simulate") {
|
|
54
53
|
return {reason: "UNSUPPORTED_EVENT", suggestedAction: "Review event payloads and async control flow before selecting user-event or fireEvent."};
|
|
55
54
|
}
|
|
@@ -65,9 +64,35 @@ export function migrationReason(api: string): {reason: string, suggestedAction:
|
|
|
65
64
|
if (api === "configure") {
|
|
66
65
|
return {reason: "ENZYME_SETUP", suggestedAction: "Keep Enzyme setup until no tests or helpers in the owning package need it."};
|
|
67
66
|
}
|
|
68
|
-
return {reason: "WRAPPER_API", suggestedAction:
|
|
67
|
+
return {reason: "WRAPPER_API", suggestedAction: WRAPPER_API_ACTIONS[api] ??
|
|
68
|
+
"Review this Enzyme wrapper API for an equivalent RTL interaction or DOM assertion."};
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
// What to write by hand for each, in RTL's terms: RTL has no component tree, so each becomes a DOM read or a user action
|
|
72
|
+
const INTERNAL_STATE_ACTIONS: Readonly<Record<string, string>> = {
|
|
73
|
+
state: "Assert what the state renders, such as text, a class, or an `aria-*` attribute, with `screen.getByText` or `getByRole`, instead of reading component state.",
|
|
74
|
+
setState: "Reach the state as a user does: fire the event that sets it with `fireEvent` or `userEvent`, or `rerender` with the props that lead to it.",
|
|
75
|
+
instance: "Trigger the method through the UI that calls it and assert the result in the DOM, or move its logic into a function and test that directly.",
|
|
76
|
+
invoke: "Fire the DOM event that calls the handler, with `fireEvent` or `userEvent`, instead of invoking the prop."
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const WRAPPER_API_ACTIONS: Readonly<Record<string, string>> = {
|
|
80
|
+
prop: "Assert what the prop produces in the DOM, such as text, an attribute, or a role. For a callback prop, fire the event that calls it and assert on a `jest.fn()`.",
|
|
81
|
+
props: "Assert what the props produce in the DOM. To check what a child component receives, mock that component with `jest.mock` and assert on the mock's calls.",
|
|
82
|
+
setProps: "Call the `rerender` that RTL's `render` returns, with the element and its new props.",
|
|
83
|
+
text: "Read `textContent` of the queried element, or find it with `screen.getByText`.",
|
|
84
|
+
html: "Assert on `container.innerHTML` or the queried element's `outerHTML`; Enzyme's `html()` of a component is its rendered markup.",
|
|
85
|
+
at: "Index the `querySelectorAll` or `getAllBy*` result; for a component selector, query the DOM element the component renders instead.",
|
|
86
|
+
first: "Take the first element of the `querySelectorAll` or `getAllBy*` result, or use `querySelector` or `getBy*` for a single match.",
|
|
87
|
+
last: "Take the last element of the `querySelectorAll` or `getAllBy*` result.",
|
|
88
|
+
children: "Read the queried element's `children`, or query its descendants with `within(element)`.",
|
|
89
|
+
exists: "Use `queryBy*` with `toBeNull()` or `not.toBeNull()`, or test the length of a `querySelectorAll`.",
|
|
90
|
+
hasClass: "Use `element.classList.contains`, or `toHaveClass` from `@testing-library/jest-dom`.",
|
|
91
|
+
getDOMNode: "Query the element directly with `screen.getBy*` or `container.querySelector`; RTL already returns DOM nodes.",
|
|
92
|
+
update: "Remove it: RTL renders the latest state on every query. Await `findBy*` or `waitFor` where the change is asynchronous.",
|
|
93
|
+
unmount: "Call the `unmount` that RTL's `render` returns, or rely on RTL's automatic cleanup after each test."
|
|
94
|
+
};
|
|
95
|
+
|
|
71
96
|
export function enclosingScope(start: Cursor): string {
|
|
72
97
|
for (let cursor = start.parent; cursor; cursor = cursor.parent) {
|
|
73
98
|
const node = cursor.value as J;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {Cursor, ExecutionContext} from "@openrewrite/rewrite";
|
|
2
|
-
import {JavaScriptVisitor, JS, JSX} from "@openrewrite/rewrite/javascript";
|
|
3
|
-
import {emptySpace, Expression, isIdentifier, J
|
|
2
|
+
import {compilationUnitOf, isObjectLiteral, JavaScriptVisitor, JS, JSX, referencesOf, walk} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {emptySpace, Expression, isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
4
4
|
import {create} from "mutative";
|
|
5
5
|
import {enzymeMethod, enzymeWrapper} from "./enzyme-usage";
|
|
6
|
+
import {importBindings} from "../util/module-imports";
|
|
6
7
|
|
|
7
8
|
const HTML_TAGS = new Set(["a", "abbr", "address", "area", "article", "aside", "audio", "b", "bdi", "bdo",
|
|
8
9
|
"blockquote", "body", "br", "button", "canvas", "caption", "cite", "code", "col", "colgroup", "data",
|
|
@@ -44,10 +45,10 @@ function compoundTags(value: string): (string | undefined)[] | undefined {
|
|
|
44
45
|
* descendants. Enzyme compares an attribute value strictly against the
|
|
45
46
|
* prop, while the DOM sees it stringified, so values that a number or boolean prop renders to are
|
|
46
47
|
* excluded. Enzyme additionally matches composite elements whose props satisfy a class, ID, or
|
|
47
|
-
* attribute compound
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* combinators and pseudo-classes are excluded.
|
|
48
|
+
* attribute compound. In the last compound that only adds matches, so counts can only drop, which a
|
|
49
|
+
* converted assertion shows, while positions can shift, which {@link positional} guards. In an ancestor
|
|
50
|
+
* compound it can also lose them, which {@link selectorLiteral} guards. A composite element between a
|
|
51
|
+
* parent and its child breaks a child combinator, and other combinators and pseudo-classes are excluded.
|
|
51
52
|
*/
|
|
52
53
|
export function domSelector(value: unknown): value is string {
|
|
53
54
|
if (typeof value !== "string" || value === "") return false;
|
|
@@ -60,13 +61,45 @@ function onlyStringPropsRenderTo(value: string): boolean {
|
|
|
60
61
|
return value !== "true" && value !== "false" && String(Number(value)) !== value;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
/**
|
|
65
|
+
* A selector literal {@link domSelector} accepts, whose ancestor compounds name a tag or only names that
|
|
66
|
+
* `hostNames` finds on host elements alone. A component that receives an ancestor compound's class or
|
|
67
|
+
* attribute passes it to the element it renders, as `<Button className="close">` does to its `button`:
|
|
68
|
+
* Enzyme then finds `.close button` and the DOM, where that `button` has the class itself, does not.
|
|
69
|
+
*/
|
|
70
|
+
export function selectorLiteral(node: J | undefined, hostNames: HostNames): node is J.Literal {
|
|
71
|
+
return node?.kind === J.Kind.Literal && acceptedSelector((node as J.Literal).value, hostNames);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A selector string {@link selectorLiteral} accepts, however it is written. */
|
|
75
|
+
function acceptedSelector(value: unknown, hostNames: HostNames): value is string {
|
|
76
|
+
if (!domSelector(value)) return false;
|
|
77
|
+
const names = untaggedAncestorNames(value as string);
|
|
78
|
+
return names !== undefined && (names.length === 0 || hostNames(names));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Whether only host elements carry each of the classes, IDs, and attribute values. */
|
|
82
|
+
export type HostNames = (names: readonly string[]) => boolean;
|
|
83
|
+
|
|
84
|
+
const NAME = new RegExp(String.raw`[.#](-?[_a-zA-Z][\w-]*)|\[${ATTRIBUTE}(?:="([^"\\]*)"|='([^'\\]*)')?\]`, "g");
|
|
85
|
+
|
|
86
|
+
/** The names in the ancestor compounds of a selector that name no tag, or undefined where one tests an attribute's presence. */
|
|
87
|
+
function untaggedAncestorNames(selector: string): string[] | undefined {
|
|
88
|
+
const compounds = selector.split(" ").slice(0, -1).filter(compound => !/^[a-z]/.test(compound));
|
|
89
|
+
const names: string[] = [];
|
|
90
|
+
for (const compound of compounds) {
|
|
91
|
+
for (const [, name, double, single] of compound.matchAll(NAME)) {
|
|
92
|
+
const value = name ?? double ?? single;
|
|
93
|
+
if (value === undefined) return undefined;
|
|
94
|
+
names.push(value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return names;
|
|
65
98
|
}
|
|
66
99
|
|
|
67
100
|
/** The tag of the elements a selector matches. */
|
|
68
|
-
function selectorTag(selector:
|
|
69
|
-
const tags = compoundTags(selector
|
|
101
|
+
function selectorTag(selector: string): string | undefined {
|
|
102
|
+
const tags = compoundTags(selector);
|
|
70
103
|
return tags?.[tags.length - 1];
|
|
71
104
|
}
|
|
72
105
|
|
|
@@ -75,15 +108,27 @@ function selectorTag(selector: J.Literal): string | undefined {
|
|
|
75
108
|
* into the matches depends on: where every compound names a tag, or `hostNodes()` keeps the host
|
|
76
109
|
* elements a single compound matches.
|
|
77
110
|
*/
|
|
78
|
-
function positional(selector:
|
|
79
|
-
const tags = compoundTags(selector
|
|
111
|
+
function positional(selector: string, hostNodes: boolean): boolean {
|
|
112
|
+
const tags = compoundTags(selector)!;
|
|
80
113
|
return tags.every(tag => tag !== undefined) || (hostNodes && tags.length === 1);
|
|
81
114
|
}
|
|
82
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Whether a `find(within)` from an element that `selector` matches finds only elements below it. Enzyme's
|
|
118
|
+
* `find` also tests the element it is called on, as `querySelectorAll` does not, and matches the compounds of
|
|
119
|
+
* a selector among the element and what is below it, where `querySelectorAll` matches the ancestor compounds
|
|
120
|
+
* anywhere in the document. So `within` is a single compound naming a tag other than the element's.
|
|
121
|
+
*/
|
|
122
|
+
function onlyBelow(selector: string, within: string): boolean {
|
|
123
|
+
const tag = selectorTag(selector);
|
|
124
|
+
const tags = compoundTags(within);
|
|
125
|
+
return tag !== undefined && tags?.length === 1 && tags[0] !== undefined && tags[0] !== tag;
|
|
126
|
+
}
|
|
127
|
+
|
|
83
128
|
// Enzyme's `simulate` calls the React handler directly, while `fireEvent` dispatches a DOM event that
|
|
84
129
|
// React 17 receives through its root container. A dispatched click also runs the default action, such
|
|
85
130
|
// as toggling a checkbox or following a link, so clicks on those elements stay excluded.
|
|
86
|
-
const SIMULATED_EVENTS = new Set(["click", "doubleClick", "mouseDown", "mouseUp", "mouseEnter", "mouseLeave",
|
|
131
|
+
export const SIMULATED_EVENTS: ReadonlySet<string> = new Set(["click", "doubleClick", "mouseDown", "mouseUp", "mouseEnter", "mouseLeave",
|
|
87
132
|
"mouseOver", "mouseOut", "mouseMove", "focus", "blur", "submit", "keyDown", "keyUp", "change", "touchStart",
|
|
88
133
|
"touchMove", "touchEnd", "wheel"]);
|
|
89
134
|
// Enzyme also accepts the DOM's lowercase names of these events, and `dblclick` for a double click
|
|
@@ -106,7 +151,7 @@ export function simpleChain(node: Expression): boolean {
|
|
|
106
151
|
return isIdentifier(node);
|
|
107
152
|
}
|
|
108
153
|
|
|
109
|
-
export type WrapperUse = "length" | "exists" | "positionExists" | "nodes" | "selectorText" | "html" | "text" | "hasClass" | "simulate" | "setProps";
|
|
154
|
+
export type WrapperUse = "length" | "exists" | "positionExists" | "nodes" | "selectorText" | "html" | "rootHtml" | "node" | "text" | "hasClass" | "simulate" | "setProps" | "snapshot";
|
|
110
155
|
|
|
111
156
|
/** `first` and `last` index like Enzyme's methods of the same name; a literal like `at(n)`. */
|
|
112
157
|
export type ElementIndex = J.Literal | "first" | "last";
|
|
@@ -114,10 +159,11 @@ export type ElementIndex = J.Literal | "first" | "last";
|
|
|
114
159
|
export interface WrapperRead {
|
|
115
160
|
use: WrapperUse;
|
|
116
161
|
wrapper: J.Identifier;
|
|
117
|
-
selector
|
|
162
|
+
/** The selector as the test writes it, a string literal or a template of constant strings. */
|
|
163
|
+
selector?: Expression;
|
|
118
164
|
index?: ElementIndex;
|
|
119
165
|
/** A `find` within the positioned element. */
|
|
120
|
-
within?:
|
|
166
|
+
within?: Expression;
|
|
121
167
|
/** Found nodes asserted only to be truthy, which any wrapper and any node list are. */
|
|
122
168
|
vacuous?: boolean;
|
|
123
169
|
/** A read outside an assertion, which throws where nothing is found, as Enzyme's single-node methods do. */
|
|
@@ -142,12 +188,6 @@ export interface WrapperScope {
|
|
|
142
188
|
unmounts: Set<string>;
|
|
143
189
|
}
|
|
144
190
|
|
|
145
|
-
// The declaration is an Enzyme `mount`, so a reference is its wrapper unless another binding of the
|
|
146
|
-
// name shadows it; a project without Enzyme's type declarations leaves the wrapper untyped.
|
|
147
|
-
function untyped(type: Type | undefined): boolean {
|
|
148
|
-
return !type || type.kind === Type.Kind.Unknown;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
191
|
function noArguments(call: J.MethodInvocation): boolean {
|
|
152
192
|
return call.arguments.elements.every(arg => arg.element.kind === J.Kind.Empty);
|
|
153
193
|
}
|
|
@@ -177,6 +217,24 @@ export function mountedElement(mount: J.MethodInvocation): boolean {
|
|
|
177
217
|
return mount.arguments.elements.length === 1 && element.element.kind !== J.Kind.Empty && element.element.kind !== JS.Kind.Spread;
|
|
178
218
|
}
|
|
179
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Whether the element a mount renders passes a `ref` anywhere in it. A bare mount statement keeps no wrapper, so
|
|
222
|
+
* such a test renders to take the nodes or instances the ref receives. Enzyme never unmounts them, while RTL's
|
|
223
|
+
* cleanup unmounts every render after the test, which throws where the test moved them, as a test of a library
|
|
224
|
+
* that reparents fibers does.
|
|
225
|
+
*/
|
|
226
|
+
export function passesRef(mount: J.MethodInvocation): boolean {
|
|
227
|
+
let ref = false;
|
|
228
|
+
walk(mount.arguments.elements[0].element, tree => {
|
|
229
|
+
if (tree.kind === JS.Kind.JsxAttribute) {
|
|
230
|
+
const key = (tree as JSX.Attribute).key;
|
|
231
|
+
if (isIdentifier(key) && key.simpleName === "ref") ref = true;
|
|
232
|
+
}
|
|
233
|
+
return !ref;
|
|
234
|
+
});
|
|
235
|
+
return ref;
|
|
236
|
+
}
|
|
237
|
+
|
|
180
238
|
/** The matcher a node list can take the place of found nodes for, as `expect`'s subject. */
|
|
181
239
|
function nodesMatcher(expectCursor: Cursor): "length" | "truthy" | undefined {
|
|
182
240
|
const expect = expectCursor.value as J;
|
|
@@ -199,11 +257,9 @@ function stringLiteral(node: J | undefined): node is J.Literal {
|
|
|
199
257
|
}
|
|
200
258
|
|
|
201
259
|
function objectProperties(node: J | undefined): Map<string, Expression> | undefined {
|
|
202
|
-
if (node
|
|
203
|
-
const object = node as J.NewClass;
|
|
204
|
-
if (object.class || !object.body) return undefined;
|
|
260
|
+
if (!node || !isObjectLiteral(node) || !node.body) return undefined;
|
|
205
261
|
const properties = new Map<string, Expression>();
|
|
206
|
-
for (const statement of
|
|
262
|
+
for (const statement of node.body.statements) {
|
|
207
263
|
const property = statement.element as JS.PropertyAssignment;
|
|
208
264
|
if (property.kind !== JS.Kind.PropertyAssignment || property.assigmentToken !== "Colon" ||
|
|
209
265
|
!isIdentifier(property.name.element) || !property.initializer) return undefined;
|
|
@@ -237,6 +293,41 @@ function nativeEventInit(event: string, init: J | undefined): [string, Expressio
|
|
|
237
293
|
return event !== "change" && properties?.size === 1 && nativeEvent ? [...nativeEvent] : undefined;
|
|
238
294
|
}
|
|
239
295
|
|
|
296
|
+
/**
|
|
297
|
+
* `expect(wrapper.html())` with a matcher RTL's `container.innerHTML` meets exactly where Enzyme's `html()` does. The two
|
|
298
|
+
* differ only where nothing renders, `null` against `""`, which a snapshot shows and a non-empty string fails either way.
|
|
299
|
+
*/
|
|
300
|
+
function innerHtmlAssertion(read: Cursor, html: J): boolean {
|
|
301
|
+
if (!expectArgument(read, html)) return false;
|
|
302
|
+
const expect = read.parentTree()!;
|
|
303
|
+
const matcherCursor = expect.parentTree();
|
|
304
|
+
const matcher = matcherCursor?.value as J | undefined;
|
|
305
|
+
if (!selectOf(matcher, expect.value) || !statement(matcherCursor!)) return false;
|
|
306
|
+
const args = (matcher as J.MethodInvocation).arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
|
|
307
|
+
if (call(matcher, "toMatchSnapshot")) return args.length === 0 || args.length === 1 && stringLiteral(args[0]);
|
|
308
|
+
return (call(matcher, "toBe") || call(matcher, "toEqual") || call(matcher, "toContain")) &&
|
|
309
|
+
args.length === 1 && stringLiteral(args[0]) && (args[0] as J.Literal).value !== "";
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** `expect(() => wrapper.unmount()).not.toThrow();`, which unmounts where it stands, as the statement `wrapper.unmount();` does. */
|
|
313
|
+
function unmountsWithoutThrowing(unmount: Cursor): boolean {
|
|
314
|
+
const lambdaCursor = unmount.parentTree();
|
|
315
|
+
const lambda = lambdaCursor?.value as J.Lambda | undefined;
|
|
316
|
+
if (lambda?.kind !== J.Kind.Lambda || lambda.body !== unmount.value ||
|
|
317
|
+
lambda.parameters.parameters.some(parameter => parameter.element.kind !== J.Kind.Empty)) return false;
|
|
318
|
+
const arrowCursor = lambdaCursor!.parentTree();
|
|
319
|
+
const arrow = arrowCursor?.value as JS.ArrowFunction | undefined;
|
|
320
|
+
if (arrow?.kind !== JS.Kind.ArrowFunction || arrow.lambda !== lambda || !expectArgument(arrowCursor!, arrow)) return false;
|
|
321
|
+
const expectCursor = arrowCursor!.parentTree()!;
|
|
322
|
+
const not = expectCursor.parentTree()?.value as J | undefined;
|
|
323
|
+
if (not?.kind !== J.Kind.FieldAccess || (not as J.FieldAccess).target !== expectCursor.value ||
|
|
324
|
+
(not as J.FieldAccess).name.element.simpleName !== "not") return false;
|
|
325
|
+
const matcherCursor = expectCursor.parentTree()!.parentTree();
|
|
326
|
+
const matcher = matcherCursor?.value as J | undefined;
|
|
327
|
+
return call(matcher, "toThrow") && (matcher as J.MethodInvocation).select?.element === not &&
|
|
328
|
+
noArguments(matcher as J.MethodInvocation) && statement(matcherCursor!) !== undefined;
|
|
329
|
+
}
|
|
330
|
+
|
|
240
331
|
function expectArgument(cursor: Cursor, node: J): boolean {
|
|
241
332
|
const parent = cursor.parentTree()?.value as J | undefined;
|
|
242
333
|
if (!call(parent, "expect")) return false;
|
|
@@ -244,7 +335,18 @@ function expectArgument(cursor: Cursor, node: J): boolean {
|
|
|
244
335
|
return !expect.select && expect.arguments.elements.length === 1 && expect.arguments.elements[0].element === node;
|
|
245
336
|
}
|
|
246
337
|
|
|
247
|
-
export
|
|
338
|
+
export interface WrapperDeclaration {
|
|
339
|
+
declaration: J.VariableDeclarations;
|
|
340
|
+
name: string;
|
|
341
|
+
mount: J.MethodInvocation;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function variableCursor(block: Cursor, declaration: J.VariableDeclarations): Cursor {
|
|
345
|
+
return new Cursor(declaration.variables[0].element, new Cursor(declaration, block));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** A `const` declaration of a wrapper that `classify` accepts the render of, with what it says of the render. */
|
|
349
|
+
export function wrapperDeclaration<R>(statement: J | undefined, classify: (render: J.MethodInvocation) => R | undefined): (WrapperDeclaration & {render: R}) | undefined {
|
|
248
350
|
if (statement?.kind !== J.Kind.VariableDeclarations) return undefined;
|
|
249
351
|
const declaration = statement as J.VariableDeclarations;
|
|
250
352
|
if (declaration.variables.length !== 1 || declaration.typeExpression ||
|
|
@@ -254,156 +356,212 @@ export function wrapperDeclaration(statement: J | undefined): {declaration: J.Va
|
|
|
254
356
|
const initializer = variable.initializer?.element;
|
|
255
357
|
if (!isIdentifier(variable.name) || initializer?.kind !== J.Kind.MethodInvocation) return undefined;
|
|
256
358
|
const mount = initializer as J.MethodInvocation;
|
|
257
|
-
if (
|
|
258
|
-
|
|
359
|
+
if (mount.typeParameters || !mountedElement(mount)) return undefined;
|
|
360
|
+
const render = classify(mount);
|
|
361
|
+
return render === undefined ? undefined : {declaration, name: variable.name.simpleName, mount, render};
|
|
259
362
|
}
|
|
260
363
|
|
|
261
364
|
/**
|
|
262
365
|
* Proves that every reference to a block-scoped wrapper is a supported DOM read, so the
|
|
263
366
|
* declaration and all its uses can be converted together. Any other reference, including
|
|
264
|
-
*
|
|
265
|
-
* the
|
|
367
|
+
* arguments, returns, and shorthand properties, rejects the whole scope. `events` are those `simulate`
|
|
368
|
+
* may dispatch, and `html` admits `html()` and the DOM node of a host element, none of which a shallow wrapper reads as the DOM does. `snapshots`
|
|
369
|
+
* admits `expect(wrapper).toMatchSnapshot()` and `expect(toJson(wrapper)).toMatchSnapshot()`, whose
|
|
370
|
+
* snapshot RTL records as the DOM rather than Enzyme's tree. `hostNames` decides the selectors {@link selectorLiteral} accepts.
|
|
266
371
|
*/
|
|
267
|
-
export
|
|
268
|
-
|
|
269
|
-
|
|
372
|
+
export function analyzeWrapperScope(block: Cursor, {declaration, name}: WrapperDeclaration,
|
|
373
|
+
{events, html, untaggedPositions, hostNames, snapshots = false, constant = () => undefined}: {events: ReadonlySet<string>, html: boolean, untaggedPositions: boolean, hostNames: HostNames, snapshots?: boolean, constant?: (node: J) => string | undefined}): WrapperScope | undefined {
|
|
374
|
+
let serializers: ReadonlySet<string> | undefined;
|
|
270
375
|
const uses = new Map<string, WrapperRead>();
|
|
271
376
|
const removedStatements = new Set<string>();
|
|
272
377
|
const unmounts = new Set<string>();
|
|
273
|
-
let declared = false;
|
|
274
|
-
let supported = true;
|
|
275
378
|
let unmounted = false;
|
|
276
379
|
const variable = declaration.variables[0].element;
|
|
277
|
-
await new class extends JavaScriptVisitor<ExecutionContext> {
|
|
278
|
-
override async visitIdentifier(identifier: J.Identifier, ctx: ExecutionContext): Promise<J | undefined> {
|
|
279
|
-
if (identifier.simpleName !== name || !supported) return identifier;
|
|
280
|
-
if (identifier === variable.name) {
|
|
281
|
-
declared = true;
|
|
282
|
-
return identifier;
|
|
283
|
-
}
|
|
284
|
-
const use = unmounted ? undefined : this.use(identifier);
|
|
285
|
-
if (!use) supported = false;
|
|
286
|
-
return identifier;
|
|
287
|
-
}
|
|
288
380
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
381
|
+
// The string a selector argument holds where Enzyme and the DOM read it alike, written as a literal or built from constants
|
|
382
|
+
const selectorValue = (node: J | undefined): string | undefined => {
|
|
383
|
+
if (!node) return undefined;
|
|
384
|
+
if (selectorLiteral(node, hostNames)) return node.value as string;
|
|
385
|
+
const value = node.kind === JS.Kind.TemplateExpression ? constant(node) : undefined;
|
|
386
|
+
return acceptedSelector(value, hostNames) ? value : undefined;
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const record = (node: Expression, read: WrapperRead): boolean => {
|
|
390
|
+
if (!simpleChain(node)) return false;
|
|
391
|
+
uses.set(node.id, read);
|
|
392
|
+
return true;
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const elementIndex = (node: J | undefined, inner: J): ElementIndex | undefined => {
|
|
396
|
+
if (!selectOf(node, inner)) return undefined;
|
|
397
|
+
const method = node as J.MethodInvocation;
|
|
398
|
+
if ((call(method, "first") || call(method, "last")) && noArguments(method)) return method.name.simpleName as "first" | "last";
|
|
399
|
+
const [position] = method.arguments.elements;
|
|
400
|
+
if (call(method, "at") && method.arguments.elements.length === 1 && position.element.kind === J.Kind.Literal &&
|
|
401
|
+
Number.isInteger((position.element as J.Literal).value) && ((position.element as J.Literal).value as number) >= 0) {
|
|
402
|
+
return position.element as J.Literal;
|
|
403
|
+
}
|
|
404
|
+
return undefined;
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const use = (identifier: J.Identifier, reference: Cursor): boolean => {
|
|
408
|
+
const ancestors: Cursor[] = [];
|
|
409
|
+
for (let c: Cursor | undefined = reference.parentTree(); c && ancestors.length < 5; c = c.parentTree()) ancestors.push(c);
|
|
410
|
+
const first = ancestors[0]?.value as J | undefined;
|
|
411
|
+
if (snapshots) {
|
|
412
|
+
serializers ??= snapshotSerializers(block);
|
|
413
|
+
const assertion = snapshotAssertion(identifier, ancestors, serializers);
|
|
414
|
+
if (assertion) return record(assertion, {wrapper: identifier, use: "snapshot"});
|
|
415
|
+
}
|
|
416
|
+
if (!selectOf(first, identifier)) return false;
|
|
417
|
+
const method = first as J.MethodInvocation;
|
|
418
|
+
if (call(method, "find")) {
|
|
419
|
+
const args = method.arguments.elements;
|
|
420
|
+
const selectorText = args.length === 1 ? selectorValue(args[0].element) : undefined;
|
|
421
|
+
if (selectorText === undefined) return false;
|
|
422
|
+
const selector = args[0].element as Expression;
|
|
423
|
+
let k = 1;
|
|
424
|
+
let inner: J = method;
|
|
425
|
+
let hostNodes = false;
|
|
426
|
+
if (call(ancestors[k]?.value, "hostNodes") && selectOf(ancestors[k].value, inner) && noArguments(ancestors[k].value)) {
|
|
427
|
+
inner = ancestors[k++].value;
|
|
428
|
+
hostNodes = true;
|
|
429
|
+
}
|
|
430
|
+
const index = elementIndex(ancestors[k]?.value, inner);
|
|
431
|
+
let within: Expression | undefined;
|
|
432
|
+
if (index) {
|
|
433
|
+
if (!untaggedPositions && !positional(selectorText, hostNodes)) return false;
|
|
434
|
+
inner = ancestors[k++].value;
|
|
435
|
+
const nested = ancestors[k]?.value as J | undefined;
|
|
436
|
+
if (call(nested, "find") && selectOf(nested, inner)) {
|
|
437
|
+
const [argument] = (nested as J.MethodInvocation).arguments.elements;
|
|
438
|
+
const withinText = (nested as J.MethodInvocation).arguments.elements.length === 1 ? selectorValue(argument.element) : undefined;
|
|
439
|
+
if (withinText === undefined || !onlyBelow(selectorText, withinText)) return false;
|
|
440
|
+
within = argument.element as Expression;
|
|
311
441
|
inner = ancestors[k++].value;
|
|
312
|
-
const nested = ancestors[k]?.value as J | undefined;
|
|
313
|
-
if (call(nested, "find") && selectOf(nested, inner)) {
|
|
314
|
-
const [argument] = (nested as J.MethodInvocation).arguments.elements;
|
|
315
|
-
if ((nested as J.MethodInvocation).arguments.elements.length !== 1 || !selectorLiteral(argument.element)) return false;
|
|
316
|
-
within = argument.element;
|
|
317
|
-
inner = ancestors[k++].value;
|
|
318
|
-
}
|
|
319
442
|
}
|
|
320
|
-
const outer = ancestors[k]?.value as J | undefined;
|
|
321
|
-
const outerCursor = ancestors[k];
|
|
322
|
-
const read = {wrapper: identifier, selector, index, within};
|
|
323
|
-
if ((!index || within) && outer?.kind === J.Kind.FieldAccess && (outer as J.FieldAccess).target === inner &&
|
|
324
|
-
(outer as J.FieldAccess).name.element.simpleName === "length") {
|
|
325
|
-
return this.record(outer as J.FieldAccess, {...read, use: "length"});
|
|
326
|
-
}
|
|
327
|
-
if (!index && expectArgument(ancestors[k - 1], inner)) {
|
|
328
|
-
const matcher = nodesMatcher(outerCursor);
|
|
329
|
-
return matcher !== undefined && this.record(inner as Expression, {...read, use: "nodes", vacuous: matcher === "truthy"});
|
|
330
|
-
}
|
|
331
|
-
if (!selectOf(outer, inner)) return false;
|
|
332
|
-
const terminal = outer as J.MethodInvocation;
|
|
333
|
-
const terminalArgs = terminal.arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
|
|
334
|
-
if (call(terminal, "exists") && terminalArgs.length === 0) return this.record(terminal, {...read, use: !index || within ? "exists" : "positionExists"});
|
|
335
|
-
if (within) return false;
|
|
336
|
-
if ((call(terminal, "text") || call(terminal, "html")) && terminalArgs.length === 0) {
|
|
337
|
-
return this.record(terminal, {...read, use: call(terminal, "text") ? "selectorText" : "html",
|
|
338
|
-
unasserted: !expectArgument(outerCursor, terminal)});
|
|
339
|
-
}
|
|
340
|
-
if (call(terminal, "hasClass") && terminalArgs.length === 1 && stringLiteral(terminalArgs[0]) &&
|
|
341
|
-
/^-?[_a-zA-Z][\w-]*$/.test(terminalArgs[0].value as string)) {
|
|
342
|
-
return this.record(terminal, {...read, use: "hasClass", className: terminalArgs[0]});
|
|
343
|
-
}
|
|
344
|
-
if (call(terminal, "simulate") && events && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
|
|
345
|
-
stringLiteral(terminalArgs[0])) {
|
|
346
|
-
const name = terminalArgs[0].value as string;
|
|
347
|
-
const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
348
|
-
if (!event) return false;
|
|
349
|
-
const nativeEvent = nativeEventInit(event, terminalArgs[1]);
|
|
350
|
-
if (!nativeEvent && !eventInit(event, terminalArgs[1])) return false;
|
|
351
|
-
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selector) ?? "")) return false;
|
|
352
|
-
return this.record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: terminalArgs[1]}});
|
|
353
|
-
}
|
|
354
|
-
return false;
|
|
355
443
|
}
|
|
356
|
-
const [
|
|
357
|
-
|
|
358
|
-
|
|
444
|
+
const outer = ancestors[k]?.value as J | undefined;
|
|
445
|
+
const outerCursor = ancestors[k];
|
|
446
|
+
const read = {wrapper: identifier, selector, index, within};
|
|
447
|
+
if ((!index || within) && outer?.kind === J.Kind.FieldAccess && (outer as J.FieldAccess).target === inner &&
|
|
448
|
+
(outer as J.FieldAccess).name.element.simpleName === "length") {
|
|
449
|
+
return record(outer as J.FieldAccess, {...read, use: "length"});
|
|
450
|
+
}
|
|
451
|
+
if (!index && expectArgument(ancestors[k - 1], inner)) {
|
|
452
|
+
const matcher = nodesMatcher(outerCursor);
|
|
453
|
+
return matcher !== undefined && record(inner as Expression, {...read, use: "nodes", vacuous: matcher === "truthy"});
|
|
359
454
|
}
|
|
360
|
-
if (
|
|
361
|
-
|
|
362
|
-
|
|
455
|
+
if (!selectOf(outer, inner)) return false;
|
|
456
|
+
const terminal = outer as J.MethodInvocation;
|
|
457
|
+
const terminalArgs = terminal.arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty).map(arg => arg.element);
|
|
458
|
+
if (call(terminal, "exists") && terminalArgs.length === 0) return record(terminal, {...read, use: !index || within ? "exists" : "positionExists"});
|
|
459
|
+
if (within) return false;
|
|
460
|
+
// A mounted host element's instance is its DOM node, as is the node `getDOMNode` returns; a shallow one has neither
|
|
461
|
+
if (html && (call(terminal, "instance") || call(terminal, "getDOMNode")) && terminalArgs.length === 0 && positional(selectorText, hostNodes)) {
|
|
462
|
+
return record(terminal, {...read, use: "node"});
|
|
363
463
|
}
|
|
364
|
-
if (
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
removedStatements.add(method.id);
|
|
368
|
-
return simpleChain(method);
|
|
464
|
+
if ((call(terminal, "text") || html && call(terminal, "html")) && terminalArgs.length === 0) {
|
|
465
|
+
return record(terminal, {...read, use: call(terminal, "text") ? "selectorText" : "html",
|
|
466
|
+
unasserted: !expectArgument(outerCursor, terminal)});
|
|
369
467
|
}
|
|
370
|
-
if (call(
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
468
|
+
if (call(terminal, "hasClass") && terminalArgs.length === 1 && stringLiteral(terminalArgs[0]) &&
|
|
469
|
+
/^-?[_a-zA-Z][\w-]*$/.test(terminalArgs[0].value as string)) {
|
|
470
|
+
return record(terminal, {...read, use: "hasClass", className: terminalArgs[0]});
|
|
471
|
+
}
|
|
472
|
+
if (call(terminal, "simulate") && events.size > 0 && statement(outerCursor) && terminalArgs.length >= 1 && terminalArgs.length <= 2 &&
|
|
473
|
+
stringLiteral(terminalArgs[0])) {
|
|
474
|
+
const name = terminalArgs[0].value as string;
|
|
475
|
+
const event = SIMULATED_EVENTS.has(name) ? name : NATIVE_EVENT_NAMES.get(name);
|
|
476
|
+
if (!event || !events.has(event)) return false;
|
|
477
|
+
const nativeEvent = nativeEventInit(event, terminalArgs[1]);
|
|
478
|
+
if (!nativeEvent && !eventInit(event, terminalArgs[1])) return false;
|
|
479
|
+
if (event === "click" && CLICK_DEFAULT_ACTIONS.has(selectorTag(selectorText) ?? "")) return false;
|
|
480
|
+
return record(terminal, {...read, use: "simulate", event, ...nativeEvent ? {nativeEvent} : {eventInit: terminalArgs[1]}});
|
|
374
481
|
}
|
|
375
482
|
return false;
|
|
376
483
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const method = node as J.MethodInvocation;
|
|
381
|
-
if ((call(method, "first") || call(method, "last")) && noArguments(method)) return method.name.simpleName as "first" | "last";
|
|
382
|
-
const [position] = method.arguments.elements;
|
|
383
|
-
if (call(method, "at") && method.arguments.elements.length === 1 && position.element.kind === J.Kind.Literal &&
|
|
384
|
-
Number.isInteger((position.element as J.Literal).value) && ((position.element as J.Literal).value as number) >= 0) {
|
|
385
|
-
return position.element as J.Literal;
|
|
386
|
-
}
|
|
387
|
-
return undefined;
|
|
484
|
+
const [argument] = method.arguments.elements;
|
|
485
|
+
if (call(method, "exists") && method.arguments.elements.length === 1 && selectorValue(argument.element) !== undefined) {
|
|
486
|
+
return record(method, {wrapper: identifier, selector: argument.element as Expression, use: "exists"});
|
|
388
487
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
488
|
+
if (call(method, "setProps") && method.arguments.elements.length === 1 && statement(ancestors[0]) &&
|
|
489
|
+
argument.element.kind !== J.Kind.Empty && argument.element.kind !== JS.Kind.Spread) {
|
|
490
|
+
return record(method, {wrapper: identifier, use: "setProps", props: argument.element as Expression});
|
|
491
|
+
}
|
|
492
|
+
if (!noArguments(method)) return false;
|
|
493
|
+
if (html && call(method, "html") && innerHtmlAssertion(ancestors[0], method)) return record(method, {wrapper: identifier, use: "rootHtml"});
|
|
494
|
+
if (call(method, "text")) return record(method, {wrapper: identifier, use: "text", unasserted: !expectArgument(ancestors[0], method)});
|
|
495
|
+
if (call(method, "update") && statement(ancestors[0])?.prefix.comments.length === 0 && method.prefix.comments.length === 0) {
|
|
496
|
+
removedStatements.add(method.id);
|
|
497
|
+
return simpleChain(method);
|
|
498
|
+
}
|
|
499
|
+
if (call(method, "unmount") && (statement(ancestors[0]) || unmountsWithoutThrowing(ancestors[0]))) {
|
|
500
|
+
unmounted = true;
|
|
501
|
+
unmounts.add(method.id);
|
|
502
|
+
return simpleChain(method);
|
|
394
503
|
}
|
|
395
|
-
|
|
396
|
-
|
|
504
|
+
return false;
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const declared = new Cursor(variable.name, variableCursor(block, declaration));
|
|
508
|
+
const references = referencesOf(declared, name, "value");
|
|
509
|
+
if (!references.some(reference => reference.identifier === variable.name)) return undefined;
|
|
510
|
+
for (const reference of references) {
|
|
511
|
+
if (reference.identifier === variable.name) continue;
|
|
512
|
+
if (reference.declares || unmounted || !use(reference.identifier, reference.cursor)) return undefined;
|
|
513
|
+
}
|
|
514
|
+
return {declaration, name, uses, removedStatements, unmounts};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** The local names a source binds to `enzyme-to-json`'s serializers, such as its default export `toJson`. */
|
|
518
|
+
function snapshotSerializers(cursor: Cursor): ReadonlySet<string> {
|
|
519
|
+
const cu = compilationUnitOf(cursor);
|
|
520
|
+
return new Set(cu ? importBindings(cu).filter(({module}) => module === "enzyme-to-json").map(({local}) => local) : []);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* The `expect(wrapper)` or `expect(toJson(wrapper))` call of a `toMatchSnapshot()` assertion of the
|
|
525
|
+
* wrapper, with no property matcher and at most a snapshot name.
|
|
526
|
+
*/
|
|
527
|
+
function snapshotAssertion(identifier: J.Identifier, ancestors: Cursor[], serializers: ReadonlySet<string>): J.MethodInvocation | undefined {
|
|
528
|
+
let k = 0;
|
|
529
|
+
const onlyArgument = (call: J | undefined, argument: J) => call?.kind === J.Kind.MethodInvocation && !(call as J.MethodInvocation).select &&
|
|
530
|
+
(call as J.MethodInvocation).arguments.elements.length === 1 && (call as J.MethodInvocation).arguments.elements[0].element === argument;
|
|
531
|
+
let subject: J = identifier;
|
|
532
|
+
const serialized = ancestors[k]?.value as J | undefined;
|
|
533
|
+
if (onlyArgument(serialized, subject) && serializers.has((serialized as J.MethodInvocation).name.simpleName)) {
|
|
534
|
+
subject = serialized!;
|
|
535
|
+
k++;
|
|
536
|
+
}
|
|
537
|
+
const expectCall = ancestors[k]?.value as J | undefined;
|
|
538
|
+
if (!onlyArgument(expectCall, subject) || (expectCall as J.MethodInvocation).name.simpleName !== "expect") return undefined;
|
|
539
|
+
const matcher = ancestors[k + 1]?.value as J | undefined;
|
|
540
|
+
if (!call(matcher, "toMatchSnapshot") || !selectOf(matcher, expectCall!) || !statement(ancestors[k + 1])) return undefined;
|
|
541
|
+
const args = (matcher as J.MethodInvocation).arguments.elements.filter(arg => arg.element.kind !== J.Kind.Empty);
|
|
542
|
+
if (args.length > 1 || (args.length === 1 && !stringLiteral(args[0].element))) return undefined;
|
|
543
|
+
return expectCall as J.MethodInvocation;
|
|
397
544
|
}
|
|
398
545
|
|
|
399
|
-
|
|
546
|
+
/** The call a chain of calls and field accesses starts with, such as `mount(...)` in `mount(...).find(...).length`. */
|
|
547
|
+
export function innermostCall(node: J): J.MethodInvocation | undefined {
|
|
548
|
+
let call: J.MethodInvocation | undefined;
|
|
549
|
+
for (let current: J | undefined = node; current;) {
|
|
550
|
+
if (current.kind === J.Kind.FieldAccess) current = (current as J.FieldAccess).target;
|
|
551
|
+
else if (current.kind === J.Kind.MethodInvocation) {
|
|
552
|
+
call = current as J.MethodInvocation;
|
|
553
|
+
current = call.select?.element;
|
|
554
|
+
} else break;
|
|
555
|
+
}
|
|
556
|
+
return call;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export function idsWithin(node: J, ids: Set<string>): string[] {
|
|
400
560
|
const found: string[] = [];
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
}
|
|
406
|
-
}().visit(node, ctx);
|
|
561
|
+
walk(node, tree => {
|
|
562
|
+
if (ids.has(tree.id)) found.push(tree.id);
|
|
563
|
+
return true;
|
|
564
|
+
});
|
|
407
565
|
return found;
|
|
408
566
|
}
|
|
409
567
|
|