Sha256: de482cf0aac98a64caddb09d73b08fdb2b921f2875e31c7a7067b8db91db4a51

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

/* global jest */

/**
 * Copyright 2004-present Facebook. All Rights Reserved.
 *
 * Implements basic mock for the fetch interface use `whatwg-fetch` polyfill.
 *
 * See https://fetch.spec.whatwg.org/
 */


function ResponseWrapper(body) {
    const response = {};
    response.body = ('object' === typeof body) ? JSON.stringify(body) : body;
    const actualClone = response.clone;
    response.clone = () => {
        const clone = actualClone.call(response);
        const [body1, body2] = body.tee();
        response.body = body1;
        clone.body = body2;
        return clone;
    };
    response.json = () => (response.body ? JSON.parse(response.body) : {});
    return response;
}

const fetch = jest.fn();

fetch.Response = ResponseWrapper;

fetch.mockResponse = (body, init) => {
    fetch.mockImplementation(
        () => Promise.resolve(new ResponseWrapper(body, init)),
    );
};

fetch.mockResponseOnce = (body, init) => {
    fetch.mockImplementationOnce(
        () => Promise.resolve(new ResponseWrapper(body, init)),
    );
};

fetch.mockResponses = (...responses) => {
    responses.forEach(([body, init]) => {
        fetch.mockImplementationOnce(
            () => Promise.resolve(new ResponseWrapper(body, init)),
        );
    });
};

// Default mock is just a empty string.
fetch.mockResponse('');

module.exports = fetch;

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hippo-fw-0.9.9 client/hippo/testing/mocks/fetch.js
hippo-fw-0.9.8 client/hippo/testing/mocks/fetch.js
hippo-fw-0.9.7 client/hippo/testing/mocks/fetch.js
hippo-fw-0.9.6 client/hippo/testing/mocks/fetch.js
hippo-fw-0.9.5 client/hippo/testing/mocks/fetch.js