Skip to content

Commit 81d9487

Browse files
committed
test: add fake rule tests for queries
1 parent 9132779 commit 81d9487

File tree

3 files changed

+252
-12
lines changed

3 files changed

+252
-12
lines changed

tests/create-testing-library-rule.test.ts

+213
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,89 @@ ruleTester.run(RULE_NAME, rule, {
8989
import { foo } from 'custom-module-forced-report'
9090
`,
9191
},
92+
93+
// Test Cases for all settings mixed
94+
{
95+
settings: {
96+
'testing-library/module': 'test-utils',
97+
'testing-library/filename-pattern': 'testing-library\\.js',
98+
},
99+
code: `
100+
// case: matching custom settings partially - module but not filename
101+
import { render } from 'test-utils'
102+
import { somethingElse } from 'another-module'
103+
const foo = require('bar')
104+
105+
const utils = render();
106+
`,
107+
},
108+
{
109+
settings: {
110+
'testing-library/module': 'test-utils',
111+
'testing-library/filename-pattern': 'testing-library\\.js',
112+
},
113+
filename: 'MyComponent.testing-library.js',
114+
code: `
115+
// case: matching custom settings partially - filename but not module
116+
import { render } from 'other-utils'
117+
import { somethingElse } from 'another-module'
118+
const foo = require('bar')
119+
120+
const utils = render();
121+
`,
122+
},
123+
124+
// Test Cases for Queries and Aggressive Queries Reporting
125+
{
126+
code: `
127+
// case: custom method not matching "getBy*" variant pattern
128+
getSomeElement('button')
129+
`,
130+
},
131+
{
132+
code: `
133+
// case: custom method not matching "queryBy*" variant pattern
134+
querySomeElement('button')
135+
`,
136+
},
137+
{
138+
settings: {
139+
'testing-library/module': 'test-utils',
140+
},
141+
code: `
142+
// case: built-in "getBy*" query not reported because custom module not imported
143+
import { render } from 'other-module'
144+
getByRole('button')
145+
`,
146+
},
147+
{
148+
settings: {
149+
'testing-library/module': 'test-utils',
150+
},
151+
code: `
152+
// case: built-in "queryBy*" query not reported because custom module not imported
153+
import { render } from 'other-module'
154+
queryByRole('button')
155+
`,
156+
},
157+
{
158+
settings: {
159+
'testing-library/filename-pattern': 'testing-library\\.js',
160+
},
161+
code: `
162+
// case: built-in "getBy*" query not reported because custom filename doesn't match
163+
getByRole('button')
164+
`,
165+
},
166+
{
167+
settings: {
168+
'testing-library/filename-pattern': 'testing-library\\.js',
169+
},
170+
code: `
171+
// case: built-in "queryBy*" query not reported because custom filename doesn't match
172+
queryByRole('button')
173+
`,
174+
},
92175
],
93176
invalid: [
94177
// Test Cases for Imports & Filename
@@ -260,5 +343,135 @@ ruleTester.run(RULE_NAME, rule, {
260343
`,
261344
errors: [{ line: 3, column: 7, messageId: 'fakeError' }],
262345
},
346+
347+
// Test Cases for all settings mixed
348+
{
349+
settings: {
350+
'testing-library/module': 'test-utils',
351+
'testing-library/filename-pattern': 'testing-library\\.js',
352+
},
353+
filename: 'MyComponent.testing-library.js',
354+
code: `
355+
// case: matching all custom settings
356+
import { render } from 'test-utils'
357+
import { somethingElse } from 'another-module'
358+
const foo = require('bar')
359+
360+
const utils = render();
361+
`,
362+
errors: [{ line: 7, column: 21, messageId: 'fakeError' }],
363+
},
364+
365+
// Test Cases for Queries and Aggressive Queries Reporting
366+
{
367+
code: `
368+
// case: built-in "getBy*" query reported without import (aggressive reporting)
369+
getByRole('button')
370+
`,
371+
errors: [{ line: 3, column: 7, messageId: 'getByError' }],
372+
},
373+
{
374+
code: `
375+
// case: built-in "queryBy*" query reported without import (aggressive reporting)
376+
queryByRole('button')
377+
`,
378+
errors: [{ line: 3, column: 7, messageId: 'queryByError' }],
379+
},
380+
{
381+
filename: 'MyComponent.spec.js',
382+
code: `
383+
// case: custom "getBy*" query reported without import (aggressive reporting)
384+
getByIcon('search')
385+
`,
386+
errors: [{ line: 3, column: 7, messageId: 'getByError' }],
387+
},
388+
{
389+
code: `
390+
// case: custom "queryBy*" query reported without import (aggressive reporting)
391+
queryByIcon('search')
392+
`,
393+
errors: [{ line: 3, column: 7, messageId: 'queryByError' }],
394+
},
395+
{
396+
settings: {
397+
'testing-library/module': 'test-utils',
398+
},
399+
code: `
400+
// case: built-in "getBy*" query reported with custom module + Testing Library package import
401+
import { render } from '@testing-library/react'
402+
getByRole('button')
403+
`,
404+
errors: [{ line: 4, column: 7, messageId: 'getByError' }],
405+
},
406+
{
407+
filename: 'MyComponent.spec.js',
408+
code: `
409+
// case: built-in "queryBy*" query reported with custom module + Testing Library package import
410+
import { render } from '@testing-library/framework'
411+
queryByRole('button')
412+
`,
413+
errors: [{ line: 4, column: 7, messageId: 'queryByError' }],
414+
},
415+
{
416+
settings: {
417+
'testing-library/module': 'test-utils',
418+
},
419+
code: `
420+
// case: built-in "getBy*" query reported with custom module + custom module import
421+
import { render } from 'test-utils'
422+
getByRole('button')
423+
`,
424+
errors: [{ line: 4, column: 7, messageId: 'getByError' }],
425+
},
426+
{
427+
filename: 'MyComponent.spec.js',
428+
code: `
429+
// case: built-in "queryBy*" query reported with custom module + custom module import
430+
import { render } from 'test-utils'
431+
queryByRole('button')
432+
`,
433+
errors: [{ line: 4, column: 7, messageId: 'queryByError' }],
434+
},
435+
436+
{
437+
settings: {
438+
'testing-library/module': 'test-utils',
439+
},
440+
code: `
441+
// case: custom "getBy*" query reported with custom module + Testing Library package import
442+
import { render } from '@testing-library/react'
443+
getByIcon('search')
444+
`,
445+
errors: [{ line: 4, column: 7, messageId: 'getByError' }],
446+
},
447+
{
448+
filename: 'MyComponent.spec.js',
449+
code: `
450+
// case: custom "queryBy*" query reported with custom module + Testing Library package import
451+
import { render } from '@testing-library/framework'
452+
queryByIcon('search')
453+
`,
454+
errors: [{ line: 4, column: 7, messageId: 'queryByError' }],
455+
},
456+
{
457+
settings: {
458+
'testing-library/module': 'test-utils',
459+
},
460+
code: `
461+
// case: custom "getBy*" query reported with custom module + custom module import
462+
import { render } from 'test-utils'
463+
getByIcon('search')
464+
`,
465+
errors: [{ line: 4, column: 7, messageId: 'getByError' }],
466+
},
467+
{
468+
filename: 'MyComponent.spec.js',
469+
code: `
470+
// case: custom "queryBy*" query reported with custom module + custom module import
471+
import { render } from 'test-utils'
472+
queryByIcon('search')
473+
`,
474+
errors: [{ line: 4, column: 7, messageId: 'queryByError' }],
475+
},
263476
],
264477
});

tests/fake-rule.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createTestingLibraryRule } from '../lib/create-testing-library-rule';
77

88
export const RULE_NAME = 'fake-rule';
99
type Options = [];
10-
type MessageIds = 'fakeError';
10+
type MessageIds = 'fakeError' | 'getByError' | 'queryByError';
1111

1212
export default createTestingLibraryRule<Options, MessageIds>({
1313
name: RULE_NAME,
@@ -20,18 +20,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
2020
},
2121
messages: {
2222
fakeError: 'fake error reported',
23+
getByError: 'some error related to getBy reported',
24+
queryByError: 'some error related to queryBy reported',
2325
},
2426
fixable: null,
2527
schema: [],
2628
},
2729
defaultOptions: [],
2830
create(context, _, helpers) {
29-
const reportRenderIdentifier = (node: TSESTree.Identifier) => {
31+
const reportCallExpressionIdentifier = (node: TSESTree.Identifier) => {
32+
// force "render" to be reported
3033
if (node.name === 'render') {
31-
context.report({
32-
node,
33-
messageId: 'fakeError',
34-
});
34+
return context.report({ node, messageId: 'fakeError' });
35+
}
36+
37+
// force queries to be reported
38+
if (helpers.isGetByQuery(node)) {
39+
return context.report({ node, messageId: 'getByError' });
40+
}
41+
42+
if (helpers.isQueryByQuery(node)) {
43+
return context.report({ node, messageId: 'queryByError' });
3544
}
3645
};
3746

@@ -40,15 +49,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
4049
// override `ImportDeclaration` from `detectTestingLibraryUtils`
4150

4251
if (node.source.value === 'report-me') {
43-
context.report({
44-
node,
45-
messageId: 'fakeError',
46-
});
52+
context.report({ node, messageId: 'fakeError' });
4753
}
4854
};
4955

5056
return {
51-
'CallExpression Identifier': reportRenderIdentifier,
57+
'CallExpression Identifier': reportCallExpressionIdentifier,
5258
ImportDeclaration: checkImportDeclaration,
5359
'Program:exit'() {
5460
const importNode = helpers.getCustomModuleImportNode();

tests/lib/rules/prefer-presence-queries.test.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,27 @@ ruleTester.run(RULE_NAME, rule, {
665665
`,
666666
errors: [{ messageId: 'wrongAbsenceQuery', line: 3, column: 16 }],
667667
},
668-
// TODO: add more tests for importing custom module
668+
{
669+
settings: {
670+
'testing-library/module': 'test-utils',
671+
},
672+
code: `
673+
// case: asserting presence incorrectly importing custom module
674+
import 'test-utils'
675+
expect(queryByRole("button")).toBeInTheDocument()
676+
`,
677+
errors: [{ line: 4, column: 14, messageId: 'wrongPresenceQuery' }],
678+
},
679+
{
680+
settings: {
681+
'testing-library/module': 'test-utils',
682+
},
683+
code: `
684+
// case: asserting absence incorrectly importing custom module
685+
import 'test-utils'
686+
expect(getByRole("button")).not.toBeInTheDocument()
687+
`,
688+
errors: [{ line: 4, column: 14, messageId: 'wrongAbsenceQuery' }],
689+
},
669690
],
670691
});

0 commit comments

Comments
 (0)