Skip to content

fix(prefer-to-have-attribute): Handle case where matcher value is empty string #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
54 changes: 54 additions & 0 deletions src/__tests__/lib/rules/prefer-to-have-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ ruleTester.run("prefer-to-have-attribute", rule, {
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", expect.stringMatching(/foo/))`,
},
{
code: `expect(getByText("yes").getAttribute("data-blah")).toBe("")`,
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
},
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", "")`,
},
{
code: `expect(getByText("yes").getAttribute("data-blah")).toBe('')`,
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
},
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", '')`,
},
{
code: `expect(getByText('foo').hasAttribute('foo')).toBe(null)`,
errors: [
Expand Down Expand Up @@ -171,6 +189,24 @@ ruleTester.run("prefer-to-have-attribute", rule, {
],
output: 'expect(element).toHaveAttribute("foo", "bar")',
},
{
code: `expect(getByText("yes").getAttribute("data-blah")).toEqual("")`,
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
},
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", "")`,
},
{
code: `expect(getByText("yes").getAttribute("data-blah")).toEqual('')`,
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
},
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", '')`,
},
{
code: 'expect(element.getAttribute("foo")).toStrictEqual("bar")',
errors: [
Expand All @@ -180,6 +216,24 @@ ruleTester.run("prefer-to-have-attribute", rule, {
],
output: 'expect(element).toHaveAttribute("foo", "bar")',
},
{
code: `expect(getByText("yes").getAttribute("data-blah")).toStrictEqual("")`,
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
},
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", "")`,
},
{
code: `expect(getByText("yes").getAttribute("data-blah")).toStrictEqual('')`,
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
},
],
output: `expect(getByText("yes")).toHaveAttribute("data-blah", '')`,
},
{
code: 'expect(element.getAttribute("foo")).toBe(null)',
errors: [
Expand Down
7 changes: 3 additions & 4 deletions src/rules/prefer-to-have-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ export const create = (context) => ({
node
) {
const arg = node.parent.parent.parent.arguments;
const isNullOrEmpty =
arg.length > 0 && (arg[0].value === null || arg[0].value === "");
const isNull = arg.length > 0 && arg[0].value === null;

const sourceCode = context.getSourceCode();
context.report({
node: node.parent,
message: `Use toHaveAttribute instead of asserting on getAttribute`,
fix: (fixer) => {
const lastFixer = isNullOrEmpty
const lastFixer = isNull
? fixer.replaceText(
node.parent.parent.parent.arguments[0],
sourceCode.getText(node.arguments[0])
Expand All @@ -86,7 +85,7 @@ export const create = (context) => ({
fixer.removeRange([node.callee.object.range[1], node.range[1]]),
fixer.replaceText(
node.parent.parent.property,
`${isNullOrEmpty ? "not." : ""}toHaveAttribute`
`${isNull ? "not." : ""}toHaveAttribute`
),
lastFixer,
];
Expand Down