UNPKG

@hexadrop/query

Version:

Hexagonal architecture utils library

57 lines (54 loc) 2.59 kB
import Either from '@hexadrop/either'; import DomainError from '@hexadrop/error'; import { jest } from 'bun:test'; import QueryBus from './bus.js'; import Query from './query.js'; import '@hexadrop/types/class'; import '@hexadrop/types/primitives'; /** * @class BunMockQueryBus * @implements QueryBus * @description Class representing a mock query bus for testing in Bun. */ declare class BunMockQueryBus implements QueryBus { /** * @property {Mock} askSpy - A mock function for the ask method. */ readonly askSpy: jest.Mock<(_query: Query<unknown>) => Promise<Either<DomainError, never>>>; /** * This is a private static method that extracts data from a query. * It omits the 'queryId' property from the query object and returns the remaining properties. * If no query is provided, it returns an empty object. * * @template QueryType - A type that extends the Query interface. * @param {QueryType} query - The query object from which to extract data. This is optional. * @returns {Omit<QueryType, 'queryId'> | {}} - An object that contains the properties of the query object, excluding 'queryId'. If no query is provided, an empty object is returned. */ private static getDataFromQuery; /** * @method ask * @description A method that takes a Query and returns either a DomainError or the ResponseType type, or a Promise of that either * @param {Query<ResponseType>} query - The Query to be asked * @returns {Either<DomainError, ResponseType> | Promise<Either<DomainError, ResponseType>} - Either a DomainError or the ResponseType type, or a Promise of either * @template ResponseType - The type of the ResponseType */ ask<const ResponseType>(query: Query<ResponseType>): Either<DomainError, ResponseType> | Promise<Either<DomainError, ResponseType>>; /** * @method assertAskedQueries * @description Method to assert that specific queries were asked. * @param {...Query[]} expectedQueries - The expected asked queries. */ assertAskedQueries(...expectedQueries: Query<unknown>[]): void; /** * @method assertLastAskedQuery * @description Method to assert that a specific query was the last one asked. * @param {Query} expectedQuery - The expected last asked query. */ assertLastAskedQuery<Response>(expectedQuery: Query<Response>): void; /** * @method assertNotAskedQuery * @description Method to assert that no query was asked. */ assertNotAskedQuery(): void; } export { BunMockQueryBus as default };