|
| 1 | +import rule, { |
| 2 | + RULE_NAME, |
| 3 | +} from '../../../lib/rules/no-global-regexp-flag-in-query'; |
| 4 | +import { createRuleTester } from '../test-utils'; |
| 5 | + |
| 6 | +const ruleTester = createRuleTester(); |
| 7 | + |
| 8 | +ruleTester.run(RULE_NAME, rule, { |
| 9 | + valid: [ |
| 10 | + ` |
| 11 | + import { screen } from '@testing-library/dom' |
| 12 | + screen.getByText(/hello/) |
| 13 | + `, |
| 14 | + ` |
| 15 | + import { screen } from '@testing-library/dom' |
| 16 | + screen.getByText(/hello/i) |
| 17 | + `, |
| 18 | + ` |
| 19 | + import { screen } from '@testing-library/dom' |
| 20 | + screen.getByText('hello') |
| 21 | + `, |
| 22 | + |
| 23 | + ` |
| 24 | + import { screen } from '@testing-library/dom' |
| 25 | + screen.findByRole('button', {name: /hello/}) |
| 26 | + `, |
| 27 | + ` |
| 28 | + import { screen } from '@testing-library/dom' |
| 29 | + screen.findByRole('button', {name: /hello/im}) |
| 30 | + `, |
| 31 | + ` |
| 32 | + import { screen } from '@testing-library/dom' |
| 33 | + screen.findByRole('button', {name: 'hello'}) |
| 34 | + `, |
| 35 | + ` |
| 36 | + const utils = render(<Component/>) |
| 37 | + utils.findByRole('button', {name: /hello/m}) |
| 38 | + `, |
| 39 | + ` |
| 40 | + const {queryAllByPlaceholderText} = render(<Component/>) |
| 41 | + queryAllByPlaceholderText(/hello/i) |
| 42 | + `, |
| 43 | + ` |
| 44 | + import { within } from '@testing-library/dom' |
| 45 | + within(element).findByRole('button', {name: /hello/i}) |
| 46 | + `, |
| 47 | + ` |
| 48 | + import { within } from '@testing-library/dom' |
| 49 | + within(element).queryByText('Hello') |
| 50 | + `, |
| 51 | + ` |
| 52 | + const text = 'hello'; |
| 53 | + /hello/g.test(text) |
| 54 | + text.match(/hello/g) |
| 55 | + `, |
| 56 | + ` |
| 57 | + const text = somethingElse() |
| 58 | + /hello/g.test(text) |
| 59 | + text.match(/hello/g) |
| 60 | + `, |
| 61 | + ` |
| 62 | + import somethingElse from 'somethingElse' |
| 63 | + somethingElse.lookup(/hello/g) |
| 64 | + `, |
| 65 | + ` |
| 66 | + import { screen } from '@testing-library/dom' |
| 67 | + screen.notAQuery(/hello/g) |
| 68 | + `, |
| 69 | + ` |
| 70 | + import { screen } from '@testing-library/dom' |
| 71 | + screen.notAQuery('button', {name: /hello/g}) |
| 72 | + `, |
| 73 | + ` |
| 74 | + const utils = render(<Component/>) |
| 75 | + utils.notAQuery('button', {name: /hello/i}) |
| 76 | + `, |
| 77 | + ` |
| 78 | + const utils = render(<Component/>) |
| 79 | + utils.notAQuery(/hello/i) |
| 80 | + `, |
| 81 | + ], |
| 82 | + invalid: [ |
| 83 | + { |
| 84 | + code: ` |
| 85 | + import { screen } from '@testing-library/dom' |
| 86 | + screen.getByText(/hello/g)`, |
| 87 | + errors: [ |
| 88 | + { |
| 89 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 90 | + line: 3, |
| 91 | + column: 26, |
| 92 | + }, |
| 93 | + ], |
| 94 | + output: ` |
| 95 | + import { screen } from '@testing-library/dom' |
| 96 | + screen.getByText(/hello/)`, |
| 97 | + }, |
| 98 | + { |
| 99 | + code: ` |
| 100 | + import { screen } from '@testing-library/dom' |
| 101 | + screen.findByRole('button', {name: /hellogg/g})`, |
| 102 | + errors: [ |
| 103 | + { |
| 104 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 105 | + line: 3, |
| 106 | + column: 44, |
| 107 | + }, |
| 108 | + ], |
| 109 | + output: ` |
| 110 | + import { screen } from '@testing-library/dom' |
| 111 | + screen.findByRole('button', {name: /hellogg/})`, |
| 112 | + }, |
| 113 | + { |
| 114 | + code: ` |
| 115 | + import { screen } from '@testing-library/dom' |
| 116 | + screen.findByRole('button', {otherProp: true, name: /hello/g})`, |
| 117 | + errors: [ |
| 118 | + { |
| 119 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 120 | + line: 3, |
| 121 | + column: 61, |
| 122 | + }, |
| 123 | + ], |
| 124 | + output: ` |
| 125 | + import { screen } from '@testing-library/dom' |
| 126 | + screen.findByRole('button', {otherProp: true, name: /hello/})`, |
| 127 | + }, |
| 128 | + { |
| 129 | + code: ` |
| 130 | + const utils = render(<Component/>) |
| 131 | + utils.findByRole('button', {name: /hello/ig})`, |
| 132 | + errors: [ |
| 133 | + { |
| 134 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 135 | + line: 3, |
| 136 | + column: 43, |
| 137 | + }, |
| 138 | + ], |
| 139 | + output: ` |
| 140 | + const utils = render(<Component/>) |
| 141 | + utils.findByRole('button', {name: /hello/i})`, |
| 142 | + }, |
| 143 | + { |
| 144 | + code: ` |
| 145 | + const {queryAllByLabelText} = render(<Component/>) |
| 146 | + queryAllByLabelText(/hello/gi)`, |
| 147 | + errors: [ |
| 148 | + { |
| 149 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 150 | + line: 3, |
| 151 | + column: 29, |
| 152 | + }, |
| 153 | + ], |
| 154 | + output: ` |
| 155 | + const {queryAllByLabelText} = render(<Component/>) |
| 156 | + queryAllByLabelText(/hello/i)`, |
| 157 | + }, |
| 158 | + { |
| 159 | + code: ` |
| 160 | + import { within } from '@testing-library/dom' |
| 161 | + within(element).findByRole('button', {name: /hello/igm})`, |
| 162 | + errors: [ |
| 163 | + { |
| 164 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 165 | + line: 3, |
| 166 | + column: 53, |
| 167 | + }, |
| 168 | + ], |
| 169 | + output: ` |
| 170 | + import { within } from '@testing-library/dom' |
| 171 | + within(element).findByRole('button', {name: /hello/im})`, |
| 172 | + }, |
| 173 | + { |
| 174 | + code: ` |
| 175 | + import { within } from '@testing-library/dom' |
| 176 | + within(element).queryAllByText(/hello/ig)`, |
| 177 | + errors: [ |
| 178 | + { |
| 179 | + messageId: 'noGlobalRegExpFlagInQuery', |
| 180 | + line: 3, |
| 181 | + column: 40, |
| 182 | + }, |
| 183 | + ], |
| 184 | + output: ` |
| 185 | + import { within } from '@testing-library/dom' |
| 186 | + within(element).queryAllByText(/hello/i)`, |
| 187 | + }, |
| 188 | + ], |
| 189 | +}); |
0 commit comments