UNPKG

@assertive-ts/core

Version:

A type-safe fluent assertion library

152 lines (151 loc) 4.71 kB
import { Assertion } from "./Assertion"; import { type TypeFactory } from "./helpers/TypeFactories"; /** * Encapsulates assertion methods applicable to arrays. * * @param T the type of the array */ export declare class ArrayAssertion<T> extends Assertion<T[]> { constructor(actual: T[]); /** * Check if all the array values match the predicate * * @example * ``` * expect([1, 2, 3]).toMatchAll(x => x < 5); * expect([apple, orange, pear]).toMatchAll(isFruit); * ``` * * @param matcher a generic matcher predicate * @returns the assertion instance */ toMatchAll(matcher: (value: T) => boolean): this; /** * Check if any of the array values match the predicate * * @example * ``` * expect([dog, apple, cat]).toMatchAny(isFruit); * ``` * * @param matcher a matcher predicate * @returns the assertion instance */ toMatchAny(matcher: (value: T) => boolean): this; /** * Check if all the values of the array satisfies a given assertion. * * @example * ``` * const checkIsFruit = (x: Fruit) => expect(x).toBeInstanceOf(Fruit); * expect([apple, pear, banana]).toSatisfyAll(checkIsFruit); * ``` * * @param consumer a consumer of the array to assert over each value of its * values * @returns the assertion instance */ toSatisfyAll(consumer: (value: T) => void): this; /** * Check if any value of the array satisfies the give assertion. * * @example * ``` * const checkIsFruit = (x: Fruit) => expect(x).toBeInstanceOf(Fruit); * expect([dog, apple, cat]).toSatisfyAny(checkIsFruit); * ``` * * @param consumer a consumer of the array to assert over each value of its * values * @returns the assertion instance */ toSatisfyAny(consumer: (value: T) => void): this; /** * Check if the array is empty. That is, when its `length` property is zero. * * @example * ``` * expect([]).toBeEmpty(); * ``` * * @returns the assertion instance */ toBeEmpty(): this; /** * Check if the array has some specific number of elements. * * @example * ``` * expect([0, 1, 2]).toHaveSize(3); * ``` * * @param size the expected number of elements in the array * @returns the assertion instance */ toHaveSize(size: number): this; /** * Check if the array contains the same elements as another. This doesn't * check the order of the elements. * * @example * ``` * expect([1, 2, 3]).toHaveSameMembers([3, 2, 1]); * ``` * * @param expected the other array to compare its elements to * @returns the assertion instance */ toHaveSameMembers(expected: T[]): this; /** * Check if the array contains all the passed values. * * @example * ``` * expect([1, 2, 3]).toContainAll(1, 3); * ``` * * @param values the values the array should contain * @returns the assertion instance */ toContainAll(...values: T[]): this; /** * Check if the array contains any of the passed values. * * @example * ``` * expect([1, 2, 3]).toContainAny(1, 50, 36); * ``` * * @param values the value the array should include (at least one) * @returns the assertion instance */ toContainAny(...values: T[]): this; /** * Check if the array contains an specific value at an exact index. * * @param index the index of the array to find the value * @param value the expected value of the index in the array * @returns the assertion instance */ toContainAt(index: number, value: T): this; /** * Extract the value on a specific index of the array and create an assertion * instance of that specific type. This method uses {@link Assertion.asType} * internally to create the new assertion instance. * * @example * ``` * expect(["foo", 2, true]) * .extracting(1, TypeFactories.Number) * .toBePositive(); * ``` * * @typeParam S the type of the factory's value * @typeParam A the type of the assertion factory * @param index the index of the array to extract the value * @param typeFactory a factory to assert the extracted value type and create * an assertion for it * @returns a more specific assertion based on the factory type for the value */ extracting<S extends T, A extends Assertion<S>>(index: number, typeFactory: TypeFactory<S, A>): A; }