eslint-plugin-jest-dom 5.7.1 → 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
- attributes,
12
- excludeValues = []
11
+ mixedPreferred,
12
+ attributes
13
13
  }) => context => {
14
- const isExcludedValue = node => excludeValues.length > 0 && node.arguments.length >= 2 && node.arguments[1].type === "Literal" && typeof node.arguments[1].value === "string" && excludeValues.some(v => v.toLowerCase() === node.arguments[1].value.toLowerCase());
15
- const getCorrectFunctionFor = (node, negated = false) => (node.arguments.length === 1 || node.arguments[1].value === true || node.arguments[1].type !== "Literal" || typeof node.arguments[1].value === "string" && node.arguments[1].value.toLowerCase() === "true" || node.arguments[1].value === "") && !negated ? preferred : negatedPreferred;
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,9 +85,6 @@ var _default = ({
64
85
  if (!isBannedArg(node)) {
65
86
  return;
66
87
  }
67
- if (isExcludedValue(node)) {
68
- return;
69
- }
70
88
  const correctFunction = getCorrectFunctionFor(node, true);
71
89
  const incorrectFunction = node.callee.property.name;
72
90
  const message = `Use ${correctFunction}() instead of not.${incorrectFunction}(${node.arguments.map(({
@@ -83,9 +101,6 @@ var _default = ({
83
101
  if (!isBannedArg(node)) {
84
102
  return;
85
103
  }
86
- if (isExcludedValue(node)) {
87
- return;
88
- }
89
104
  const {
90
105
  isDTLQuery
91
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
- attributes: ["checked", "aria-checked"],
27
- excludeValues: ["mixed"]
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
- attributes: ["aria-pressed"],
27
- excludeValues: ["mixed"]
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
- [`CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
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({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "5.7.1",
3
+ "version": "5.8.0",
4
4
  "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
5
  "keywords": [
6
6
  "eslint",