eslint-plugin-jest-dom
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -93,3 +93,6 @@ "use strict"; | ||
if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") return; | ||
if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") { | ||
return; | ||
} | ||
context.report({ | ||
@@ -100,3 +103,6 @@ node: matcher, | ||
fix(fixer) { | ||
if (checkedProp.name === "classList" && matcher.name !== "toContain") return; | ||
if (checkedProp.name === "classList" && matcher.name !== "toContain") { | ||
return; | ||
} | ||
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 }"}`)]; | ||
@@ -146,3 +152,6 @@ } | ||
fix(fixer) { | ||
if (className.name === "classList" && matcher.name !== "toContain") return; | ||
if (className.name === "classList" && matcher.name !== "toContain") { | ||
return; | ||
} | ||
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 }"}`)]; | ||
@@ -160,3 +169,7 @@ } | ||
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1); | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return; | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") { | ||
return; | ||
} | ||
const { | ||
@@ -184,3 +197,7 @@ isDTLQuery | ||
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1); | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return; | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") { | ||
return; | ||
} | ||
const { | ||
@@ -208,3 +225,7 @@ isDTLQuery | ||
const classNameValue = context.getSourceCode().getText(classArg).slice(1, -1); | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return; | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") { | ||
return; | ||
} | ||
const { | ||
@@ -211,0 +232,0 @@ isDTLQuery |
@@ -29,2 +29,10 @@ "use strict"; | ||
const create = context => { | ||
function getReplacementObjectProperty(styleName) { | ||
if (styleName.type === "Literal") { | ||
return camelCase(styleName.value); | ||
} | ||
return `[${context.getSourceCode().getText(styleName)}]`; | ||
} | ||
function getReplacementStyleParam(styleName, styleValue) { | ||
@@ -111,3 +119,3 @@ 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}\``}`; | ||
//expect(el.style["foo-bar"]).toBe("baz") | ||
[`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) { | ||
[`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) { | ||
const styleName = node.parent.property; | ||
@@ -118,10 +126,14 @@ const [styleValue] = node.parent.parent.parent.parent.arguments; | ||
const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1]; | ||
let fix = null; | ||
if (typeof styleValue.value !== "number" && !(styleValue.value instanceof RegExp)) { | ||
fix = fixer => { | ||
return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, typeof styleName.value === "number" ? `{${getReplacementObjectProperty(styleValue)}: expect.anything()}` : getReplacementStyleParam(styleName, styleValue))]; | ||
}; | ||
} | ||
context.report({ | ||
node: node.property, | ||
message: "Use toHaveStyle instead of asserting on element style", | ||
fix(fixer) { | ||
return [fixer.removeRange([startOfStyleMemberExpression, endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))]; | ||
} | ||
fix | ||
}); | ||
@@ -136,10 +148,14 @@ }, | ||
const endOfStyleMemberExpression = node.parent.parent.arguments[0].range[1]; | ||
let fix = null; | ||
if (typeof styleName.value !== "number") { | ||
fix = fixer => { | ||
return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))]; | ||
}; | ||
} | ||
context.report({ | ||
node: node.property, | ||
message: "Use toHaveStyle instead of asserting on element style", | ||
fix(fixer) { | ||
return [fixer.removeRange([node.object.range[1], endOfStyleMemberExpression]), fixer.replaceText(matcher, "toHaveStyle"), fixer.replaceText(styleValue, getReplacementStyleParam(styleName, styleValue))]; | ||
} | ||
fix | ||
}); | ||
@@ -146,0 +162,0 @@ }, |
{ | ||
"name": "eslint-plugin-jest-dom", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom", | ||
@@ -48,11 +48,11 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@typescript-eslint/parser": "^4.33.0", | ||
"eslint": "^7.31.0", | ||
"eslint-remote-tester": "^2.0.1", | ||
"eslint-remote-tester-repositories": "^0.0.3", | ||
"kcd-scripts": "^11.2.2", | ||
"@typescript-eslint/parser": "^5.9.1", | ||
"eslint": "^8.7.0", | ||
"eslint-remote-tester": "^2.1.1", | ||
"eslint-remote-tester-repositories": "^0.0.5", | ||
"kcd-scripts": "^12.0.0", | ||
"typescript": "^4.5.3" | ||
}, | ||
"peerDependencies": { | ||
"eslint": ">=6.8" | ||
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0" | ||
}, | ||
@@ -62,6 +62,5 @@ "eslintConfig": { | ||
"rules": { | ||
"babel/quotes": "off", | ||
"consistent-return": "off", | ||
"max-lines-per-function": "off", | ||
"testing-library/no-dom-import": "off", | ||
"consistent-return": "off" | ||
"testing-library/no-dom-import": "off" | ||
} | ||
@@ -68,0 +67,0 @@ }, |
79737
1244