0.12.2 • Published 3 years ago

request-registry-mock v0.12.2

Weekly downloads
359
License
-
Repository
-
Last release
3 years ago

Request Registry Mock

Helper to create type-safe mocks for unit tests or demos similar to fetch-mock.

Getting started

npm install --save-dev request-registry-mock

Api

mockEndpoint

The mockEndpoint is the main feature of this package.
If executed on an endpoint it will overwrite the original data fetch logic with the given one.

import {mockEndpoint} from 'request-registry-mock';

const userEndpoint = createGetEndpoint({ url: () => '/user' });
mockEndpoint(getUserName, async () => ({ name: 'Joe' }));
console.log(await userEndpoint()); // Will return the mocked value `{name: 'Joe'}`

createMockEndpoint

The createMockEndpoint allows to create a mock controller for an endpoint

simple usage:

import {createMockEndpoint} from 'request-registry-mock';

const userJoeMock = createMockEndpoint(getUserName, async () => ({
    name: 'Joe',
}));
// Activate mock:
userJoeMock.activate();
// Deactivate mock:
userJoeMock.clear();

advanced usage:

A mock can also be based on the request information.

import {createMockEndpoint} from 'request-registry-mock';

// Wait 400ms before responding with the mock result:
const delay = 400;

const userDynamicMock = createMockEndpoint(
    getUserName,
    async (keys, url, headers) => {
        // Respond with different data depending on the url:
        if (url === '/user/1') {
            return {
                name: 'Alex',
            };
        }
        return {
            name: 'Joe',
        };
    },
    delay
);

mockEndpointOnce

The mockEndpointOnce is similar to mockEndpoint however it will unbind after the first response.

const userEndpoint = createGetEndpoint({ url: () => '/user' });
mockEndpointOnce(getUserName, async () => ({ name: 'Joe' }));
console.log(await userEndpoint()); // Will return the mocked value `{name: 'Joe'}`
userEndpoint.refresh();
console.log(await userEndpoint()); // Will return the value of an unmocked call`

activateMocks

Activate multiple mocks at once

Usage:

import {createMockEndpoint} from 'request-registry-mock';

const userJoeMock = createMockEndpoint(getUserName, async () => ({
    name: 'Joe',
}));
const userAgeMock = createMockEndpoint(getUserAge, async () => ({ age: 99 }));
activateMocks(userJoeMock, userAgeMock);

unmockAllEndpoints

Will clear all previously activated mocks

Usage:

import {unmockAllEndpoints} from 'request-registry-mock';

unmockAllEndpoints();

groupMocks

Allows to group multiple mocks into one

Usage:

const userAgeMock = createMockEndpoint(getAge, async () => ({ age: 99 }));
const userNameMock = createMockEndpoint(getName, async () => ({
    name: 'Alex',
}));
const mockGroup = groupMockEndpoints(userAgeMock, userNameMock);
mockGroup.activate();

It is even possible to group multiple groups into one.

setRequestMockLogging

In some scenarios like jest test runs it can be helpful to disable the logging done by this library.

setRequestMockLogging(false);

License

MIT license

0.12.2

3 years ago

0.12.1

4 years ago

0.12.0

4 years ago

0.11.2

4 years ago

0.11.1

4 years ago

0.11.0

4 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.2

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago