///
/**
* Any locator, that is locator that matches any other locators (for use in unit tests).
*/
export type AnyLocator = ((parameters?: any) => AnyMark) & WithLocator;
/**
* A mark that matches any locator (for use in unit tests).
*/
export type AnyMark = WithMark & WithErrorAttribute;
/**
* Attributes object.
*/
export type Attributes = Readonly>;
/**
* Symbol key for saving component parameters of locator.
*/
export declare const COMPONENT_PARAMETERS: unique symbol;
/**
* `createLocator` overload for component locator.
*/
export type CreateComponentLocator = >(
this: void,
properties: PropertiesError extends string ? PropertiesError : Properties,
) => CreateLocator extends string ? never : Properties>;
/**
* Presentation of `createLocator` function in types.
* Creates locator type by properties and optional `MappingResult` type.
*/
export type CreateLocator<
PropertiesOrRootLocator extends Partial | WithLocator,
MappingResult = HiddenKey,
> = true extends
| (PropertiesOrRootLocator extends Partial | WithLocator
? false
: true)
| IsEqual
| IsEqual
? unknown
: IsEqual extends true
? PropertiesOrRootLocator extends WithLocator
? PropertiesOrRootLocator
: PropertiesOrRootLocator extends Partial
? IsEqual, unknown> extends true
? unknown
: IsEqual, object> extends true
? unknown
: LocatorFromLocatorTree>
: unknown
: PropertiesOrRootLocator extends WithLocator
? NormalizeTree, MappingResult>
: unknown;
/**
* Type of `createRootLocator` function (with overloads).
*/
export type CreateRootLocatorFunction = CreateRootLocator & CreateRootLocatorWithMapping;
/**
* Symbol key for generating type error on HTML elements.
*/
export declare const ERROR_ATTRIBUTE: GetErrorAttribute;
/**
* Filled options of root locator, maybe with mapping attributes function.
*/
export type FilledRootOptions = Partial> & RootOptionsWithDefaults;
/**
* Presentation of `getLocatorParameters` function in types.
* Get type of parameters of component locator by component properties.
*/
export type GetLocatorParameters<
Properties extends Partial,
> = true extends
| (Properties extends Partial ? false : true)
| IsEqual
| IsEqual>, {}>
? unknown
: ExtractNodeComponentParameters>;
/**
* Type of `getLocatorParameters` function.
*/
export type GetLocatorParametersFunction = <
Properties extends Partial,
>(
this: void,
properties: PropertiesError extends string ? PropertiesError : Properties,
) => GetLocatorParameters extends string ? never : Properties>;
/**
* Symbol key for saving hidden normalized tree.
*/
export declare const HIDDEN: unique symbol;
/**
* Symbol key for tree of empty locator (i.e. `Locator`).
*/
export declare const IS_EMPTY_LOCATOR: unique symbol;
/**
* Creates component locator type by locator description and locator parameters.
*/
export type Locator<
Description extends LocatorDescriptionConstraint,
Parameters extends ParametersConstraint = void,
ComponentParameters extends ParametersConstraint | 'sameParameters' = void,
> = true extends
| (Description extends LocatorDescriptionConstraint ? false : true)
| (Parameters extends ParametersConstraint ? false : true)
| (ComponentParameters extends ParametersConstraint | 'sameParameters' ? false : true)
| IsEqual
| IsEqual
| IsEqual
? unknown
: LocatorFromLocatorTree<
LocatorTree<
Description,
AddUndefinedIfRequiredParametersEmpty,
AddUndefinedIfRequiredParametersEmpty<
IsEqual extends true
? Parameters
: ComponentParameters
>
>
>;
/**
* Symbol key to define locator.
*/
export declare const LOCATOR: unique symbol;
/**
* Constraint of locator, that is the argument of `Mark`.
*/
export type LocatorConstraint = WithLocator;
/**
* Constraint of description of locator tree, that is the first argument of `Locator` and `Node`.
*/
export type LocatorDescriptionConstraint = Readonly<
Record &
Partial<
WithComponentParameters &
WithHidden &
WithLocator &
WithMark &
WithNode &
WithParameters
>
> | void;
/**
* Creates locator type by locator description and locator parameters
* for component, behaves like an element.
*/
export type LocatorOfElement<
Description extends LocatorDescriptionConstraint,
Parameters extends ParametersConstraint = void,
ComponentParameters extends ParametersConstraint | 'sameParameters' = void,
> = true extends
| (Description extends LocatorDescriptionConstraint ? false : true)
| (Parameters extends ParametersConstraint ? false : true)
| (ComponentParameters extends ParametersConstraint | 'sameParameters' ? false : true)
| IsEqual
| IsEqual
| IsEqual
? unknown
: LocatorFromLocatorTree<
LocatorTree<
Description,
AddUndefinedIfRequiredParametersEmpty,
AddUndefinedIfRequiredParametersEmpty<
IsEqual extends true
? Parameters
: ComponentParameters
>,
'LocatorOfElement'
>
>;
/**
* Creates mark with component locator type for component properties.
*/
export type Mark = true extends
| (SomeLocator extends LocatorConstraint ? false : true)
| IsEqual
| IsEqual, unknown>
? unknown
: WithMark> &
WithErrorAttribute<
| 'The mark of locator should be removed from spreaded properties using the removeMarkFromProperties'
| CannotMarkElementMessage
>;
/**
* Symbol key for saving locator tree in mark (in properties object).
*/
export declare const MARK: unique symbol;
/**
* Creates node locator type by locator description and locator parameters.
*/
export type Node<
Description extends LocatorDescriptionConstraint,
Parameters extends ParametersConstraint = void,
> = true extends
| (Description extends LocatorDescriptionConstraint ? false : true)
| (Parameters extends ParametersConstraint ? false : true)
| IsEqual
| IsEqual
? unknown
: WithNode<
LocatorTree, void, 'Node'>
> &
WithErrorAttribute<'Node cannot be used here'>;
/**
* Symbol key for saving locator tree of node locator.
*/
export declare const NODE: unique symbol;
/**
* Symbol key for saving locator parameters.
*/
export declare const PARAMETERS: unique symbol;
/**
* Constraint of locator parameters, that is the second argument of `Locator` and `Node`.
*/
export type ParametersConstraint = Attributes | void;
/**
* Constraint of properties with mark, that is the argument of `createLocator` and `removeMarkFromProperties`.
*/
export type PropertiesWithMarkConstraint = WithMark;
/**
* Constraint of properties with mark with parameters, that is the argument of `getLocatorParameters`.
*/
export type PropertiesWithMarkWithParametersConstraint = WithMark;
/**
* Presentation of `removeMarkFromProperties` function in types.
* Returns type of properties without locator mark.
*/
export type RemoveMarkFromProperties> =
true extends IsEqual ? unknown : Omit;
/**
* Type of `removeMarkFromProperties` function.
*/
export type RemoveMarkFromPropertiesFunction = <
Properties extends Partial,
>(
this: void,
properties: PropertiesError extends string ? PropertiesError : Properties,
) => RemoveMarkFromProperties extends string ? never : Properties>;
/**
* Options of root locator (as `createLocator` second argument).
*/
export type RootOptions = Partial;
/**
* Symbol key for saving locator tree for prototype keys.
*/
export declare const TREE: unique symbol;
/**
* Adds `IS_EMPTY_LOCATOR` key if locator tree is empty (i.e. for `Locator`).
*/
type AddIsEmptyLocatorKeyIfNeeded =
IsEqual extends true ? WithIsEmptyLocator : {};
/**
* Adds `undefined` to parameters if there are no required parameters.
*/
type AddUndefinedIfRequiredParametersEmpty =
IsRequiredParametersEmpty extends true ? Parameters | undefined : Parameters;
/**
* The function part of the base node.
*/
type BaseLocatorFunction = (
this: void,
...arguments: LocatorArguments
) => Return;
/**
* Base node of locator tree (by locator parameters, locator component parameters,
* optional subtree and `IsChildLocator` flag).
*/
type BaseNode<
Parameters,
ComponentParameters,
Subtree = undefined,
IsChildLocator extends boolean = false,
ParametersInArguments = IsChildLocator extends true ? ComponentParameters : Parameters,
> = (IsParametersEmpty extends true
? ((this: void) => LocatorCallResult) &
WithErrorAttribute<'The locator should be called'>
: BaseLocatorFunction> &
WithErrorAttribute<'The locator should be called (maybe with parameters)'>) &
(IsParametersEmpty extends true
? {}
: WithComponentParameters) &
(IsParametersEmpty extends true ? {} : WithParameters);
/**
* Error message for situations where a HTML element is marked with a component locator.
*/
type CannotMarkElementMessage = 'Cannot mark HTML element with component locator';
/**
* Type of symbol key for saving component parameters of locator.
*/
type ComponentParametersKey = typeof COMPONENT_PARAMETERS;
/**
* `createLocator` overload for root locator.
*/
type CreateRootLocator = (
this: void,
pathPrefix: IsEqual extends true ? never : string,
rootOptions?: Partial,
) => CreateLocator;
/**
* `createLocator` overload for root locator with attributes mapping.
*/
type CreateRootLocatorWithMapping = (
this: void,
pathPrefix: IsEqual extends true ? never : string,
rootOptions: Partial & MapAttributes,
) => CreateLocator;
/**
* Base type of error attribute value for all HTML elements.
*/
type ErrorAttributeBaseType =
| 'https://www.npmjs.com/package/create-locator#error-attribute'
| undefined;
/**
* Type of symbol key for generating type error on HTML elements.
*/
type ErrorAttributeKey = typeof ERROR_ATTRIBUTE;
/**
* Extracts component parameters from some base node.
*/
type ExtractNodeComponentParameters = SomeNode extends WithComponentParameters
? SomeNode[ComponentParametersKey]
: {};
/**
* Extracts parameters from some base or normalized node.
*/
type ExtractNodeParameters = SomeNode extends WithParameters
? SomeNode[ParametersKey]
: {};
/**
* Get error attribute key from `React.AriaAttributes` interface.
*/
type GetErrorAttribute =
Attribute extends unknown
? IsEqual extends true
? Attribute
: never
: never;
/**
* Type of symbol key for saving hidden normalized tree.
*/
type HiddenKey = typeof HIDDEN;
/**
* Returns `true` if types are exactly equal, `false` otherwise.
* `IsEqual<{foo: string}, {foo: string}>` = `true`.
* `IsEqual<{readonly foo: string}, {foo: string}>` = `false`.
*/
type IsEqual =
(() => Type extends X ? 1 : 2) extends () => Type extends Y ? 1 : 2 ? true : false;
/**
* Returns `true` if type includes `undefined`, `false` otherwise.
* `IsIncludeUndefined` = `false`.
* `IsIncludeUndefined` = `true`.
*/
type IsIncludeUndefined = true extends (Type extends undefined ? true : never) ? true : false;
/**
* Type of symbol key for tree of empty locator (i.e. `Locator`).
*/
type IsEmptyLocatorKey = typeof IS_EMPTY_LOCATOR;
/**
* Returns error string if properties object is not valid for `CreateComponentLocator`
* and other functions, `undefined` otherwise.
*/
type PropertiesError =
Properties extends Partial>
? 'Properties are unmarked by any locator; use & Mark'
: Properties extends Partial>>
? Properties extends WithErrorAttribute
? undefined
: 'This component does not behave like an element; use Locator instead of LocatorOfElement for it'
: Properties extends WithErrorAttribute
? 'This component behaves like an element; use LocatorOfElement for it'
: undefined;
/**
* Returns `true` if parameters is empty, `false` otherwise.
* `IsParametersEmpty<{foo: string}>` = `false`.
* `IsParametersEmpty<{}>` = `true`.
*/
type IsParametersEmpty = Keys extends never ? true : false;
/**
* Returns `true` if there are no required parameters, `false` otherwise.
*/
type IsRequiredParametersEmpty = RequiredKeys extends never ? true : false;
/**
* Returns all keys of type.
* `Keys<{foo: string}>` = `"foo"`.
* `Keys<{foo: string} | {bar?: number}>` = `"foo" | "bar"`.
*/
type Keys = Type extends unknown ? keyof Type : never;
/**
* Arguments of locator as function by locator parameters.
*/
type LocatorArguments =
IsIncludeUndefined extends true
? [parameters?: Parameters]
: [parameters: Parameters];
/**
* Locator call result by locator tree and `IsChildLocator` flag.
*/
type LocatorCallResult = Tree extends object
? WithMark &
WithErrorAttribute<
IsChildLocator extends true
? Tree extends WithLocator<'LocatorOfElement'>
? undefined
: CannotMarkElementMessage
: undefined
>
: Attributes;
/**
* Type of runtime locator object by locator tree.
*/
type LocatorFromLocatorTree = BaseNode<
ExtractNodeParameters,
ExtractNodeComponentParameters,
Tree
> & {
readonly [Key in string & keyof Tree]: TreeNode>;
} & WithLocator;
/**
* Type of symbol key to define locator.
*/
type LocatorKey = typeof LOCATOR;
/**
* Creates locator tree by locator description, locator parameters,
* locator component parameters and locator kind.
*/
type LocatorTree<
Description,
Parameters,
ComponentParameters,
Kind = 'Locator',
MergedDescriptions = UnionToIntersection,
> = BaseNode & {
readonly [Key in string & keyof MergedDescriptions]-?: TreeNode<
Key,
LocatorTreeNode>
>;
} & WithLocator &
AddIsEmptyLocatorKeyIfNeeded;
/**
* Get locator tree from locator object.
*/
type LocatorTreeFromLocator = SomeLocator extends WithLocator
? BaseNode, ExtractNodeComponentParameters> & {
readonly [Key in string & keyof SomeLocator]: TreeNode>;
} & WithLocator &
AddIsEmptyLocatorKeyIfNeeded
: unknown;
/**
* Get locator tree from properties with mark.
*/
type LocatorTreeFromMark> = Exclude<
Properties[MarkKey],
undefined
>;
/**
* Creates locator tree node by locator description node.
*/
type LocatorTreeNode = [DescriptionNode] extends [WithLocator]
? BaseNode<
ExtractNodeParameters,
ExtractNodeComponentParameters,
LocatorTreeFromLocator,
true
> &
WithHidden>>>
: [DescriptionNode] extends [WithNode]
? DescriptionNode[NodeKey]
: BaseNode, {}>;
/**
* Additional option of root locator for mapping attributes.
*/
type MapAttributes = {
readonly mapAttributesChain: (this: void, attributes: readonly Attributes[]) => MappingResult;
};
/**
* Type of symbol key for saving locator tree in mark (in properties object).
*/
type MarkKey = typeof MARK;
/**
* Type of symbol key for saving locator tree of node locator.
*/
type NodeKey = typeof NODE;
/**
* Normalize base node of locator tree by base node and mapping result.
*/
type NormalizeBaseNode = BaseNode extends WithParameters
? NormalizedLocatorFunction & WithParameters
: (this: void) => MappingResult;
/**
* The function part of the or normalized base node.
*/
type NormalizedLocatorFunction = ((
this: void,
parameters: UnwrappedTree[ParametersKey],
) => NormalizeTreeWithParameters) &
((this: void) => MappingResult);
/**
* Normalize list of subnodes of locator tree.
*/
type NormalizeListOfSubnodes = {
readonly [Key in string & keyof Subnodes]: TreeNode<
Key,
NormalizeSubnode, MappingResult>
>;
};
/**
* Normalize subnode of locator tree.
*/
type NormalizeSubnode = Subnode extends WithHidden
? IsEqual extends true
? Subnode[HiddenKey]
: NormalizeTree
: NormalizeTree;
/**
* Normalize locator tree to hidden internal brief presentation (so-called normalized tree).
*/
type NormalizeTree = NormalizeBaseNode<
UnwrapTree,
MappingResult
> &
NormalizeListOfSubnodes, MappingResult>;
/**
* Normalize unwrapped locator tree, which will be used as the return value
* when calling normalized tree nodes with parameters.
*/
type NormalizeTreeWithParameters = ((
this: void,
) => MappingResult) &
WithParameters &
NormalizeListOfSubnodes;
/**
* Message for type error in `Locator<...>` and `Node<...>` first argument.
*/
type NotLocatorDescription = 'Not a locator tree description';
/**
* Type of symbol key for saving locator parameters.
*/
type ParametersKey = typeof PARAMETERS;
/**
* Keys of prototypes of `Object` and `Function`.
*/
type PrototypeKeys = string & (keyof typeof Object.prototype | keyof typeof Function.prototype);
/**
* Returns required keys of type.
* `RequiredKeys<{foo: string}>` = `"foo"`.
* `RequiredKeys<{foo: string, bar?: number}>` = `"foo"`.
*/
type RequiredKeys> =
TypeKeys extends Keys
? Type extends Required>
? TypeKeys
: never
: never;
/**
* Options of root locator with defaults values.
*/
type RootOptionsWithDefaults = Readonly<{
isProduction: boolean;
parameterAttributePrefix: string;
pathAttribute: string;
pathSeparator: string;
}>;
/**
* Type of symbol key for saving locator tree for prototype keys.
*/
type TreeKey = typeof TREE;
/**
* Wrap tree node with tree key for prototype keys.
*/
type TreeNode = Key extends PrototypeKeys ? Tree & WithTree : Tree;
/**
* Converts union of types to intersection of this types.
*/
type UnionToIntersection = (Union extends any ? (arg: Union) => void : never) extends (
arg: infer Intersection,
) => void
? Intersection
: never;
/**
* Unwrap tree node from tree key, if any.
*/
type UnwrapTree = Tree extends WithTree ? Tree[TreeKey] : Tree;
/**
* Any node of locator tree with some component parameters.
*/
type WithComponentParameters = Readonly<
Record
>;
/**
* Object (usually properties object) with error message in error attribute key.
*/
type WithErrorAttribute = Partial<
Readonly>
>;
/**
* Object with hidden normalized tree of locators.
*/
type WithHidden = Readonly>;
/**
* Object with symbol key for tree of empty locator (i.e. `Locator`).
*/
type WithIsEmptyLocator = Readonly>;
/**
* Object with locator key with locator kind (it's locator itself).
*/
type WithLocator = Readonly>;
/**
* Object with mark with locator tree (unnormalized tree of locators, produced by `Locator<...>`).
*/
type WithMark = Readonly>;
/**
* Object with node locator (produced by `Node<...>`).
*/
type WithNode = Readonly>;
/**
* Any node of locator tree with some parameters.
*/
type WithParameters = Readonly>;
/**
* Object with tree of locators under some prototype key.
*/
type WithTree = Readonly>;