Skip to content

Commit c1678a1

Browse files
committed
feat: support shadow-dom-testing-library
1 parent fc1272c commit c1678a1

12 files changed

Lines changed: 99 additions & 54 deletions

src/utils/index.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const LIBRARY_MODULES = [
2828
'@testing-library/vue',
2929
'@testing-library/svelte',
3030
'@marko/testing-library',
31+
'shadow-dom-testing-library',
3132
] as const;
3233

3334
const USER_EVENT_MODULE = '@testing-library/user-event';
@@ -59,6 +60,15 @@ const ALL_QUERIES_METHODS = [
5960
'ByDisplayValue',
6061
'ByRole',
6162
'ByTestId',
63+
// queries coming from 'shadow-dom-testing-library'
64+
'ByShadowLabelText',
65+
'ByShadowPlaceholderText',
66+
'ByShadowText',
67+
'ByShadowAltText',
68+
'ByShadowTitle',
69+
'ByShadowDisplayValue',
70+
'ByShadowRole',
71+
'ByShadowTestId',
6272
] as const;
6373

6474
const SYNC_QUERIES_COMBINATIONS = combineQueries(
@@ -157,27 +167,27 @@ const PRESENCE_MATCHERS = [
157167
const ABSENCE_MATCHERS = ['toBeNull', 'toBeFalsy'] as const;
158168

159169
export {
160-
combineQueries,
161-
getDocsUrl,
162-
SYNC_QUERIES_VARIANTS,
163-
ASYNC_QUERIES_VARIANTS,
164-
ALL_QUERIES_VARIANTS,
170+
ABSENCE_MATCHERS,
171+
ALL_QUERIES_COMBINATIONS,
165172
ALL_QUERIES_METHODS,
166-
SYNC_QUERIES_COMBINATIONS,
173+
ALL_QUERIES_VARIANTS,
174+
ALL_RETURNING_NODES,
167175
ASYNC_QUERIES_COMBINATIONS,
168-
ALL_QUERIES_COMBINATIONS,
176+
ASYNC_QUERIES_VARIANTS,
169177
ASYNC_UTILS,
178+
combineQueries,
170179
DEBUG_UTILS,
180+
EVENT_HANDLER_METHODS,
171181
EVENTS_SIMULATORS,
172-
TESTING_FRAMEWORK_SETUP_HOOKS,
182+
getDocsUrl,
173183
LIBRARY_MODULES,
174-
PROPERTIES_RETURNING_NODES,
175184
METHODS_RETURNING_NODES,
176-
ALL_RETURNING_NODES,
185+
OLD_LIBRARY_MODULES,
177186
PRESENCE_MATCHERS,
178-
ABSENCE_MATCHERS,
179-
EVENT_HANDLER_METHODS,
187+
PROPERTIES_RETURNING_NODES,
188+
SYNC_QUERIES_COMBINATIONS,
189+
SYNC_QUERIES_VARIANTS,
190+
TESTING_FRAMEWORK_SETUP_HOOKS,
180191
USER_EVENT_METHODS,
181192
USER_EVENT_MODULE,
182-
OLD_LIBRARY_MODULES,
183193
};

tests/rules/await-async-queries.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
2525
'@testing-library/react',
2626
'@testing-library/vue',
2727
'@marko/testing-library',
28+
'shadow-dom-testing-library',
2829
];
2930

3031
interface TestCode {
@@ -121,6 +122,11 @@ ruleTester.run(rule.name, rule, {
121122
testingFramework: '@marko/testing-library',
122123
}),
123124

125+
// async shadow-dom-testing-library screen queries declaration are valid
126+
...createTestCase((query) => `await screen.${query}('foo')`, {
127+
testingFramework: 'shadow-dom-testing-library',
128+
}),
129+
124130
// async queries not called are valid
125131
...createTestCase((query) => `expect(screen.${query}).toBeDefined()`, {
126132
isAsync: false,

tests/rules/await-async-utils.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
1919
'@testing-library/react',
2020
'@testing-library/vue',
2121
'@marko/testing-library',
22+
'shadow-dom-testing-library',
2223
];
2324

2425
ruleTester.run(rule.name, rule, {

tests/rules/no-container.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
88
'@testing-library/react',
99
'@testing-library/vue',
1010
'@marko/testing-library',
11+
'shadow-dom-testing-library',
1112
];
1213

1314
ruleTester.run(rule.name, rule, {

tests/rules/no-debugging-utils.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
88
'@testing-library/react',
99
'@testing-library/vue',
1010
'@marko/testing-library',
11+
'shadow-dom-testing-library',
1112
];
1213

1314
ruleTester.run(rule.name, rule, {

tests/rules/no-node-access.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
1818
'@testing-library/react',
1919
'@testing-library/vue',
2020
'@marko/testing-library',
21+
'shadow-dom-testing-library',
2122
];
2223

2324
ruleTester.run(rule.name, rule, {

tests/rules/no-render-in-lifecycle.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
99
'@testing-library/react',
1010
'@testing-library/vue',
1111
'@marko/testing-library',
12+
'shadow-dom-testing-library',
1213
];
1314

1415
ruleTester.run(rule.name, rule, {

tests/rules/no-test-id-queries.test.ts

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
99
'@testing-library/react',
1010
'@testing-library/vue',
1111
'@marko/testing-library',
12+
'shadow-dom-testing-library',
1213
];
1314

1415
const QUERIES = [
@@ -20,6 +21,47 @@ const QUERIES = [
2021
'findAllByTestId',
2122
];
2223

24+
const createInvalidTestCase = (framework: string, query: string) => {
25+
return [
26+
{
27+
code: `
28+
import { render } from '${framework}';
29+
30+
test('test', async () => {
31+
const { ${query} } = render(<MyComponent />);
32+
33+
expect(${query}('my-test-id')).toBeInTheDocument();
34+
});
35+
`,
36+
errors: [
37+
{
38+
messageId: 'noTestIdQueries',
39+
line: 7,
40+
column: 14,
41+
},
42+
],
43+
},
44+
{
45+
code: `
46+
import { render, screen } from '${framework}';
47+
48+
test('test', async () => {
49+
render(<MyComponent />);
50+
51+
expect(screen.${query}('my-test-id')).toBeInTheDocument();
52+
});
53+
`,
54+
errors: [
55+
{
56+
messageId: 'noTestIdQueries',
57+
line: 7,
58+
column: 14,
59+
},
60+
],
61+
},
62+
] as const;
63+
};
64+
2365
ruleTester.run(rule.name, rule, {
2466
valid: [
2567
`
@@ -43,44 +85,22 @@ ruleTester.run(rule.name, rule, {
4385
`,
4486
],
4587

46-
invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) =>
47-
QUERIES.flatMap((query) => [
48-
{
49-
code: `
50-
import { render } from '${framework}';
51-
52-
test('test', async () => {
53-
const { ${query} } = render(<MyComponent />);
54-
55-
expect(${query}('my-test-id')).toBeInTheDocument();
56-
});
57-
`,
58-
errors: [
59-
{
60-
messageId: 'noTestIdQueries',
61-
line: 7,
62-
column: 14,
63-
},
64-
],
65-
},
66-
{
67-
code: `
68-
import { render, screen } from '${framework}';
69-
70-
test('test', async () => {
71-
render(<MyComponent />);
72-
73-
expect(screen.${query}('my-test-id')).toBeInTheDocument();
74-
});
75-
`,
76-
errors: [
77-
{
78-
messageId: 'noTestIdQueries',
79-
line: 7,
80-
column: 14,
81-
},
82-
],
83-
},
84-
])
85-
),
88+
invalid: [
89+
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) =>
90+
QUERIES.flatMap((query) => createInvalidTestCase(framework, query))
91+
),
92+
// special cases for shadow-dom-testing-library
93+
...[
94+
'ByShadowLabelText',
95+
'ByShadowPlaceholderText',
96+
'ByShadowText',
97+
'ByShadowAltText',
98+
'ByShadowTitle',
99+
'ByShadowDisplayValue',
100+
'ByShadowRole',
101+
'ByShadowTestId',
102+
].flatMap((query) =>
103+
createInvalidTestCase('shadow-dom-testing-library', query)
104+
),
105+
],
86106
});

tests/rules/prefer-find-by.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
1919
'@testing-library/react',
2020
'@testing-library/vue',
2121
'@marko/testing-library',
22+
'shadow-dom-testing-library',
2223
];
2324

2425
function buildFindByMethod(queryMethod: string) {

tests/rules/prefer-query-by-disappearance.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
99
'@testing-library/react',
1010
'@testing-library/vue',
1111
'@marko/testing-library',
12+
'shadow-dom-testing-library',
1213
];
1314

1415
ruleTester.run(rule.name, rule, {

0 commit comments

Comments
 (0)