Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from "@babel/types";

export default function(
export default function (
api,
{
attrName = "data-test-id",
Expand All @@ -27,10 +27,10 @@ export default function(
"h6",
"svg",
"path",
"g"
"g",
],
additionalIgnoreElements = [],
delimiter = "-"
delimiter = "-",
}
) {
let isRootElement = true;
Expand All @@ -54,7 +54,7 @@ export default function(
isRootElement || path.parent.type === "ReturnStatement";
const isIgnoredElement = [
...ignoreElements,
...additionalIgnoreElements
...additionalIgnoreElements,
].includes(componentName);

if (
Expand All @@ -80,16 +80,17 @@ export default function(
? t.jsxExpressionContainer(t.identifier(concatComponentName))
: t.stringLiteral(concatComponentName);

path.node.openingElement.attributes.push(
t.jSXAttribute(t.jSXIdentifier(attrName), testId)
);
!isIgnoredElement &&
path.node.openingElement.attributes.push(
t.jSXAttribute(t.jSXIdentifier(attrName), testId)
);

mode === "full" &&
passDownComponentName(path, componentName, mode, delimiter);
}
},
});
}
}
},
},
};
}

Expand Down Expand Up @@ -130,11 +131,11 @@ const passDownComponentName = (path, componentName, mode, delimiter) => {
}

isRootElement = false;
}
},
});
};

const getKey = path => {
const getKey = (path) => {
const keyAttribute = path.node.openingElement.attributes.find(
({ name }) => name && name.name === "key"
);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/classes/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Foo extends React.Component {
render() {
const { children, className } = this.props;
return (
<div className={className} data-test-id="Foo">
<div className={className}>
<div>
<Bar data-test-id="Foo-Bar">{children}</Bar>
</div>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/complex/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Foo = ({ className, children }) => {
const x = 2;
return (
<div className={className} data-test-id="Foo">
<div className={className}>
<div>
<Bar data-test-id="Foo-Bar">{children}</Bar>
</div>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/fragment/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Foo = ({ children }) => (
<>
<React.Fragment>
<Fragment>
<div data-test-id="Foo">
<div>
<Bar data-test-id="Foo-Bar">{children}</Bar>
</div>
</Fragment>
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/map/expected.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Foo = () => (
<div data-test-id="Foo">
<div>
<div>
<Bar data-test-id="Foo-Bar">
{[1, 2, 3, 4, 5].map(i => (
{[1, 2, 3, 4, 5].map((i) => (
<span key={i}>{i}</span>
))}
</Bar>
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/nested/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Foo = () => (
<div data-test-id="Foo">
<div>
<Bar data-test-id="Foo-Bar" />
</div>
);

const Bar = () => <div data-test-id="Bar">Hi</div>;
const Bar = () => <div>Hi</div>;
2 changes: 1 addition & 1 deletion test/fixtures/options-additionalIgnoreElements/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Foo = () => (
<div data-test-id="Foo">
<div>
<div>
<span>
<test>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/options-attrName/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Foo = () => (
<div test-id="Foo">
<div>
<div>
<Bar1 test-id="Foo-Bar1" />
<Bar2 test-id="Foo-Bar2">
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/options-mode-full/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Foo = () => (
<div data-test-id="Foo">
<div>
<div>
<Bar1 data-test-id="Foo-div-Bar1" />
<Bar2 data-test-id="Foo-div-Bar2">
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/options-mode-minimal/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Foo = () => (
<div data-test-id="Foo">
<div>
<div>
<Bar1 data-test-id="Bar1" />
<Bar2 data-test-id="Bar2">
Expand Down