Element queries
Find rendered elements the way a screen reader does: by role, by accessibility label or by text. import { screen, within } from '@blotch/angular-lynx-testing-library';
Usage
render()'s result has every query, bound to itscontainer.screenhas them bound todocument.body.within()binds them to one element.
*ByRole
Testing Library's *ByRole, reading Lynx's accessibility attributes as a screen reader does. It also finds what Testing Library finds, such as an <input> as a textbox.
<image>is animgunless it setsaccessibility-traits. Other traits, such astext, give no role.accessibility-headingis aheading, as Android reads it. This adds to the roles from the traits.accessibility-role-descriptionofbutton,switch,checkbox,imageorprogressbaris that role, as Android reads it.imageisimg. This adds to the roles from the traits too.
Which elements match
- Only reachable elements match. A screen reader reaches
<text>,<image>, and other elements withaccessibility-element="true". accessibility-elements-hiddenhides an element and everything in it.- A
tabbaris read from the container around the tabs, so it needs noaccessibility-element. - Pass
hidden: trueto include the rest.
Options
nameis theaccessibility-label.- A
<text>without one is named by its text. - A
<view>isn't named by the text inside it, since iOS doesn't read it. - An
accessibility-statuscomes first, as iOS and Android read it:{ name: 'Unread, Inbox' }.
- A
selectedis theselectedtrait:accessibility-traits="button,selected".- An active
routerLinkActiveelement gets it, and[lynxAccessibilityTraits]="{ selected: true }"adds it. - iOS reads a comma-separated list, but Android reads only one trait.
- An active
value: { text }is theaccessibility-value.- Other ARIA states, such as
checkedorexpanded, have no Lynx attribute. Aswitchorcheckboxfrom a role description never has them.
ARIA in a template
The renderer turns these ARIA attributes into Lynx's, so the queries find them as VoiceOver and TalkBack do:
aria-checked, aria-expanded, aria-pressed and the rest of ARIA do nothing on a device. Don't test for them: a screen reader won't read them. Use ARIA attributes has the details.
*ByAccessibilityLabel
Finds elements by their accessibility-label, the text a screen reader reads for them. Testing Library's *ByLabelText reads <label> and aria-* attributes, not Lynx's accessibility-label.
label: a string, a RegExp or a function, as forgetByText.options:exact: falsematches part of the label in any case.normalizerchanges how whitespace is trimmed.- Variants: the same six as Testing Library's queries.
findBy…waits for the element, 1 second by default.
screen
Every query, bound to document.body, with Testing Library's debug() and logTestingPlaygroundURL().
within(element, queries?)
Binds every query to element, to search one part of the tree.
- Pass
queriesto bind only those. getQueriesForElementis the same function.
queries
Every query, unbound, each taking the container first. These are Testing Library's, with *ByRole and *ByAccessibilityLabel from this library.
- Each is also exported on its own, such as
getByAccessibilityLabel(container, 'Close'). - The
*ByRoleexports aregetByRole,getAllByRole,queryByRole,queryAllByRole,findByRoleandfindAllByRole. - The
*ByAccessibilityLabelexports follow the same six names.
Text written straight in a view
getByText finds the element whose own text matches, as in a browser. For text written straight in a <view> or <div>, that's the plain <text> the runtime draws it in (see HTML tags). Its parentElement is the <view>.
