MCPcopy
hub / github.com/testing-library/dom-testing-library / getLabels

Function getLabels

src/label-helpers.ts:57–82  ·  view source on GitHub ↗
(
  container: Element,
  element: Element,
  {selector = '*'} = {},
)

Source from the content-addressed store, hash-verified

55}
56
57function getLabels(
58 container: Element,
59 element: Element,
60 {selector = '*'} = {},
61): {content: string | null; formControl: HTMLElement | null}[] {
62 const ariaLabelledBy = element.getAttribute('aria-labelledby')
63 const labelsId = ariaLabelledBy ? ariaLabelledBy.split(' ') : []
64 return labelsId.length
65 ? labelsId.map(labelId => {
66 const labellingElement = container.querySelector<HTMLElement>(
67 `[id="${labelId}"]`,
68 )
69 return labellingElement
70 ? {content: getLabelContent(labellingElement), formControl: null}
71 : {content: '', formControl: null}
72 })
73 : Array.from(getRealLabels(element)).map(label => {
74 const textToMatch = getLabelContent(label)
75 const formControlSelector =
76 'button, input, meter, output, progress, select, textarea'
77 const labelledFormControl = Array.from(
78 label.querySelectorAll<HTMLElement>(formControlSelector),
79 ).filter(formControlElement => formControlElement.matches(selector))[0]
80 return {content: textToMatch, formControl: labelledFormControl}
81 })
82}
83
84export {getLabels, getRealLabels, getLabelContent}

Callers 2

getSuggestedQueryFunction · 0.90
queryAllByLabelTextFunction · 0.90

Calls 2

getLabelContentFunction · 0.85
getRealLabelsFunction · 0.85

Tested by

no test coverage detected