Skip to content

Commit 808c623

Browse files
authored
fix(to-have-attribute): fixed bug in prefer-to-have-attribute autofix (#64)
1 parent 564178b commit 808c623

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/__tests__/lib/rules/prefer-to-have-attribute.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-template-curly-in-string */
12
/**
23
* @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
34
* @author Ben Monro
@@ -14,7 +15,7 @@ import * as rule from "../../../rules/prefer-to-have-attribute";
1415
// Tests
1516
//------------------------------------------------------------------------------
1617

17-
const ruleTester = new RuleTester();
18+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
1819
ruleTester.run("prefer-to-have-attribute", rule, {
1920
valid: [
2021
"expect(element.foo).toBeTruthy()",
@@ -42,6 +43,17 @@ ruleTester.run("prefer-to-have-attribute", rule, {
4243
],
4344
output: `expect(element).toHaveAttribute('foo', expect.stringContaining('bar'));`,
4445
},
46+
{
47+
code:
48+
"expect(element.getAttribute('foo')).toContain(`bar=${encodeURIComponent(baz.id)}`);",
49+
errors: [
50+
{
51+
message: "Use toHaveAttribute instead of asserting on getAttribute",
52+
},
53+
],
54+
output:
55+
"expect(element).toHaveAttribute('foo', expect.stringContaining(`bar=${encodeURIComponent(baz.id)}`));",
56+
},
4557
{
4658
code: 'expect(element.getAttribute("foo")).toBe("bar")',
4759
errors: [

src/rules/prefer-to-have-attribute.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ export const create = (context) => ({
3030
node.parent.parent.property.range[0],
3131
node.parent.parent.parent.range[1],
3232
],
33-
`not.toHaveAttribute(${node.arguments[0].raw})`
33+
`not.toHaveAttribute(${context
34+
.getSourceCode()
35+
.getText(node.arguments[0])})`
3436
),
3537
],
3638
});
3739
},
3840
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](
3941
node
4042
) {
43+
const sourceCode = context.getSourceCode();
4144
context.report({
4245
node: node.parent,
4346
message: `Use toHaveAttribute instead of asserting on getAttribute`,
@@ -46,11 +49,11 @@ export const create = (context) => ({
4649
fixer.replaceText(node.parent.parent.property, "toHaveAttribute"),
4750
fixer.replaceText(
4851
node.parent.parent.parent.arguments[0],
49-
`${
50-
node.arguments[0].raw
51-
}, expect.string${node.parent.parent.property.name.slice(2)}ing(${
52-
node.parent.parent.parent.arguments[0].raw
53-
})`
52+
`${sourceCode.getText(
53+
node.arguments[0]
54+
)}, expect.string${node.parent.parent.property.name.slice(
55+
2
56+
)}ing(${sourceCode.getText(node.parent.parent.parent.arguments[0])})`
5457
),
5558
],
5659
});
@@ -62,18 +65,19 @@ export const create = (context) => ({
6265
const isNullOrEmpty =
6366
arg.length > 0 && (arg[0].value === null || arg[0].value === "");
6467

68+
const sourceCode = context.getSourceCode();
6569
context.report({
6670
node: node.parent,
6771
message: `Use toHaveAttribute instead of asserting on getAttribute`,
6872
fix: (fixer) => {
6973
const lastFixer = isNullOrEmpty
7074
? fixer.replaceText(
7175
node.parent.parent.parent.arguments[0],
72-
node.arguments[0].raw
76+
sourceCode.getText(node.arguments[0])
7377
)
7478
: fixer.insertTextBefore(
7579
node.parent.parent.parent.arguments[0],
76-
`${node.arguments[0].raw}, `
80+
`${sourceCode.getText(node.arguments[0])}, `
7781
);
7882

7983
return [
@@ -122,7 +126,7 @@ export const create = (context) => ({
122126
),
123127
fixer.replaceText(
124128
node.parent.parent.parent.arguments[0],
125-
node.arguments[0].raw
129+
context.getSourceCode().getText(node.arguments[0])
126130
),
127131
],
128132
});
@@ -148,7 +152,9 @@ export const create = (context) => ({
148152
],
149153
`${
150154
node.parent.parent.property.name === "toBeFalsy" ? "not." : ""
151-
}toHaveAttribute(${node.arguments[0].raw})`
155+
}toHaveAttribute(${context
156+
.getSourceCode()
157+
.getText(node.arguments[0])})`
152158
),
153159
],
154160
});

0 commit comments

Comments
 (0)