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