@muspellheim/shared
    Preparing search index...

    Class ConfigurableResponses<T>

    Handle returning pre-configured responses.

    This is one of the nullability patterns from James Shore's article on testing without mocks.

    Example usage for stubbing fetch function:

    function createFetchStub(responses) {
    const configurableResponses = ConfigurableResponses.create(responses);
    return async function () {
    const response = configurableResponses.next();
    return {
    status: response.status,
    json: async () => response.body,
    };
    };
    }

    Type Parameters

    • T = unknown
    Index

    Constructors

    Methods

    Constructors

    • Create a list of responses (by providing an array), or a single repeating response (by providing any other type). 'Name' is optional and used in error messages.

      Type Parameters

      • T = unknown

      Parameters

      • Optionalresponses: T | T[]

        A single response or an array of responses.

      • Optionalname: string

        An optional name for the responses.

      Returns ConfigurableResponses<T>

    Methods

    • Get the next configured response. Throws an error when configured with a list of responses and no more responses remain.

      Returns T

      The next response.

    • Create a list of responses (by providing an array), or a single repeating response (by providing any other type). 'Name' is optional and used in error messages.

      Type Parameters

      • T

      Parameters

      • Optionalresponses: T | T[]

        A single response or an array of responses.

      • Optionalname: string

        An optional name for the responses.

      Returns ConfigurableResponses<T>

    • Convert all properties in an object into ConfigurableResponse instances. For example, { a: 1 } becomes { a: ConfigurableResponses.create(1) }. 'Name' is optional and used in error messages.

      Type Parameters

      • T extends Record<string, unknown>

      Parameters

      • responseObject: T

        An object with single response or an array of responses.

      • Optionalname: string

        An optional name for the responses.

      Returns any