How to use the fake-xml-http-request function in fake-xml-http-request

To help you get started, we’ve selected a few fake-xml-http-request examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TradeMe / tractor / plugins / mock-requests / src / scripts / shim-xhr.js View on Github external
originalXHR.prototype.open = function (method, url) {
        let mock = tractor.getMatchingMock(method, url);

        if (!mock) {
            let message = `Unexpected "${method}" request to "${url}".`;
            /* eslint-disable no-console */
            console.error(message);
            /* eslint-enable no-console */
        }

        if (!mock || mock && mock.passThrough) {
            return originalOpen.apply(this, arguments);
        }

        let real = this;
        let fake = new FakeXMLHttpRequest();
        Object.setPrototypeOf(real, FakeXMLHttpRequest.prototype);

        real.send = function () {
            copyEvents(real, fake);
            FakeXMLHttpRequest.prototype.send.apply(this, arguments);
            fake.send.apply(fake, arguments);
            FakeXMLHttpRequest.prototype.respond.call(this, mock.status, mock.headers, mock.body);
            fake.respond.apply(fake, arguments);
        };

        copyEvents(real, fake);
        FakeXMLHttpRequest.prototype.open.apply(this, arguments);
        fake.open.apply(fake, arguments);
    };

fake-xml-http-request

test infrastructure for a fake XMLHttpRequest object

MIT
Latest version published 3 years ago

Package Health Score

59 / 100
Full package analysis

Similar packages