eslint-plugin-jest-dom 4.0.1 → 4.0.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.
@@ -90,13 +90,19 @@ const create = context => ({
90
90
  } = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp);
91
91
  if (!isDTLQuery) return; // don't report here if using `expect.foo()`
92
92
 
93
- if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") return;
93
+ if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") {
94
+ return;
95
+ }
96
+
94
97
  context.report({
95
98
  node: matcher,
96
99
  messageId,
97
100
 
98
101
  fix(fixer) {
99
- if (checkedProp.name === "classList" && matcher.name !== "toContain") return;
102
+ if (checkedProp.name === "classList" && matcher.name !== "toContain") {
103
+ return;
104
+ }
105
+
100
106
  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
107
  }
102
108
 
@@ -141,7 +147,10 @@ const create = context => ({
141
147
  messageId,
142
148
 
143
149
  fix(fixer) {
144
- if (className.name === "classList" && matcher.name !== "toContain") return;
150
+ if (className.name === "classList" && matcher.name !== "toContain") {
151
+ return;
152
+ }
153
+
145
154
  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
155
  }
147
156
 
@@ -154,7 +163,11 @@ const create = context => ({
154
163
  const matcher = node.callee.property;
155
164
  const [classArg, classValueArg] = node.arguments;
156
165
  const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
157
- if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
166
+
167
+ if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
168
+ return;
169
+ }
170
+
158
171
  const {
159
172
  isDTLQuery
160
173
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
@@ -177,7 +190,11 @@ const create = context => ({
177
190
  const matcher = node.callee.property;
178
191
  const [classArg, classValueArg] = node.arguments;
179
192
  const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
180
- if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
193
+
194
+ if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
195
+ return;
196
+ }
197
+
181
198
  const {
182
199
  isDTLQuery
183
200
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.object.arguments[0]);
@@ -200,7 +217,11 @@ const create = context => ({
200
217
  const [classArg, classValue] = node.arguments;
201
218
  const classValueArg = classValue.arguments[0];
202
219
  const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1);
203
- if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return;
220
+
221
+ if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") {
222
+ return;
223
+ }
224
+
204
225
  const {
205
226
  isDTLQuery
206
227
  } = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]);
@@ -26,6 +26,14 @@ const meta = {
26
26
  exports.meta = meta;
27
27
 
28
28
  const create = context => {
29
+ function getReplacementObjectProperty(styleName) {
30
+ if (styleName.type === "Literal") {
31
+ return camelCase(styleName.value);
32
+ }
33
+
34
+ return `[${context.getSourceCode().getText(styleName)}]`;
35
+ }
36
+
29
37
  function getReplacementStyleParam(styleName, styleValue) {
30
38
  return styleName.type === "Literal" ? `{${camelCase(styleName.value)}: ${context.getSourceCode().getText(styleValue)}}` : `${context.getSourceCode().getText(styleName).slice(0, -1)}: ${styleValue.type === "TemplateLiteral" ? context.getSourceCode().getText(styleValue).substring(1) : `${styleValue.value}\``}`;
31
39
  }
@@ -107,20 +115,24 @@ const create = context => {
107
115
  },
108
116
 
109
117
  //expect(el.style["foo-bar"]).toBe("baz")
110
- [`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/(Template)?Literal/][parent.parent.callee.name=expect]`](node) {
118
+ [`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/((Template)?Literal|Identifier)/][parent.parent.callee.name=expect]`](node) {
111
119
  const styleName = node.parent.property;
112
120
  const [styleValue] = node.parent.parent.parent.parent.arguments;
113
121
  const matcher = node.parent.parent.parent.property;
114
122
  const startOfStyleMemberExpression = node.object.range[1];
115
123
  const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
124
+ let fix = null;
125
+
126
+ if (typeof styleValue.value !== "number" && !(styleValue.value instanceof RegExp)) {
127
+ fix = fixer => {
128
+ return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, typeof styleName.value === "number" ? `{${getReplacementObjectProperty(styleValue)}: expect.anything()}` : getReplacementStyleParam(styleName, styleValue))];
129
+ };
130
+ }
131
+
116
132
  context.report({
117
133
  node: node.property,
118
134
  message: "Use toHaveStyle instead of asserting on element style",
119
-
120
- fix(fixer) {
121
- return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
122
- }
123
-
135
+ fix
124
136
  });
125
137
  },
126
138
 
@@ -130,14 +142,18 @@ const create = context => {
130
142
  const [styleValue] = node.parent.parent.parent.parent.parent.arguments;
131
143
  const matcher = node.parent.parent.parent.parent.property;
132
144
  const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1];
133
- context.report({
134
- node: node.property,
135
- message: "Use toHaveStyle instead of asserting on element style",
145
+ let fix = null;
136
146
 
137
- fix(fixer) {
147
+ if (typeof styleName.value !== "number") {
148
+ fix = fixer => {
138
149
  return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))];
139
- }
150
+ };
151
+ }
140
152
 
153
+ context.report({
154
+ node: node.property,
155
+ message: "Use toHaveStyle instead of asserting on element style",
156
+ fix
141
157
  });
142
158
  },
143
159
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest-dom",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [
@@ -45,23 +45,22 @@
45
45
  "requireindex": "^1.2.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@typescript-eslint/parser": "^4.33.0",
49
- "eslint": "^7.31.0",
50
- "eslint-remote-tester": "^2.0.1",
51
- "eslint-remote-tester-repositories": "^0.0.3",
52
- "kcd-scripts": "^11.2.2",
48
+ "@typescript-eslint/parser": "^5.9.1",
49
+ "eslint": "^8.7.0",
50
+ "eslint-remote-tester": "^2.1.1",
51
+ "eslint-remote-tester-repositories": "^0.0.5",
52
+ "kcd-scripts": "^12.0.0",
53
53
  "typescript": "^4.5.3"
54
54
  },
55
55
  "peerDependencies": {
56
- "eslint": ">=6.8"
56
+ "eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
57
57
  },
58
58
  "eslintConfig": {
59
59
  "extends": "./node_modules/kcd-scripts/eslint.js",
60
60
  "rules": {
61
- "babel/quotes": "off",
61
+ "consistent-return": "off",
62
62
  "max-lines-per-function": "off",
63
- "testing-library/no-dom-import": "off",
64
- "consistent-return": "off"
63
+ "testing-library/no-dom-import": "off"
65
64
  }
66
65
  },
67
66
  "eslintIgnore": [