Skip to content

Commit a76d74c

Browse files
author
Ben Monro
committed
fix: fixed bug in prefer-focus with call expression in expect statement
1 parent 0c0dfec commit a76d74c

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

lib/rules/prefer-focus.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ module.exports = {
3939
node: node.parent,
4040
message: `Use toHaveFocus instead of checking activeElement`,
4141
fix(fixer) {
42-
return [
43-
fixer.replaceText(node, element.name),
44-
fixer.remove(element),
45-
fixer.replaceText(matcher, 'toHaveFocus'),
46-
];
42+
if (element.name) {
43+
return [
44+
fixer.replaceText(node, element.name),
45+
fixer.remove(element),
46+
fixer.replaceText(matcher, 'toHaveFocus'),
47+
];
48+
} else {
49+
return [
50+
fixer.removeRange([node.start, element.start]),
51+
fixer.insertTextAfterRange(
52+
[element.end, element.end + 1],
53+
'.not.toHaveFocus()'
54+
),
55+
];
56+
}
4757
},
4858
});
4959
},
@@ -77,6 +87,15 @@ module.exports = {
7787
node: node.parent,
7888
message: `Use toHaveFocus instead of checking activeElement`,
7989
fix(fixer) {
90+
if (!element.name) {
91+
return [
92+
fixer.removeRange([node.start, element.start]),
93+
fixer.insertTextAfterRange(
94+
[element.end, element.end + 1],
95+
'.toHaveFocus()'
96+
),
97+
];
98+
}
8099
return [
81100
fixer.replaceText(node, element.name),
82101
fixer.remove(element),

tests/lib/rules/prefer-focus.js

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ ruleTester.run('prefer-focus', rule, {
2424
],
2525
output: 'expect(foo).toHaveFocus()',
2626
},
27+
{
28+
code: `expect(document.activeElement).toBe(getByText('Foo'))`,
29+
errors: [
30+
{
31+
message: 'Use toHaveFocus instead of checking activeElement',
32+
},
33+
],
34+
output: `expect(getByText('Foo')).toHaveFocus()`,
35+
},
36+
{
37+
code: `expect(document.activeElement).not.toBe(getByText('Foo'))`,
38+
errors: [
39+
{
40+
message: 'Use toHaveFocus instead of checking activeElement',
41+
},
42+
],
43+
output: `expect(getByText('Foo')).not.toHaveFocus()`,
44+
},
2745
{
2846
code: 'expect(document.activeElement).not.toBe(foo)',
2947
errors: [

0 commit comments

Comments
 (0)