eslint-plugin-jest-dom 3.9.1 → 3.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assignment-ast.js +98 -0
- package/dist/createBannedAttributeRule.js +117 -0
- package/dist/index.js +51 -0
- package/dist/queries.js +11 -0
- package/dist/rules/prefer-checked.js +31 -0
- package/dist/rules/prefer-empty.js +119 -0
- package/dist/rules/prefer-enabled-disabled.js +31 -0
- package/dist/rules/prefer-focus.js +80 -0
- package/dist/rules/prefer-in-document.js +197 -0
- package/dist/rules/prefer-required.js +31 -0
- package/dist/rules/prefer-to-have-attribute.js +97 -0
- package/dist/rules/prefer-to-have-class.js +221 -0
- package/dist/rules/prefer-to-have-style.js +185 -0
- package/dist/rules/prefer-to-have-text-content.js +64 -0
- package/dist/rules/prefer-to-have-value.js +103 -0
- package/package.json +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.create = exports.meta = void 0;
|
|
7
|
+
|
|
8
|
+
var _queries = require("../queries");
|
|
9
|
+
|
|
10
|
+
var _assignmentAst = require("../assignment-ast");
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @fileoverview prefer toBeInTheDocument over checking getAttribute/hasAttribute
|
|
14
|
+
* @author Anton Niklasson
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/*eslint complexity: ["error", {"max": 20}]*/
|
|
18
|
+
const meta = {
|
|
19
|
+
type: "suggestion",
|
|
20
|
+
docs: {
|
|
21
|
+
category: "Best Practices",
|
|
22
|
+
description: "Prefer .toBeInTheDocument() for asserting the existence of a DOM node",
|
|
23
|
+
url: "prefer-in-document",
|
|
24
|
+
recommended: true
|
|
25
|
+
},
|
|
26
|
+
fixable: "code",
|
|
27
|
+
messages: {
|
|
28
|
+
"use-document": `Prefer .toBeInTheDocument() for asserting DOM node existence`
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
exports.meta = meta;
|
|
32
|
+
|
|
33
|
+
function isAntonymMatcher(matcherNode, matcherArguments) {
|
|
34
|
+
return matcherNode.name === "toBeNull" || matcherNode.name === "toBeFalsy" || usesToBeOrToEqualWithNull(matcherNode, matcherArguments) || usesToHaveLengthZero(matcherNode, matcherArguments);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) {
|
|
38
|
+
return (matcherNode.name === "toBe" || matcherNode.name === "toEqual") && matcherArguments[0].value === null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function usesToHaveLengthZero(matcherNode, matcherArguments) {
|
|
42
|
+
return matcherNode.name === "toHaveLength" && matcherArguments[0].value === 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const create = context => {
|
|
46
|
+
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
|
|
47
|
+
|
|
48
|
+
function getLengthValue(matcherArguments) {
|
|
49
|
+
let lengthValue;
|
|
50
|
+
|
|
51
|
+
if (matcherArguments[0].type === "Identifier") {
|
|
52
|
+
const assignment = (0, _assignmentAst.getAssignmentForIdentifier)(context, matcherArguments[0].name);
|
|
53
|
+
|
|
54
|
+
if (!assignment) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
lengthValue = assignment.value;
|
|
59
|
+
} else if (matcherArguments[0].type === "Literal") {
|
|
60
|
+
lengthValue = matcherArguments[0].value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return lengthValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function check({
|
|
67
|
+
queryNode,
|
|
68
|
+
matcherNode,
|
|
69
|
+
matcherArguments,
|
|
70
|
+
negatedMatcher,
|
|
71
|
+
expect
|
|
72
|
+
}) {
|
|
73
|
+
// only report on dom nodes which we can resolve to RTL queries.
|
|
74
|
+
if (!queryNode || !queryNode.name && !queryNode.property) return; // toHaveLength() is only invalid with 0 or 1
|
|
75
|
+
|
|
76
|
+
if (matcherNode.name === "toHaveLength" && matcherArguments.length) {
|
|
77
|
+
const lengthValue = getLengthValue(matcherArguments); // isNotToHaveLengthZero represents .not.toHaveLength(0) which is a valid use of toHaveLength
|
|
78
|
+
|
|
79
|
+
const isNotToHaveLengthZero = usesToHaveLengthZero(matcherNode, matcherArguments) && negatedMatcher;
|
|
80
|
+
const isValidUseOfToHaveLength = isNotToHaveLengthZero || !["Literal", "Identifier"].includes(matcherArguments[0].type) || lengthValue === undefined || lengthValue > 1;
|
|
81
|
+
|
|
82
|
+
if (isValidUseOfToHaveLength) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
} // toBe() or toEqual() are only invalid with null
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if (matcherNode.name === "toBe" || matcherNode.name === "toEqual") {
|
|
89
|
+
if (!matcherArguments.length || !usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const query = queryNode.name || queryNode.property.name;
|
|
95
|
+
|
|
96
|
+
if (_queries.queries.includes(query)) {
|
|
97
|
+
context.report({
|
|
98
|
+
node: matcherNode,
|
|
99
|
+
messageId: "use-document",
|
|
100
|
+
loc: matcherNode.loc,
|
|
101
|
+
|
|
102
|
+
fix(fixer) {
|
|
103
|
+
const operations = []; // Remove any arguments in the matcher
|
|
104
|
+
|
|
105
|
+
for (const argument of Array.from(matcherArguments)) {
|
|
106
|
+
operations.push(fixer.remove(argument));
|
|
107
|
+
} // AllBy should not be used with toBeInTheDocument
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
operations.push(fixer.replaceText(queryNode.property || queryNode, query.replace("All", ""))); // Flip the .not if necessary
|
|
111
|
+
|
|
112
|
+
if (isAntonymMatcher(matcherNode, matcherArguments)) {
|
|
113
|
+
if (negatedMatcher) {
|
|
114
|
+
operations.push(fixer.replaceTextRange([expect.range[1], matcherNode.range[1]], ".toBeInTheDocument"));
|
|
115
|
+
return operations;
|
|
116
|
+
} else {
|
|
117
|
+
operations.push(fixer.insertTextBefore(matcherNode, "not."));
|
|
118
|
+
}
|
|
119
|
+
} // Replace the actual matcher
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
operations.push(fixer.replaceText(matcherNode, "toBeInTheDocument"));
|
|
123
|
+
return operations;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
// expect(<query>).not.<matcher>
|
|
132
|
+
[`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.property.name='not'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`](node) {
|
|
133
|
+
if (!node.callee.object.object.arguments.length) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const arg = node.callee.object.object.arguments[0];
|
|
138
|
+
const queryNode = arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
|
|
139
|
+
const matcherNode = node.callee.property;
|
|
140
|
+
const matcherArguments = node.arguments;
|
|
141
|
+
const expect = node.callee.object.object;
|
|
142
|
+
check({
|
|
143
|
+
negatedMatcher: true,
|
|
144
|
+
queryNode,
|
|
145
|
+
matcherNode,
|
|
146
|
+
matcherArguments,
|
|
147
|
+
expect
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
// // const foo = <query> expect(foo).not.<matcher>
|
|
152
|
+
[`MemberExpression[object.object.callee.name=expect][object.property.name=not][property.name=${alternativeMatchers}][object.object.arguments.0.type=Identifier]`](node) {
|
|
153
|
+
const queryNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.object.arguments[0].name);
|
|
154
|
+
const matcherNode = node.property;
|
|
155
|
+
const matcherArguments = node.parent.arguments;
|
|
156
|
+
const expect = node.object.object;
|
|
157
|
+
check({
|
|
158
|
+
negatedMatcher: true,
|
|
159
|
+
queryNode: queryNode && queryNode.callee || queryNode,
|
|
160
|
+
matcherNode,
|
|
161
|
+
matcherArguments,
|
|
162
|
+
expect
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
// const foo = <query> expect(foo).<matcher>
|
|
167
|
+
[`MemberExpression[object.callee.name=expect][property.name=${alternativeMatchers}][object.arguments.0.type=Identifier]`](node) {
|
|
168
|
+
const queryNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.arguments[0].name);
|
|
169
|
+
const matcherNode = node.property;
|
|
170
|
+
const matcherArguments = node.parent.arguments;
|
|
171
|
+
check({
|
|
172
|
+
negatedMatcher: false,
|
|
173
|
+
queryNode: queryNode && queryNode.callee || queryNode,
|
|
174
|
+
matcherNode,
|
|
175
|
+
matcherArguments
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
// expect(await <query>).<matcher>
|
|
180
|
+
// expect(<query>).<matcher>
|
|
181
|
+
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`](node) {
|
|
182
|
+
const arg = node.callee.object.arguments[0];
|
|
183
|
+
const queryNode = arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
|
|
184
|
+
const matcherNode = node.callee.property;
|
|
185
|
+
const matcherArguments = node.arguments;
|
|
186
|
+
check({
|
|
187
|
+
negatedMatcher: false,
|
|
188
|
+
queryNode,
|
|
189
|
+
matcherNode,
|
|
190
|
+
matcherArguments
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
exports.create = create;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.create = exports.meta = void 0;
|
|
9
|
+
|
|
10
|
+
var _createBannedAttributeRule = _interopRequireDefault(require("../createBannedAttributeRule"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks
|
|
14
|
+
* @author Ben Monro
|
|
15
|
+
*/
|
|
16
|
+
const meta = {
|
|
17
|
+
docs: {
|
|
18
|
+
description: "prefer toBeRequired over checking properties",
|
|
19
|
+
category: "Best Practices",
|
|
20
|
+
recommended: true,
|
|
21
|
+
url: "prefer-required"
|
|
22
|
+
},
|
|
23
|
+
fixable: "code"
|
|
24
|
+
};
|
|
25
|
+
exports.meta = meta;
|
|
26
|
+
const create = (0, _createBannedAttributeRule.default)({
|
|
27
|
+
preferred: "toBeRequired",
|
|
28
|
+
negatedPreferred: "not.toBeRequired",
|
|
29
|
+
attributes: ["required", "aria-required"]
|
|
30
|
+
});
|
|
31
|
+
exports.create = create;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.create = exports.meta = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute
|
|
10
|
+
* @author Ben Monro
|
|
11
|
+
*/
|
|
12
|
+
//------------------------------------------------------------------------------
|
|
13
|
+
// Rule Definition
|
|
14
|
+
//------------------------------------------------------------------------------
|
|
15
|
+
const meta = {
|
|
16
|
+
docs: {
|
|
17
|
+
category: "Best Practices",
|
|
18
|
+
description: "prefer toHaveAttribute over checking getAttribute/hasAttribute ",
|
|
19
|
+
url: "prefer-to-have-attribute",
|
|
20
|
+
recommended: true
|
|
21
|
+
},
|
|
22
|
+
fixable: "code"
|
|
23
|
+
};
|
|
24
|
+
exports.meta = meta;
|
|
25
|
+
|
|
26
|
+
const create = context => ({
|
|
27
|
+
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull/]`](node) {
|
|
28
|
+
context.report({
|
|
29
|
+
node: node.parent,
|
|
30
|
+
message: `Use toHaveAttribute instead of asserting on getAttribute`,
|
|
31
|
+
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `not.toHaveAttribute(${context.getSourceCode().getText(node.arguments[0])})`)]
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`](node) {
|
|
36
|
+
const sourceCode = context.getSourceCode();
|
|
37
|
+
context.report({
|
|
38
|
+
node: node.parent,
|
|
39
|
+
message: `Use toHaveAttribute instead of asserting on getAttribute`,
|
|
40
|
+
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, "toHaveAttribute"), fixer.replaceText(node.parent.parent.parent.arguments[0], `${sourceCode.getText(node.arguments[0])}, expect.string${node.parent.parent.property.name.slice(2)}ing(${sourceCode.getText(node.parent.parent.parent.arguments[0])})`)]
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
|
|
45
|
+
const arg = node.parent.parent.parent.arguments;
|
|
46
|
+
const isNull = arg.length > 0 && arg[0].value === null;
|
|
47
|
+
const sourceCode = context.getSourceCode();
|
|
48
|
+
context.report({
|
|
49
|
+
node: node.parent,
|
|
50
|
+
message: `Use toHaveAttribute instead of asserting on getAttribute`,
|
|
51
|
+
fix: fixer => {
|
|
52
|
+
const lastFixer = isNull ? fixer.replaceText(node.parent.parent.parent.arguments[0], sourceCode.getText(node.arguments[0])) : fixer.insertTextBefore(node.parent.parent.parent.arguments[0], `${sourceCode.getText(node.arguments[0])}, `);
|
|
53
|
+
return [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, `${isNull ? "not." : ""}toHaveAttribute`), lastFixer];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
[`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull|toBeUndefined|toBeDefined/]`](node) {
|
|
59
|
+
context.report({
|
|
60
|
+
node: node.parent.parent.property,
|
|
61
|
+
message: "Invalid matcher for hasAttribute"
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
[`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeUndefined|toBeDefined/]`](node) {
|
|
66
|
+
context.report({
|
|
67
|
+
node: node.parent.parent.property,
|
|
68
|
+
message: "Invalid matcher for getAttribute"
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
[`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`](node) {
|
|
73
|
+
if (typeof node.parent.parent.parent.arguments[0].value === "boolean") {
|
|
74
|
+
context.report({
|
|
75
|
+
node: node.parent,
|
|
76
|
+
message: `Use toHaveAttribute instead of asserting on hasAttribute`,
|
|
77
|
+
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceText(node.parent.parent.property, `${node.parent.parent.parent.arguments[0].value === false ? "not." : ""}toHaveAttribute`), fixer.replaceText(node.parent.parent.parent.arguments[0], context.getSourceCode().getText(node.arguments[0]))]
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
context.report({
|
|
81
|
+
node: node.parent.parent.property,
|
|
82
|
+
message: "Invalid matcher for hasAttribute"
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
[`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeTruthy|toBeFalsy/]`](node) {
|
|
88
|
+
context.report({
|
|
89
|
+
node: node.parent,
|
|
90
|
+
message: `Use toHaveAttribute instead of asserting on hasAttribute`,
|
|
91
|
+
fix: fixer => [fixer.removeRange([node.callee.object.range[1], node.range[1]]), fixer.replaceTextRange([node.parent.parent.property.range[0], node.parent.parent.parent.range[1]], `${node.parent.parent.property.name === "toBeFalsy" ? "not." : ""}toHaveAttribute(${context.getSourceCode().getText(node.arguments[0])})`)]
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
exports.create = create;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.create = exports.meta = void 0;
|
|
7
|
+
|
|
8
|
+
var _assignmentAst = require("../assignment-ast");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @fileoverview prefer toHaveClass over checking element className
|
|
12
|
+
* @author Ben Monro
|
|
13
|
+
*/
|
|
14
|
+
//------------------------------------------------------------------------------
|
|
15
|
+
// Rule Definition
|
|
16
|
+
//------------------------------------------------------------------------------
|
|
17
|
+
const messageId = "use-to-have-class";
|
|
18
|
+
const meta = {
|
|
19
|
+
docs: {
|
|
20
|
+
category: "Best Practices",
|
|
21
|
+
url: "prefer-to-have-class",
|
|
22
|
+
description: "prefer toHaveClass over checking element className",
|
|
23
|
+
recommended: true
|
|
24
|
+
},
|
|
25
|
+
messages: {
|
|
26
|
+
[messageId]: `Prefer .toHaveClass() over checking element className`
|
|
27
|
+
},
|
|
28
|
+
fixable: "code"
|
|
29
|
+
};
|
|
30
|
+
exports.meta = meta;
|
|
31
|
+
|
|
32
|
+
const create = context => ({
|
|
33
|
+
//expect(el.classList.contains("foo")).toBe(true)
|
|
34
|
+
[`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.callee.object.property.name=classList][callee.object.arguments.0.callee.property.name=contains][callee.property.name=/toBe(Truthy|Falsy)?|to(Strict)?Equal/]`](node) {
|
|
35
|
+
const classValue = node.callee.object.arguments[0].arguments[0];
|
|
36
|
+
const checkedProp = node.callee.object.arguments[0].callee.object.object;
|
|
37
|
+
const matcher = node.callee.property;
|
|
38
|
+
const [matcherArg] = node.arguments;
|
|
39
|
+
const [expectArg] = node.callee.object.arguments;
|
|
40
|
+
const isTruthy = matcher.name === "toBe" && matcherArg.value === true || matcher.name === "toBeTruthy";
|
|
41
|
+
context.report({
|
|
42
|
+
node: matcher,
|
|
43
|
+
messageId,
|
|
44
|
+
|
|
45
|
+
fix(fixer) {
|
|
46
|
+
return [fixer.removeRange([checkedProp.range[1], expectArg.range[1]]), fixer.replaceText(matcher, `${isTruthy ? "" : "not."}toHaveClass`), matcherArg ? fixer.replaceText(matcherArg, context.getSourceCode().getText(classValue)) : fixer.insertTextBefore(context.getSourceCode().getTokenAfter(matcher, {
|
|
47
|
+
skip: 1
|
|
48
|
+
}), context.getSourceCode().getText(classValue))];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
//expect(el.classList[0]).toBe("bar")
|
|
55
|
+
[`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.object.property.name=classList][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
|
|
56
|
+
const [classValue] = node.arguments;
|
|
57
|
+
const matcher = node.callee.property;
|
|
58
|
+
const classNameProp = node.callee.object.arguments[0].object;
|
|
59
|
+
const expectArg = node.callee.object.arguments[0];
|
|
60
|
+
context.report({
|
|
61
|
+
node: matcher,
|
|
62
|
+
messageId,
|
|
63
|
+
|
|
64
|
+
fix(fixer) {
|
|
65
|
+
//can't autofix here as it toHaveClass doesn't have a partial matcher / regex for class names.
|
|
66
|
+
if (matcher.name === "toContain") return;
|
|
67
|
+
return [fixer.removeRange([classNameProp.object.range[1], expectArg.range[1]]), fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classValue, context.getSourceCode().getText(classValue))];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
//expect(el.classList[0]).not.toBe("bar")
|
|
74
|
+
[`CallExpression[callee.object.object.callee.name=expect][callee.object.object.arguments.0.object.property.name=classList][callee.object.property.name=not][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
|
|
75
|
+
//can't autofix this case because the class could be in another element of the classList array.
|
|
76
|
+
context.report({
|
|
77
|
+
node,
|
|
78
|
+
messageId
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
//expect(el.className | el.classList).toBe("bar") / toStrict?Equal / toContain
|
|
83
|
+
[`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.property.name=/class(Name|List)/][callee.property.name=/toBe$|to(Strict)?Equal|toContain/]`](node) {
|
|
84
|
+
const checkedProp = node.callee.object.arguments[0].property;
|
|
85
|
+
const [classValue] = node.arguments;
|
|
86
|
+
const matcher = node.callee.property;
|
|
87
|
+
const classNameProp = node.callee.object.arguments[0].object;
|
|
88
|
+
const {
|
|
89
|
+
isDTLQuery
|
|
90
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
|
|
91
|
+
if (!isDTLQuery) return; // don't report here if using `expect.foo()`
|
|
92
|
+
|
|
93
|
+
if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") return;
|
|
94
|
+
context.report({
|
|
95
|
+
node: matcher,
|
|
96
|
+
messageId,
|
|
97
|
+
|
|
98
|
+
fix(fixer) {
|
|
99
|
+
if (checkedProp.name === "classList" && matcher.name !== "toContain") return;
|
|
100
|
+
return [fixer.removeRange([classNameProp.range[1], checkedProp.range[1]]), fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classValue, `${context.getSourceCode().getText(classValue)}${matcher.name === "toContain" ? "" : ", { exact: true }"}`)];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
//expect(el.className | el.classList).toEqual(expect.stringContaining("foo") | objectContaining) / toStrictEqual
|
|
107
|
+
[`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.property.name=/class(Name|List)/][callee.property.name=/to(Strict)?Equal/][arguments.0.callee.object.name=expect]`](node) {
|
|
108
|
+
const className = node.callee.object.arguments[0].property;
|
|
109
|
+
const [classValue] = node.arguments[0].arguments;
|
|
110
|
+
const matcher = node.callee.property;
|
|
111
|
+
const classNameProp = node.callee.object.arguments[0].object;
|
|
112
|
+
const matcherArg = node.arguments[0].callee.property;
|
|
113
|
+
const {
|
|
114
|
+
isDTLQuery
|
|
115
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
|
|
116
|
+
if (!isDTLQuery) return;
|
|
117
|
+
context.report({
|
|
118
|
+
node: matcher,
|
|
119
|
+
messageId,
|
|
120
|
+
|
|
121
|
+
fix(fixer) {
|
|
122
|
+
if (matcherArg.name !== "stringContaining") return;
|
|
123
|
+
return [fixer.removeRange([classNameProp.range[1], className.range[1]]), fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(node.arguments[0], `${context.getSourceCode().getText(classValue)}`)];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
//expect(screen.getByRole("button").className | classList).not.toBe("foo"); / toStrict?Equal / toContain
|
|
130
|
+
[`CallExpression[callee.object.object.callee.name=expect][callee.object.object.arguments.0.property.name=/class(Name|List)/][callee.object.property.name=not][callee.property.name=/toBe$|to(Strict)?Equal|toContain/]`](node) {
|
|
131
|
+
const className = node.callee.object.object.arguments[0].property;
|
|
132
|
+
const [classValue] = node.arguments;
|
|
133
|
+
const matcher = node.callee.property;
|
|
134
|
+
const classNameProp = node.callee.object.object.arguments[0].object;
|
|
135
|
+
const {
|
|
136
|
+
isDTLQuery
|
|
137
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
|
|
138
|
+
if (!isDTLQuery) return;
|
|
139
|
+
context.report({
|
|
140
|
+
node: matcher,
|
|
141
|
+
messageId,
|
|
142
|
+
|
|
143
|
+
fix(fixer) {
|
|
144
|
+
if (className.name === "classList" && matcher.name !== "toContain") return;
|
|
145
|
+
return [fixer.removeRange([classNameProp.range[1], className.range[1]]), fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classValue, `${context.getSourceCode().getText(classValue)}${matcher.name === "toContain" ? "" : ", { exact: true }"}`)];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
//expect(el).toHaveProperty("className", "foo: bar");
|
|
152
|
+
//expect(el).toHaveAttribute("class", "foo: bar");
|
|
153
|
+
[[`CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveAttribute][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`, `CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveProperty][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`].join(",")](node) {
|
|
154
|
+
const matcher = node.callee.property;
|
|
155
|
+
const [classArg, classValueArg] = node.arguments;
|
|
156
|
+
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
|
|
157
|
+
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
|
|
158
|
+
const {
|
|
159
|
+
isDTLQuery
|
|
160
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
161
|
+
if (!isDTLQuery) return;
|
|
162
|
+
context.report({
|
|
163
|
+
node: matcher,
|
|
164
|
+
messageId,
|
|
165
|
+
|
|
166
|
+
fix(fixer) {
|
|
167
|
+
return [fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classArg, context.getSourceCode().getText(classValueArg)), fixer.replaceText(classValueArg, `{ exact: true }`)];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
//expect(el).not.toHaveAttribute("class", "foo: bar");
|
|
174
|
+
//expect(el).not.toHaveProperty("className", "foo: bar");
|
|
175
|
+
[[`CallExpression[callee.object.object.callee.name=expect][callee.object.property.name=not][callee.property.name=toHaveAttribute][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`, `CallExpression[callee.object.object.callee.name=expect][callee.object.property.name=not][callee.property.name=toHaveProperty][arguments.0.type=/Literal/][arguments.1.type=/Literal$/]`].join(",")](node) {
|
|
176
|
+
//[callee.object.property.name=/toHaveAttribute|toHaveProperty/][arguments.0.value=class][arguments.1.type=/Literal$/]
|
|
177
|
+
const matcher = node.callee.property;
|
|
178
|
+
const [classArg, classValueArg] = node.arguments;
|
|
179
|
+
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
|
|
180
|
+
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
|
|
181
|
+
const {
|
|
182
|
+
isDTLQuery
|
|
183
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.object.arguments[0]);
|
|
184
|
+
if (!isDTLQuery) return;
|
|
185
|
+
context.report({
|
|
186
|
+
node: matcher,
|
|
187
|
+
messageId,
|
|
188
|
+
|
|
189
|
+
fix(fixer) {
|
|
190
|
+
return [fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classArg, context.getSourceCode().getText(classValueArg)), fixer.replaceText(classValueArg, `{ exact: true }`)];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
//expect(el).toHaveProperty(`className`, expect.stringContaining("foo"));
|
|
197
|
+
//expect(el).toHaveAttribute(`class`, expect.stringContaining("foo"));
|
|
198
|
+
[[`CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveAttribute][arguments.0.type=/Literal/][arguments.1.callee.object.name=expect][arguments.1.callee.property.name=stringContaining]`, `CallExpression[callee.object.callee.name=expect][callee.property.name=toHaveProperty][arguments.0.type=/Literal/][arguments.1.callee.object.name=expect][arguments.1.callee.property.name=stringContaining]`].join(",")](node) {
|
|
199
|
+
const matcher = node.callee.property;
|
|
200
|
+
const [classArg, classValue] = node.arguments;
|
|
201
|
+
const classValueArg = classValue.arguments[0];
|
|
202
|
+
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
|
|
203
|
+
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
|
|
204
|
+
const {
|
|
205
|
+
isDTLQuery
|
|
206
|
+
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
207
|
+
if (!isDTLQuery) return;
|
|
208
|
+
context.report({
|
|
209
|
+
node: matcher,
|
|
210
|
+
messageId,
|
|
211
|
+
|
|
212
|
+
fix(fixer) {
|
|
213
|
+
return [fixer.replaceText(matcher, "toHaveClass"), fixer.replaceText(classArg, context.getSourceCode().getText(classValueArg)), fixer.removeRange([classArg.range[1], classValue.range[1]])];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
exports.create = create;
|