import { uniq } from "lodash" ; import { ReactTestInstance } from "react-test-renderer" ; import { queryAllByText } from "./byText" ; import matchText , { TextMatchOptions , TextMatchPattern } from "./matchText" ; const ariaLabelMatcher = ( pattern , options ? ) => ( i ) => { if ( typeof i . type !== "string" || ! i . props [ "aria-label" ] ) { return false ; } return matchText ( pattern , i . props [ "aria-label" ] , { collapseWhitespace : false , ... options , } ) ; } ; interface SelectorOptions { selector ? : string ; } export function getByLabelText ( container , pattern , options ? ) { const results = queryAllByLabelText ( container , pattern , options ) ; if ( results . length === 1 ) { return results [ 0 ] ; } if ( results . length === 0 ) { throw new Error ( `Couldn't find element with label text ${ pattern }` ) ; } throw new Error ( `Found multiple elements with label text ${ pattern }` ) ; } export function getAllByLabelText ( container , pattern , options ? ) { const results = queryAllByLabelText ( container , pattern , options ) ; if ( results . length ) { return results ; } throw new Error ( `Could't find element with label text ${ pattern }` ) ; } export function queryByLabelText ( container , pattern , options ? ) { const results = queryAllByLabelText ( container , pattern , options ) ; if ( results . length ) { return results [ 0 ] ; } return null ; } export function queryAllByLabelText ( container , pattern , options ? ) { let matches = container . findAll ( ariaLabelMatcher ( pattern , options ) ) ; queryAllByText ( container , pattern , options ) . forEach ( ( i ) => { if ( typeof i . type !== "string" ) { return ; } if ( i . props . id ) { try { matches . push ( container . find ( ( x ) => typeof x . type === "string" && x . props [ "aria-labelledby" ] === i . props . id ) ) ; } catch { } } } ) ; queryAllByText ( container , pattern , { ... options , selector : "label" } ) . forEach ( ( i ) => { if ( i . props . htmlFor ) { try { matches . push ( container . find ( ( x ) => typeof x . type === "string" && x . props . id === i . props . htmlFor ) ) ; } catch { } } } ) ; if ( options && options . selector ) { matches = matches . filter ( ( m ) => m . type === options . selector ) ; } return uniq ( matches ) ; }