diff --git a/src/queries/unsafe-props.ts b/src/queries/unsafe-props.ts deleted file mode 100644 index b01d7261..00000000 --- a/src/queries/unsafe-props.ts +++ /dev/null @@ -1,80 +0,0 @@ -import prettyFormat from 'pretty-format'; -import { HostElement } from 'universal-test-renderer'; -import { ErrorWithStack, prepareErrorMessage } from '../helpers/errors'; -import { createQueryByError } from '../helpers/errors'; -import { findAllByProps } from '../helpers/find-all'; - -const UNSAFE_getByProps = ( - instance: HostElement, -): ((props: { [propName: string]: any }) => HostElement) => - function getByPropsFn(props: { [propName: string]: any }) { - try { - const results = findAllByProps(instance, props); - if (results.length === 0) { - throw new ErrorWithStack( - `No instances found with props:\n${prettyFormat(props)}`, - getByPropsFn, - ); - } - if (results.length > 1) { - throw new ErrorWithStack( - `Found multiple instances with props:\n${prettyFormat(props)}`, - getByPropsFn, - ); - } - return results[0]; - } catch (error) { - throw new ErrorWithStack(prepareErrorMessage(error), getByPropsFn); - } - }; - -const UNSAFE_getAllByProps = ( - instance: HostElement, -): ((props: { [propName: string]: any }) => Array) => - function getAllByPropsFn(props: { [propName: string]: any }) { - const results = findAllByProps(instance, props); - if (results.length === 0) { - throw new ErrorWithStack( - `No instances found with props:\n${prettyFormat(props)}`, - getAllByPropsFn, - ); - } - return results; - }; - -const UNSAFE_queryByProps = ( - instance: HostElement, -): ((props: { [propName: string]: any }) => HostElement | null) => - function queryByPropsFn(props: { [propName: string]: any }) { - try { - return UNSAFE_getByProps(instance)(props); - } catch (error) { - return createQueryByError(error, queryByPropsFn); - } - }; - -const UNSAFE_queryAllByProps = - (instance: HostElement): ((props: { [propName: string]: any }) => Array) => - (props: { [propName: string]: any }) => { - try { - return UNSAFE_getAllByProps(instance)(props); - } catch { - return []; - } - }; - -// Unsafe aliases -export type UnsafeByPropsQueries = { - UNSAFE_getByProps: (props: { [key: string]: any }) => HostElement; - UNSAFE_getAllByProps: (props: { [key: string]: any }) => Array; - UNSAFE_queryByProps: (props: { [key: string]: any }) => HostElement | null; - UNSAFE_queryAllByProps: (props: { [key: string]: any }) => Array; -}; - -// TODO: migrate to makeQueries pattern -export const bindUnsafeByPropsQueries = (instance: HostElement): UnsafeByPropsQueries => ({ - UNSAFE_getByProps: UNSAFE_getByProps(instance), - UNSAFE_getAllByProps: UNSAFE_getAllByProps(instance), - UNSAFE_queryByProps: UNSAFE_queryByProps(instance), - UNSAFE_queryAllByProps: UNSAFE_queryAllByProps(instance), -}); diff --git a/src/queries/unsafe-type.ts b/src/queries/unsafe-type.ts deleted file mode 100644 index cff25130..00000000 --- a/src/queries/unsafe-type.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { HostElement } from 'universal-test-renderer'; -import { ErrorWithStack } from '../helpers/errors'; -import { createQueryByError } from '../helpers/errors'; -import { findAll } from '../helpers/find-all'; - -const UNSAFE_getByType = (instance: HostElement): ((type: string) => HostElement) => - function getByTypeFn(type: string) { - const results = findAllByType(instance, type); - if (results.length === 0) { - throw new ErrorWithStack(`No instances found with type:\n${type}`, getByTypeFn); - } - if (results.length > 1) { - throw new ErrorWithStack(`Found multiple instances with type:\n${type}`, getByTypeFn); - } - return results[0]; - }; - -const UNSAFE_getAllByType = (instance: HostElement): ((type: string) => Array) => - function getAllByTypeFn(type: string) { - const results = findAllByType(instance, type); - if (results.length === 0) { - throw new ErrorWithStack(`No instances found with type:\n${type}`, getAllByTypeFn); - } - return results; - }; - -const UNSAFE_queryByType = (instance: HostElement): ((type: string) => HostElement | null) => - function queryByTypeFn(type: string) { - try { - return UNSAFE_getByType(instance)(type); - } catch (error) { - return createQueryByError(error, queryByTypeFn); - } - }; - -const UNSAFE_queryAllByType = - (instance: HostElement): ((type: string) => Array) => - (type: string) => { - try { - return UNSAFE_getAllByType(instance)(type); - } catch { - return []; - } - }; - -// Unsafe aliases -export type UnsafeByTypeQueries = { - UNSAFE_getByType: (type: string) => HostElement; - UNSAFE_getAllByType: (type: string) => Array; - UNSAFE_queryByType: (type: string) => HostElement | null; - UNSAFE_queryAllByType: (type: string) => Array; -}; - -// TODO: migrate to makeQueries pattern -export const bindUnsafeByTypeQueries = (instance: HostElement): UnsafeByTypeQueries => ({ - UNSAFE_getByType: UNSAFE_getByType(instance), - UNSAFE_getAllByType: UNSAFE_getAllByType(instance), - UNSAFE_queryByType: UNSAFE_queryByType(instance), - UNSAFE_queryAllByType: UNSAFE_queryAllByType(instance), -}); - -function findAllByType(instance: HostElement, type: string) { - return findAll(instance, (element) => element.type === type); -} diff --git a/src/screen.ts b/src/screen.ts index dcfd9f97..ac25b088 100644 --- a/src/screen.ts +++ b/src/screen.ts @@ -58,14 +58,6 @@ const defaultScreen: Screen = { queryAllByRole: notImplemented, findByRole: notImplemented, findAllByRole: notImplemented, - UNSAFE_getByProps: notImplemented, - UNSAFE_getAllByProps: notImplemented, - UNSAFE_queryByProps: notImplemented, - UNSAFE_queryAllByProps: notImplemented, - UNSAFE_getByType: notImplemented, - UNSAFE_getAllByType: notImplemented, - UNSAFE_queryByType: notImplemented, - UNSAFE_queryAllByType: notImplemented, getByPlaceholderText: notImplemented, getAllByPlaceholderText: notImplemented, queryByPlaceholderText: notImplemented, diff --git a/src/within.ts b/src/within.ts index fc46dbde..cd1abbd5 100644 --- a/src/within.ts +++ b/src/within.ts @@ -1,25 +1,21 @@ import { HostElement } from 'universal-test-renderer'; -import { bindByTextQueries } from './queries/text'; -import { bindByTestIdQueries } from './queries/test-id'; import { bindByDisplayValueQueries } from './queries/display-value'; -import { bindByPlaceholderTextQueries } from './queries/placeholder-text'; -import { bindByLabelTextQueries } from './queries/label-text'; import { bindByHintTextQueries } from './queries/hint-text'; +import { bindByLabelTextQueries } from './queries/label-text'; +import { bindByPlaceholderTextQueries } from './queries/placeholder-text'; import { bindByRoleQueries } from './queries/role'; -import { bindUnsafeByTypeQueries } from './queries/unsafe-type'; -import { bindUnsafeByPropsQueries } from './queries/unsafe-props'; +import { bindByTestIdQueries } from './queries/test-id'; +import { bindByTextQueries } from './queries/text'; export function within(instance: HostElement) { return { - ...bindByTextQueries(instance), - ...bindByTestIdQueries(instance), ...bindByDisplayValueQueries(instance), ...bindByPlaceholderTextQueries(instance), - ...bindByLabelTextQueries(instance), ...bindByHintTextQueries(instance), + ...bindByLabelTextQueries(instance), ...bindByRoleQueries(instance), - ...bindUnsafeByTypeQueries(instance), - ...bindUnsafeByPropsQueries(instance), + ...bindByTestIdQueries(instance), + ...bindByTextQueries(instance), }; }