|
1 | 1 | import {configure} from '../config'
|
2 |
| -import {screen} from '..' |
3 |
| -import {renderIntoDocument} from './helpers/test-utils' |
| 2 | +import {screen, getSuggestedQuery} from '..' |
| 3 | +import {renderIntoDocument, render} from './helpers/test-utils' |
4 | 4 |
|
5 | 5 | beforeAll(() => {
|
6 | 6 | configure({throwSuggestions: true})
|
@@ -307,3 +307,45 @@ test(`should suggest getByTitle`, () => {
|
307 | 307 | // `getByText` will always be the suggested query as it is higher up the list.
|
308 | 308 | expect(() => screen.getByTestId('svg')).toThrowError(/getByText\('Close'\)/)
|
309 | 309 | })
|
| 310 | + |
| 311 | +test('getSuggestedQuery handles `variant` and defaults to `get`', () => { |
| 312 | + const button = render(`<button>submit</button>`).container.firstChild |
| 313 | + |
| 314 | + expect(getSuggestedQuery(button).toString()).toMatch(/getByRole/) |
| 315 | + expect(getSuggestedQuery(button, 'get').toString()).toMatch(/getByRole/) |
| 316 | + expect(getSuggestedQuery(button, 'getAll').toString()).toMatch(/getAllByRole/) |
| 317 | + expect(getSuggestedQuery(button, 'query').toString()).toMatch(/queryByRole/) |
| 318 | + expect(getSuggestedQuery(button, 'queryAll').toString()).toMatch( |
| 319 | + /queryAllByRole/, |
| 320 | + ) |
| 321 | + expect(getSuggestedQuery(button, 'find').toString()).toMatch(/findByRole/) |
| 322 | + expect(getSuggestedQuery(button, 'findAll').toString()).toMatch( |
| 323 | + /findAllByRole/, |
| 324 | + ) |
| 325 | +}) |
| 326 | + |
| 327 | +test('getSuggestedQuery returns rich data for tooling', () => { |
| 328 | + const button = render(`<button>submit</button>`).container.firstChild |
| 329 | + |
| 330 | + expect(getSuggestedQuery(button)).toMatchObject({ |
| 331 | + queryName: 'Role', |
| 332 | + queryMethod: 'getByRole', |
| 333 | + queryArgs: ['button', {name: /submit/i}], |
| 334 | + variant: 'get', |
| 335 | + }) |
| 336 | + |
| 337 | + expect(getSuggestedQuery(button).toString()).toEqual( |
| 338 | + `getByRole('button', { name: /submit/i })`, |
| 339 | + ) |
| 340 | + |
| 341 | + const div = render(`<a>cancel</a>`).container.firstChild |
| 342 | + |
| 343 | + expect(getSuggestedQuery(div)).toMatchObject({ |
| 344 | + queryName: 'Text', |
| 345 | + queryMethod: 'getByText', |
| 346 | + queryArgs: ['cancel'], |
| 347 | + variant: 'get', |
| 348 | + }) |
| 349 | + |
| 350 | + expect(getSuggestedQuery(div).toString()).toEqual(`getByText('cancel')`) |
| 351 | +}) |
0 commit comments