eslint-plugin-jest-dom 5.7.0 → 5.8.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.
|
@@ -8,11 +8,32 @@ var _assignmentAst = require("./assignment-ast");
|
|
|
8
8
|
var _default = ({
|
|
9
9
|
preferred,
|
|
10
10
|
negatedPreferred,
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
mixedPreferred,
|
|
12
|
+
attributes
|
|
13
13
|
}) => context => {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Returns the value of the second argument of the given node, if it's a string literal
|
|
16
|
+
* or otherwise null
|
|
17
|
+
*
|
|
18
|
+
* @param node
|
|
19
|
+
*
|
|
20
|
+
* @return {string | null}
|
|
21
|
+
*/
|
|
22
|
+
const findSecondStringLiteralArgumentValue = node => node.arguments.length >= 2 && node.arguments[1].type === "Literal" && typeof node.arguments[1].value === "string" && node.arguments[1].value || null;
|
|
23
|
+
const getCorrectFunctionFor = (node, negated = false) => {
|
|
24
|
+
const value = findSecondStringLiteralArgumentValue(node);
|
|
25
|
+
const isMixed = mixedPreferred && (value || "").toLowerCase() === "mixed";
|
|
26
|
+
if (isMixed) {
|
|
27
|
+
return negated ? `not.${mixedPreferred}` : mixedPreferred;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// use the negated preference if we're _not_ using a truthy value,
|
|
31
|
+
// or we have been told we're negated // todo: this might be a bug?
|
|
32
|
+
if (negated || !(node.arguments.length === 1 || node.arguments[1].value === true || node.arguments[1].type !== "Literal" || (value || "").toLowerCase() === "true" || node.arguments[1].value === "")) {
|
|
33
|
+
return negatedPreferred;
|
|
34
|
+
}
|
|
35
|
+
return preferred;
|
|
36
|
+
};
|
|
16
37
|
const isBannedArg = node => node.arguments.length && attributes.some(attr => attr === node.arguments[0].value);
|
|
17
38
|
|
|
18
39
|
//expect(el).not.toBeEnabled() => expect(el).toBeDisabled()
|
|
@@ -64,14 +85,14 @@ var _default = ({
|
|
|
64
85
|
if (!isBannedArg(node)) {
|
|
65
86
|
return;
|
|
66
87
|
}
|
|
67
|
-
if (isExcludedValue(node)) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
const arg = node.arguments[0].value;
|
|
71
88
|
const correctFunction = getCorrectFunctionFor(node, true);
|
|
72
89
|
const incorrectFunction = node.callee.property.name;
|
|
90
|
+
const message = `Use ${correctFunction}() instead of not.${incorrectFunction}(${node.arguments.map(({
|
|
91
|
+
raw,
|
|
92
|
+
name
|
|
93
|
+
}) => raw || name).join(", ")})`;
|
|
73
94
|
context.report({
|
|
74
|
-
message
|
|
95
|
+
message,
|
|
75
96
|
node,
|
|
76
97
|
fix: fixer => fixer.replaceTextRange([node.callee.object.property.range[0], node.range[1]], `${correctFunction}()`)
|
|
77
98
|
});
|
|
@@ -80,9 +101,6 @@ var _default = ({
|
|
|
80
101
|
if (!isBannedArg(node)) {
|
|
81
102
|
return;
|
|
82
103
|
}
|
|
83
|
-
if (isExcludedValue(node)) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
104
|
const {
|
|
87
105
|
isDTLQuery
|
|
88
106
|
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
|
|
@@ -23,6 +23,6 @@ const meta = exports.meta = {
|
|
|
23
23
|
const create = exports.create = (0, _createBannedAttributeRule.default)({
|
|
24
24
|
preferred: "toBeChecked",
|
|
25
25
|
negatedPreferred: "not.toBeChecked",
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
mixedPreferred: "toBePartiallyChecked",
|
|
27
|
+
attributes: ["checked", "aria-checked"]
|
|
28
28
|
});
|
|
@@ -23,6 +23,6 @@ const meta = exports.meta = {
|
|
|
23
23
|
const create = exports.create = (0, _createBannedAttributeRule.default)({
|
|
24
24
|
preferred: "toBePressed",
|
|
25
25
|
negatedPreferred: "not.toBePressed",
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
mixedPreferred: "toBePartiallyPressed",
|
|
27
|
+
attributes: ["aria-pressed"]
|
|
28
28
|
});
|
|
@@ -55,6 +55,23 @@ const create = context => {
|
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
|
+
// expect(element['aria-valuenow']).toBe('foo') / toEqual / toStrictEqual
|
|
59
|
+
// expect(<query>['aria-valuenow']).toBe('foo') / toEqual / toStrictEqual
|
|
60
|
+
// expect((await <query>)['aria-valuenow']).toBe('foo') / toEqual / toStrictEqual
|
|
61
|
+
[`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.arguments.0.property.value='aria-valuenow'][callee.object.callee.name=expect]`](node) {
|
|
62
|
+
const valueProp = node.callee.object.arguments[0].property;
|
|
63
|
+
const matcher = node.callee.property;
|
|
64
|
+
const queryNode = node.callee.object.arguments[0].object;
|
|
65
|
+
if (isValidQueryNode(queryNode)) {
|
|
66
|
+
context.report({
|
|
67
|
+
messageId,
|
|
68
|
+
node,
|
|
69
|
+
fix(fixer) {
|
|
70
|
+
return [fixer.remove((0, _context.getSourceCode)(context).getTokenBefore(valueProp)), fixer.remove((0, _context.getSourceCode)(context).getTokenAfter(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
},
|
|
58
75
|
// expect(element.value).not.toBe('foo') / toEqual / toStrictEqual
|
|
59
76
|
// expect(<query>.value).not.toBe('foo') / toEqual / toStrictEqual
|
|
60
77
|
// expect((await <query>).value).not.toBe('foo') / toEqual / toStrictEqual
|
|
@@ -72,8 +89,26 @@ const create = context => {
|
|
|
72
89
|
});
|
|
73
90
|
}
|
|
74
91
|
},
|
|
92
|
+
// expect(element['aria-valuenow']).not.toBe('foo') / toEqual / toStrictEqual
|
|
93
|
+
// expect(<query>['aria-valuenow']).not.toBe('foo') / toEqual / toStrictEqual
|
|
94
|
+
// expect((await <query>)['aria-valuenow']).not.toBe('foo') / toEqual / toStrictEqual
|
|
95
|
+
[`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.object.callee.name=expect][callee.object.property.name=not][callee.object.object.arguments.0.property.value='aria-valuenow']`](node) {
|
|
96
|
+
const queryNode = node.callee.object.object.arguments[0].object;
|
|
97
|
+
const valueProp = node.callee.object.object.arguments[0].property;
|
|
98
|
+
const matcher = node.callee.property;
|
|
99
|
+
if (isValidQueryNode(queryNode)) {
|
|
100
|
+
context.report({
|
|
101
|
+
messageId,
|
|
102
|
+
node,
|
|
103
|
+
fix(fixer) {
|
|
104
|
+
return [fixer.remove((0, _context.getSourceCode)(context).getTokenBefore(valueProp)), fixer.remove((0, _context.getSourceCode)(context).getTokenAfter(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
},
|
|
75
109
|
//expect(element).toHaveAttribute('value', 'foo') / Property
|
|
76
|
-
|
|
110
|
+
//expect(element).toHaveAttribute('aria-valuenow', 'foo') / Property
|
|
111
|
+
[`CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=/^(value|aria-valuenow)$/][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=/(value|aria-valuenow)$/][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
|
|
77
112
|
const matcher = node.callee.property;
|
|
78
113
|
const [prop, value] = node.arguments;
|
|
79
114
|
context.report({
|