Skip to content

Commit 1e20f0c

Browse files
mdotwillsMatthew WillsMichaelDeBoeybenmonro
authored
fix(prefer-to-have-style): Handle case where matcher toBe contains empty string value (#180)
This change purposefully treats an empty string value for certain matchers as value instead of as null. Prior to this change, the fixer was incorrectly inferring a .not. Matchers affected are: - toBe - toStrictEqual - toEqual Co-authored-by: Matthew Wills <[email protected]> Co-authored-by: Michaël De Boey <[email protected]> Co-authored-by: Ben Monro <[email protected]>
1 parent 6bc032d commit 1e20f0c

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

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

+54
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ ruleTester.run("prefer-to-have-attribute", rule, {
7272
],
7373
output: `expect(getByText("yes")).toHaveAttribute("data-blah", expect.stringMatching(/foo/))`,
7474
},
75+
{
76+
code: `expect(getByText("yes").getAttribute("data-blah")).toBe("")`,
77+
errors: [
78+
{
79+
message: "Use toHaveAttribute instead of asserting on getAttribute",
80+
},
81+
],
82+
output: `expect(getByText("yes")).toHaveAttribute("data-blah", "")`,
83+
},
84+
{
85+
code: `expect(getByText("yes").getAttribute("data-blah")).toBe('')`,
86+
errors: [
87+
{
88+
message: "Use toHaveAttribute instead of asserting on getAttribute",
89+
},
90+
],
91+
output: `expect(getByText("yes")).toHaveAttribute("data-blah", '')`,
92+
},
7593
{
7694
code: `expect(getByText('foo').hasAttribute('foo')).toBe(null)`,
7795
errors: [
@@ -171,6 +189,24 @@ ruleTester.run("prefer-to-have-attribute", rule, {
171189
],
172190
output: 'expect(element).toHaveAttribute("foo", "bar")',
173191
},
192+
{
193+
code: `expect(getByText("yes").getAttribute("data-blah")).toEqual("")`,
194+
errors: [
195+
{
196+
message: "Use toHaveAttribute instead of asserting on getAttribute",
197+
},
198+
],
199+
output: `expect(getByText("yes")).toHaveAttribute("data-blah", "")`,
200+
},
201+
{
202+
code: `expect(getByText("yes").getAttribute("data-blah")).toEqual('')`,
203+
errors: [
204+
{
205+
message: "Use toHaveAttribute instead of asserting on getAttribute",
206+
},
207+
],
208+
output: `expect(getByText("yes")).toHaveAttribute("data-blah", '')`,
209+
},
174210
{
175211
code: 'expect(element.getAttribute("foo")).toStrictEqual("bar")',
176212
errors: [
@@ -180,6 +216,24 @@ ruleTester.run("prefer-to-have-attribute", rule, {
180216
],
181217
output: 'expect(element).toHaveAttribute("foo", "bar")',
182218
},
219+
{
220+
code: `expect(getByText("yes").getAttribute("data-blah")).toStrictEqual("")`,
221+
errors: [
222+
{
223+
message: "Use toHaveAttribute instead of asserting on getAttribute",
224+
},
225+
],
226+
output: `expect(getByText("yes")).toHaveAttribute("data-blah", "")`,
227+
},
228+
{
229+
code: `expect(getByText("yes").getAttribute("data-blah")).toStrictEqual('')`,
230+
errors: [
231+
{
232+
message: "Use toHaveAttribute instead of asserting on getAttribute",
233+
},
234+
],
235+
output: `expect(getByText("yes")).toHaveAttribute("data-blah", '')`,
236+
},
183237
{
184238
code: 'expect(element.getAttribute("foo")).toBe(null)',
185239
errors: [

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,14 @@ export const create = (context) => ({
6464
node
6565
) {
6666
const arg = node.parent.parent.parent.arguments;
67-
const isNullOrEmpty =
68-
arg.length > 0 && (arg[0].value === null || arg[0].value === "");
67+
const isNull = arg.length > 0 && arg[0].value === null;
6968

7069
const sourceCode = context.getSourceCode();
7170
context.report({
7271
node: node.parent,
7372
message: `Use toHaveAttribute instead of asserting on getAttribute`,
7473
fix: (fixer) => {
75-
const lastFixer = isNullOrEmpty
74+
const lastFixer = isNull
7675
? fixer.replaceText(
7776
node.parent.parent.parent.arguments[0],
7877
sourceCode.getText(node.arguments[0])
@@ -86,7 +85,7 @@ export const create = (context) => ({
8685
fixer.removeRange([node.callee.object.range[1], node.range[1]]),
8786
fixer.replaceText(
8887
node.parent.parent.property,
89-
`${isNullOrEmpty ? "not." : ""}toHaveAttribute`
88+
`${isNull ? "not." : ""}toHaveAttribute`
9089
),
9190
lastFixer,
9291
];

0 commit comments

Comments
 (0)