-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathprefer-to-have-style.js
77 lines (76 loc) · 2.57 KB
/
prefer-to-have-style.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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-style", 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.backgroundImage).toBe(`url(${foo})`)",
errors,
output: "expect(el).toHaveStyle({backgroundImage:`url(${foo})`})",
},
{
code: "expect(el.style.backgroundImage).not.toBe(`url(${foo})`)",
errors,
output: "expect(el).not.toHaveStyle({backgroundImage:`url(${foo})`})",
},
{
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(el.style["scroll-snap-type"]).toBe(`${x} mandatory`)',
errors,
output: "expect(el).toHaveStyle({scrollSnapType: `${x} mandatory`})",
},
{
code: `expect(el.style["scroll-snap-type"]).not.toBe("x mandatory")`,
errors,
output: `expect(el).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;")`,
},
],
});