|
| 1 | +import rule, { RULE_NAME } from '../../../lib/rules/no-test-id-queries'; |
| 2 | +import { createRuleTester } from '../test-utils'; |
| 3 | + |
| 4 | +const ruleTester = createRuleTester(); |
| 5 | + |
| 6 | +const SUPPORTED_TESTING_FRAMEWORKS = [ |
| 7 | + '@testing-library/dom', |
| 8 | + '@testing-library/angular', |
| 9 | + '@testing-library/react', |
| 10 | + '@testing-library/vue', |
| 11 | + '@marko/testing-library', |
| 12 | +]; |
| 13 | + |
| 14 | +const QUERIES = [ |
| 15 | + 'getByTestId', |
| 16 | + 'queryByTestId', |
| 17 | + 'getAllByTestId', |
| 18 | + 'queryAllByTestId', |
| 19 | + 'findByTestId', |
| 20 | + 'findAllByTestId', |
| 21 | +]; |
| 22 | + |
| 23 | +ruleTester.run(RULE_NAME, rule, { |
| 24 | + valid: [ |
| 25 | + { |
| 26 | + code: ` |
| 27 | + import { render } from '@testing-library/react'; |
| 28 | +
|
| 29 | + test('test', async () => { |
| 30 | + const { getByRole } = render(<MyComponent />); |
| 31 | +
|
| 32 | + expect(getByRole('button')).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + `, |
| 35 | + }, |
| 36 | + ], |
| 37 | + |
| 38 | + invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) => |
| 39 | + QUERIES.flatMap((query) => [ |
| 40 | + { |
| 41 | + code: ` |
| 42 | + import { render } from '${framework}'; |
| 43 | +
|
| 44 | + test('test', async () => { |
| 45 | + const { ${query} } = render(<MyComponent />); |
| 46 | +
|
| 47 | + expect(${query}('my-test-id')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + `, |
| 50 | + errors: [ |
| 51 | + { |
| 52 | + messageId: 'noTestIdQueries', |
| 53 | + line: 7, |
| 54 | + column: 14, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: ` |
| 60 | + import { render, screen } from '${framework}'; |
| 61 | +
|
| 62 | + test('test', async () => { |
| 63 | + render(<MyComponent />); |
| 64 | +
|
| 65 | + expect(screen.${query}('my-test-id')).toBeInTheDocument(); |
| 66 | + }); |
| 67 | + `, |
| 68 | + errors: [ |
| 69 | + { |
| 70 | + messageId: 'noTestIdQueries', |
| 71 | + line: 7, |
| 72 | + column: 14, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ]) |
| 77 | + ), |
| 78 | +}); |
0 commit comments