test: updated tests to use template instead of helper
MODIFY
internal/js-ast-utils/isJSXElement.test.ts
internal/js-ast-utils/isJSXElement.test.ts
import {test} from "rome";
-import {AnyNode, jsExpressionStatement} from "@internal/ast";
-import {parseJS} from "@internal/js-parser";
import {isJSXElement} from "./isJSXElement";
-import {createBuilder} from "@internal/ast/utils";
-
-function helper(input: string): AnyNode {
- const node = jsExpressionStatement.assert(
- parseJS({
- input,
- path: "unknown",
- }).body[0],
- );
- return node.expression;
-}
+import {template} from "@internal/js-ast-utils";
test(
"returns true for jsx elements",
(t) => {
- t.true(isJSXElement(helper("<div>a</div>"), "div"));
+ t.true(isJSXElement(template.expression`<div>a</div>`, "div"));
+ t.true(isJSXElement(template.expression`<ul></ul>`, "ul"));
+ t.true(isJSXElement(template.expression`<ul>text</ul>`, "ul"));
t.true(
- isJSXElement(
- createBuilder(
- "JSXElement",
- {
- bindingKeys: {},
- visitorKeys: {
- name: true,
- typeArguments: true,
- attributes: true,
- children: true,
- },
- },
- ).create(helper("<ul></ul>")),
- "ul",
- ),
+ isJSXElement(template.expression`<span id="key">{var}</span>`, "span"),
);
- t.true(isJSXElement(helper("<ul>text</ul>"), "ul"));
- t.true(isJSXElement(helper(`<span id="key">{var}</span>`), "span"));
- t.true(isJSXElement(helper("<CustomComp></CustomComp>"), "CustomComp"));
- t.true(isJSXElement(helper("<component>"), "component"));
+ t.true(
+ isJSXElement(template.expression`<CustomComp></CustomComp>`, "CustomComp"),
+ );
+ t.true(isJSXElement(template.expression`<component>`, "component"));
},
);
test(
"returns false for non-jsx elements",
(t) => {
- t.false(isJSXElement(helper("2+3"), "div"));
- t.false(isJSXElement(helper("someValue / 4"), "ul"));
- t.false(isJSXElement(helper("2;"), "li"));
- t.false(isJSXElement(helper("true"), "span"));
- t.false(
- isJSXElement(
- createBuilder(
- "jsIfStatement",
- {
- bindingKeys: {},
- visitorKeys: {test: true, consequent: true, alternate: true},
- },
- ).create({}),
- "li",
- ),
- );
+ t.false(isJSXElement(template.expression`2+3`, "div"));
+ t.false(isJSXElement(template.expression`someValue / 4`, "ul"));
+ t.false(isJSXElement(template.expression`2;`, "li"));
+ t.false(isJSXElement(template.expression`true`, "span"));
+ t.false(isJSXElement(template.statement`if(a>2){}`, "li"));
},
);
test(
"returns false when there is name mismatch",
(t) => {
- t.false(isJSXElement(helper("<div></div>"), "ul"));
- t.false(isJSXElement(helper("<span></span>"), "img"));
- t.false(isJSXElement(helper("<test>"), "otherTest"));
+ t.false(isJSXElement(template.expression`<div></div>`, "ul"));
+ t.false(isJSXElement(template.expression`<span></span>`, "img"));
+ t.false(isJSXElement(template.expression`<test>`, "otherTest"));
},
);

test: updated tests to use template instead of helper (#1090)