dom-testing-library icon indicating copy to clipboard operation
dom-testing-library copied to clipboard

expose more functions from role-helpers

Open kkoo95 opened this issue 10 months ago • 3 comments

Describe the feature you'd like:

Expose all the computeAriaXXX functions from role-helpers to provide greater flexibility for test authoring. This would enable writing tests like this:

const optionsList = within(getByRole(document.body, 'listbox', { name: /Options list/ }));

expect(
    optionsList.queryAllByRole('option').map(e => ({
        name: computeAccessibleName(e),
        selected: computeAriaSelected(e),
    }))
).toEqual([
    {  name: 'manager1', selected: true },
    {  name: 'external2', selected: false },
]);

Here, we combine the verification of the order and the specific ARIA properties for each option into a single expectation. Otherwise, it would require at least two steps to achieve the same result.

expect(optionsList.queryAllByRole('option').map(e => computeAccessibleName(e))).toEqual(['manager1', 'external2']);
expect(optionsList.queryByRole('option', { name: 'manager1', selected: true })).toBeDefined();
expect(optionsList.queryByRole('option', { name: 'external2', selected: false })).toBeDefined();

kkoo95 avatar Apr 29 '25 16:04 kkoo95

Thanks for opening this one @kkoo95. The computeAccessibleName function is imported from dom-accessibility-api, if you want that one, you can npm install and use it. https://github.com/eps1lon/dom-accessibility-api/tree/main

MatanBobi avatar May 02 '25 14:05 MatanBobi

hum it's not what i was asking for 😅 @MatanBobi

I noticed indeed that name and description were already available. But I was talking about the other helpers such as computeAriaSelected (see src/role-helpers.js)

kkoo95 avatar May 06 '25 19:05 kkoo95

Missed that, sorry. IMO this should also probably be exported from DOM-accessibility-api and shouldn't live in our repo. I currently don't have the capacity to move that functionality there but I do think that we should do it.

MatanBobi avatar May 06 '25 20:05 MatanBobi