-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): added new rule for prefer-to-have-style (#76)
* feat(rules): added new rule prefer-to-have-style * updated readme * updated readme * updated readme * updated readme fix(rules): use .range instead of start/end
- Loading branch information
Showing
14 changed files
with
377 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# prefer toHaveProperty over checking element.style (prefer-to-have-style) | ||
|
||
This rule is an autofixable rule that reports usages of checking element.style in expect statements in preference of using the jest-dom | ||
`toHaveStyle` matcher. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
expect(el.style.foo).toBe("bar"); | ||
expect(el.style.foo).not.toBe("bar"); | ||
expect(el.style).toHaveProperty("background-color", "green"); | ||
expect(screen.getByTestId("foo").style["scroll-snap-type"]).toBe("x mandatory"); | ||
expect(el.style).toContain("background-color"); | ||
expect(el.style).not.toContain("background-color"); | ||
expect(el).toHaveAttribute( | ||
"style", | ||
"background-color: green; border-width: 10px; color: blue;" | ||
); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
expect(el).toHaveStyle({ foo: "bar" }); | ||
expect(el.style).toMatchSnapshot(); | ||
expect(el.style).toEqual(foo); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about using built in matchers for checking style on dom | ||
elements. | ||
|
||
## Further Reading | ||
|
||
- [jest-dom toHaveStyle](https://github.com/testing-library/jest-dom#tohavestyle) | ||
- [ElementCSSInlineStyle.style](https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{"rules":{ | ||
"no-template-curly-in-string":"off" | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { RuleTester } from "eslint"; | ||
import * as rule from "../../../rules/prefer-to-have-style"; | ||
|
||
const errors = [ | ||
{ message: "Use toHaveStyle instead of asserting on element style" }, | ||
]; | ||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); | ||
ruleTester.run("prefer-to-have-attribute", rule, { | ||
valid: [ | ||
`expect(el).toHaveStyle({foo:"bar"})`, | ||
`expect(el.style).toMatchSnapshot()`, | ||
`expect(el.style).toEqual(foo)`, | ||
`expect(el).toHaveAttribute("style")`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `expect(el.style.foo).toBe("bar")`, | ||
errors, | ||
output: `expect(el).toHaveStyle({foo:"bar"})`, | ||
}, | ||
{ | ||
code: `expect(el.style.foo).not.toBe("bar")`, | ||
errors, | ||
output: `expect(el).not.toHaveStyle({foo:"bar"})`, | ||
}, | ||
{ | ||
code: `expect(el.style).toHaveProperty("background-color", "green")`, | ||
errors, | ||
output: `expect(el).toHaveStyle({backgroundColor: "green"})`, | ||
}, | ||
{ | ||
code: `expect(el.style).not.toHaveProperty("background-color", "green")`, | ||
errors, | ||
output: `expect(el).not.toHaveStyle({backgroundColor: "green"})`, | ||
}, | ||
{ | ||
code: `expect(screen.getByTestId("foo").style["scroll-snap-type"]).toBe("x mandatory")`, | ||
errors, | ||
output: `expect(screen.getByTestId("foo")).toHaveStyle({scrollSnapType: "x mandatory"})`, | ||
}, | ||
{ | ||
code: `expect(screen.getByTestId("foo").style["scroll-snap-type"]).not.toBe("x mandatory")`, | ||
errors, | ||
output: `expect(screen.getByTestId("foo")).not.toHaveStyle({scrollSnapType: "x mandatory"})`, | ||
}, | ||
{ | ||
code: `expect(el.style).toContain("background-color")`, | ||
errors, | ||
output: `expect(el).toHaveStyle({backgroundColor: expect.anything()})`, | ||
}, | ||
{ | ||
code: `expect(el.style).not.toContain("background-color")`, | ||
errors, | ||
output: `expect(el).not.toHaveStyle({backgroundColor: expect.anything()})`, | ||
}, | ||
{ | ||
code: `expect(el).toHaveAttribute("style", "background-color: green; border-width: 10px; color: blue;")`, | ||
errors, | ||
output: `expect(el).toHaveStyle("background-color: green; border-width: 10px; color: blue;")`, | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.