Skip to content

Commit 429e1ba

Browse files
authored
fix(rules): improvements to rules that should only apply to dom nodes (#129)
1 parent c595f94 commit 429e1ba

File tree

7 files changed

+241
-130
lines changed

7 files changed

+241
-130
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Thanks goes to these people ([emoji key][emojis]):
164164

165165
<!-- markdownlint-enable -->
166166
<!-- prettier-ignore-end -->
167+
167168
<!-- ALL-CONTRIBUTORS-LIST:END -->
168169

169170
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/__fixtures__/createBannedAttributeTestCases.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@ export default ({ preferred, negatedPreferred, attribute }) => {
44
const doubleNegativeCases = negatedPreferred.startsWith("toBe")
55
? [
66
{
7-
code: `expect(element).not.${negatedPreferred}`,
7+
code: `const el = screen.getByText("foo"); expect(el).not.${negatedPreferred}`,
88
errors: [
99
{
1010
message: `Use ${preferred} instead of not.${negatedPreferred}`,
1111
},
1212
],
13-
output: `expect(element).${preferred}`,
13+
output: `const el = screen.getByText("foo"); expect(el).${preferred}`,
1414
},
1515
{
16-
code: `expect(element).not.${preferred}`,
16+
code: `expect(getByText("foo")).not.${negatedPreferred}`,
17+
errors: [
18+
{
19+
message: `Use ${preferred} instead of not.${negatedPreferred}`,
20+
},
21+
],
22+
output: `expect(getByText("foo")).${preferred}`,
23+
},
24+
{
25+
code: `const el = screen.getByText("foo"); expect(el).not.${preferred}`,
1726
errors: [
1827
{
1928
message: `Use ${negatedPreferred} instead of not.${preferred}`,
2029
},
2130
],
22-
output: `expect(element).${negatedPreferred}`,
31+
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
2332
},
2433
]
2534
: [];
@@ -57,85 +66,88 @@ export default ({ preferred, negatedPreferred, attribute }) => {
5766

5867
return {
5968
valid: [
60-
"expect(element).not.toHaveProperty('value', 'foo')",
61-
`expect(element).${preferred}`,
62-
`expect(element).${negatedPreferred}`,
63-
"expect(element).toHaveProperty('value', 'bar')",
69+
`const el = screen.getByText("foo"); expect(el).not.toHaveProperty('value', 'foo')`,
70+
`const el = screen.getByText("foo"); expect(el).${preferred}`,
71+
`const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
72+
`const el = screen.getByText("foo"); expect(el).toHaveProperty('value', 'bar')`,
73+
`const el = foo.bar(); expect(el).toHaveProperty("${attribute}", true)`,
74+
`expect(getFoo().${attribute}).toBe("bar")`,
75+
`expect(getFoo().${attribute}).not.toBe("bar")`,
6476
],
6577
invalid: [
6678
...doubleNegativeCases,
6779
...directChecks,
6880
{
69-
code: `expect(element).toHaveProperty('${attribute}', true)`,
81+
code: `const el = screen.getByText("foo"); expect(el).toHaveProperty('${attribute}', true)`,
7082
errors: [
7183
{
7284
message: `Use ${preferred} instead of toHaveProperty('${attribute}', true)`,
7385
},
7486
],
75-
output: `expect(element).${preferred}`,
87+
output: `const el = screen.getByText("foo"); expect(el).${preferred}`,
7688
},
7789
{
78-
code: `expect(element).toHaveProperty('${attribute}', false)`,
90+
code: `const el = screen.getByText("foo"); expect(el).toHaveProperty('${attribute}', false)`,
7991
errors: [
8092
{
8193
message: `Use ${negatedPreferred} instead of toHaveProperty('${attribute}', false)`,
8294
},
8395
],
84-
output: `expect(element).${negatedPreferred}`,
96+
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
8597
},
8698
{
87-
code: `expect(element).toHaveAttribute('${attribute}', false)`,
99+
code: `const el = screen.getByText("foo"); expect(el).toHaveAttribute('${attribute}', false)`,
88100
errors: [
89101
{
90102
message: `Use ${negatedPreferred} instead of toHaveAttribute('${attribute}', false)`,
91103
},
92104
],
93-
output: `expect(element).${negatedPreferred}`,
105+
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
94106
},
95107
{
96-
code: `expect(element).toHaveProperty('${attribute}')`,
108+
code: `const el = screen.getByText("foo"); expect(el).toHaveProperty('${attribute}')`,
97109
errors: [
98110
{
99111
message: `Use ${preferred} instead of toHaveProperty('${attribute}')`,
100112
},
101113
],
102-
output: `expect(element).${preferred}`,
114+
output: `const el = screen.getByText("foo"); expect(el).${preferred}`,
103115
},
104116
{
105-
code: `expect(element).toHaveAttribute('${attribute}')`,
117+
code: `const el = screen.getByText("foo"); expect(el).toHaveAttribute('${attribute}')`,
106118
errors: [
107119
{
108120
message: `Use ${preferred} instead of toHaveAttribute('${attribute}')`,
109121
},
110122
],
111-
output: `expect(element).${preferred}`,
123+
output: `const el = screen.getByText("foo"); expect(el).${preferred}`,
112124
},
113125
{
114-
code: `expect(element).not.toHaveAttribute('${attribute}')`,
126+
code: `const el = screen.getByText("foo"); expect(el).not.toHaveAttribute('${attribute}')`,
115127
errors: [
116128
{
117129
message: `Use ${negatedPreferred} instead of not.toHaveAttribute('${attribute}')`,
118130
},
119131
],
120-
output: `expect(element).${negatedPreferred}`,
132+
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
121133
},
122134
{
123-
code: `expect(element).not.toHaveProperty('${attribute}')`,
135+
code: `const el = screen.getByText("foo"); expect(el).not.toHaveProperty('${attribute}')`,
124136
errors: [
125137
{
126138
message: `Use ${negatedPreferred} instead of not.toHaveProperty('${attribute}')`,
127139
},
128140
],
129-
output: `expect(element).${negatedPreferred}`,
141+
output: `const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,
130142
},
131143
{
132-
code: `expect(element).toHaveAttribute("${attribute}", "")`,
144+
code: `const el = screen.getByText("foo"); expect(el).toHaveAttribute("${attribute}", "")`,
133145
errors: [
134146
{
135147
message: `Use ${preferred} instead of toHaveAttribute("${attribute}", "")`,
136148
},
137149
],
138-
output: `expect(element).${preferred}`,
150+
output: `const el = screen.getByText("foo"); expect(el).${preferred}`,
139151
},
140152
{
141153
code: `expect(getByText("foo")).toHaveAttribute("${attribute}", "true")`,
@@ -174,7 +186,7 @@ export default ({ preferred, negatedPreferred, attribute }) => {
174186
output: `expect(getByText("foo")).${negatedPreferred}`,
175187
},
176188
{
177-
code: `expect(element).toHaveProperty('${attribute}', foo)`,
189+
code: `const el = screen.getByText("foo"); expect(el).toHaveProperty('${attribute}', foo)`,
178190
errors: [
179191
{
180192
message: `Use ${preferred} instead of toHaveProperty('${attribute}', foo)`,

0 commit comments

Comments
 (0)