diff --git a/src/components/Collapse/Collapse.test.tsx b/src/components/Collapse/Collapse.test.tsx index 1848cc82..64fd45a0 100644 --- a/src/components/Collapse/Collapse.test.tsx +++ b/src/components/Collapse/Collapse.test.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; -import { getJestSelectors } from '../../utils'; +import { getJestSelectors } from '@volkovlabs/jest-selectors'; import { Collapse } from './Collapse'; type Props = React.ComponentProps; diff --git a/src/utils/tests.ts b/src/utils/tests.ts index e0ac2249..45d56fa0 100644 --- a/src/utils/tests.ts +++ b/src/utils/tests.ts @@ -1,55 +1,6 @@ -import { BoundFunctions, GetByBoundAttribute, Queries, Screen } from '@testing-library/react'; +import { getJestSelectors } from '@volkovlabs/jest-selectors'; import { TestIds } from '../constants'; -/** - * Jest Selector - */ -type JestSelector = ( - noThrowOnNotFound?: boolean, - ...args: Args -) => ReturnType; - -/** - * Jest Selectors - */ -type JestSelectors = { - [K in keyof T]: T[K] extends (...args: infer Args) => void ? JestSelector : JestSelector<[]>; -}; - -/** - * Get Jest Selectors - * @param selectors - * @param enforceTestIdSelectorForKeys - */ -export const getJestSelectors = - >( - selectors: Selectors, - enforceTestIdSelectorForKeys: Array = [] - ): ((screen: Screen | BoundFunctions) => JestSelectors) => - (screen) => { - return Object.entries(selectors).reduce((acc, [key, selector]) => { - /** - * Get Element function - * @param noThrowOnNotFound - * @param args - */ - const getElement = (noThrowOnNotFound = false, ...args: unknown[]) => { - const value = typeof selector === 'function' ? selector(...args) : selector; - - if (value.startsWith('data-testid') || enforceTestIdSelectorForKeys.includes(key as keyof Selectors)) { - return noThrowOnNotFound ? screen.queryByTestId(value) : screen.getByTestId(value); - } - - return noThrowOnNotFound ? screen.queryByLabelText(value) : screen.getByLabelText(value); - }; - - return { - ...acc, - [key]: getElement, - }; - }, {} as JestSelectors); - }; - /** * Get Custom Code Editor Selectors */