import { getByLgId, queryBySelector } from '@lg-tools/test-harnesses'; import { getLgIds as getLgFormFieldIds } from '@leafygreen-ui/form-field'; import { LgIdString } from '@leafygreen-ui/lib'; import { getLgIds as getLgTypographyLgIds } from '@leafygreen-ui/typography'; import { DEFAULT_LGID_ROOT, getLgIds } from '../utils/getLgIds'; import { TestUtilsReturnType } from './getTestUtils.types'; export const getTestUtils = ( lgId: LgIdString = DEFAULT_LGID_ROOT, ): TestUtilsReturnType => { const lgIds = getLgIds(lgId); const lgFormFieldIds = getLgFormFieldIds(lgIds.root); const typographyLgIds = getLgTypographyLgIds(lgIds.root); /** * Queries the DOM for the element using the `data-lgid` data attribute. * Will throw if no element is found. */ const element = getByLgId!(lgId); /** * Queries the `element` for the label element. Will return `null` if the label is not found. */ const getLabel = queryBySelector( element, `[data-lgid=${typographyLgIds.label}]`, ); /** * Queries the `element` for the description element. Will return `null` if the desription is not found. */ const getDescription = queryBySelector( element, `[data-lgid=${typographyLgIds.description}]`, ); /** * Queries the `element` for the input element. */ const getInput = queryBySelector( element, 'input', ) as HTMLInputElement; /** * Queries the `element` for the error message element. Will return `null` if the error message is not found. */ const getErrorMessage = queryBySelector( element, `[data-lgid=${lgFormFieldIds.errorMessage}]`, ); /** * Queries the `element` for aria-disabled. */ const isInputDisabled = () => { const ariaDisabled = queryBySelector( element, '[aria-disabled="true"]', ); return !!ariaDisabled; }; /** * Returns the input value. */ const getInputValue = () => { return getInput.value; }; /** * Queries the `element` for the checkmark Icon. */ const isValid = () => { const checkmarkIcon = queryBySelector(element, 'svg'); if (!checkmarkIcon) return false; const titleElement = checkmarkIcon.querySelector('title'); return titleElement?.textContent === 'Valid'; }; /** * Queries the `element` for the warning Icon. */ const isError = () => { const warningIcon = queryBySelector(element, 'svg'); if (!warningIcon) return false; const titleElement = warningIcon.querySelector('title'); return titleElement?.textContent === 'Error'; }; /** * Queries the `element` for the warning Icon. */ const isOptional = () => { const optionalEl = queryBySelector( element, `[data-lgid=${lgFormFieldIds.optional}]`, ); return !!optionalEl; }; return { getLabel: () => getLabel, getDescription: () => getDescription, getInput: () => getInput, getErrorMessage: () => getErrorMessage, isDisabled: () => isInputDisabled(), isValid: () => isValid(), isError: () => isError(), getInputValue: () => getInputValue(), isOptional: () => isOptional(), }; };