<commit_before>"use strict";

var referee = require("referee");
var sinon = require("../../lib/sinon");
var TextDecoder = global.TextDecoder || require("text-encoding").TextDecoder;
var assert = referee.assert;
var refute = referee.refute;

var globalXMLHttpRequest = global.XMLHttpRequest;
var globalActiveXObject = global.ActiveXObject;

var supportsProgressEvents = typeof ProgressEvent !== "undefined";
var supportsFormData = typeof FormData !== "undefined";
var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";
var supportsBlob = (function () {
    try {
        return !!new Blob();
    } catch (e) {
        return false;
    }
})();

var fakeXhrSetUp = function () {
    this.fakeXhr = sinon.useFakeXMLHttpRequest();
};

var fakeXhrTearDown = function () {
    if (typeof this.fakeXhr.restore === "function") {
        this.fakeXhr.restore();
    }
};

var runWithWorkingXHROveride = function (workingXHR, test) {
    try {
        var original = sinon.xhr.workingXHR;
        sinon.xhr.workingXHR = workingXHR;
        test();
    } finally {
        sinon.xhr.workingXHR = original;
    }
};

var assertArrayBufferMatches = function (actual, expected, encoding) {
    assert(actual instanceof ArrayBuffer, "${0} expected to be an ArrayBuffer");
    var actualString = new TextDecoder(encoding || "utf-8").decode(actual);
    assert.same(actualString, expected, "ArrayBuffer [${0}] expected to match ArrayBuffer [${1}]");
};

var assertBlobMatches = function (actual, expected, done) {
    var actualReader = new FileReader();
    actualReader.onloadend = function () {
        assert.same(actualReader.result, expected);
        done();
    };
    actualReader.readAsText(actual);
};

var assertProgressEvent = function (event, progress) {
    assert.equals(event.loaded, progress);
    assert.equals(event.total, progress);
    assert.equals(event.lengthComputable, !!progress);
};

var assertEventOrdering = function (event, progress, callback) {
    it("should follow request sequence for " + event, function (done) {
        var expectedOrder = [
            "upload:progress",
            "upload:" + event,
            "upload:loadend",
            "xhr:progress",
            "xhr:on" + event,
            "xhr:" + event
        ];
        var eventOrder = [];

        function observe(name) {
            return function (e) {
                assertProgressEvent(e, progress);
                eventOrder.push(name);
            };
        }

        this.xhr.open("GET", "/");
        this.xhr.send();

        this.xhr.upload.addEventListener("progress", observe("upload:progress"));
        this.xhr.upload.addEventListener(event, observe("upload:" + event));
        this.xhr.upload.addEventListener("loadend", observe("upload:loadend"));
        this.xhr.addEventListener("progress", observe("xhr:progress"));
        this.xhr.addEventListener(event, observe("xhr:" + event));
        this.xhr["on" + event] = observe("xhr:on" + event);
        this.xhr.addEventListener("loadend", function (e) {
            assertProgressEvent(e, progress);
            assert.equals(eventOrder, expectedOrder);

            done();
        });

        callback(this.xhr);
    });
};

if (typeof window !== "undefined") {
    describe("sinon.FakeXMLHttpRequest", function () {

        afterEach(function () {
            delete sinon.FakeXMLHttpRequest.onCreate;
        });

        it("is constructor", function () {
            assert.isFunction(sinon.FakeXMLHttpRequest);
            assert.same(sinon.FakeXMLHttpRequest.prototype.constructor, sinon.FakeXMLHttpRequest);<commit_msg>added tests to ensure only expected events are fired (#1043)<commit_after>"use strict";

var referee = require("referee");
var sinon = require("../../lib/sinon");
var TextDecoder = global.TextDecoder || require("text-encoding").TextDecoder;
var assert = referee.assert;
var refute = referee.refute;

var globalXMLHttpRequest = global.XMLHttpRequest;
var globalActiveXObject = global.ActiveXObject;

var supportsProgressEvents = typeof ProgressEvent !== "undefined";
var supportsFormData = typeof FormData !== "undefined";
var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";
var supportsBlob = (function () {
    try {
        return !!new Blob();
    } catch (e) {
        return false;
    }
})();

var fakeXhrSetUp = function () {
    this.fakeXhr = sinon.useFakeXMLHttpRequest();
};

var fakeXhrTearDown = function () {
    if (typeof this.fakeXhr.restore === "function") {
        this.fakeXhr.restore();
    }
};

var runWithWorkingXHROveride = function (workingXHR, test) {
    try {
        var original = sinon.xhr.workingXHR;
        sinon.xhr.workingXHR = workingXHR;
        test();
    } finally {
        sinon.xhr.workingXHR = original;
    }
};

var assertArrayBufferMatches = function (actual, expected, encoding) {
    assert(actual instanceof ArrayBuffer, "${0} expected to be an ArrayBuffer");
    var actualString = new TextDecoder(encoding || "utf-8").decode(actual);
    assert.same(actualString, expected, "ArrayBuffer [${0}] expected to match ArrayBuffer [${1}]");
};

var assertBlobMatches = function (actual, expected, done) {
    var actualReader = new FileReader();
    actualReader.onloadend = function () {
        assert.same(actualReader.result, expected);
        done();
    };
    actualReader.readAsText(actual);
};

var assertProgressEvent = function (event, progress) {
    assert.equals(event.loaded, progress);
    assert.equals(event.total, progress);
    assert.equals(event.lengthComputable, !!progress);
};

var assertEventOrdering = function (event, progress, callback) {
    it("should follow request sequence for " + event, function (done) {
        var expectedOrder = [
            "upload:progress",
            "upload:" + event,
            "upload:loadend",
            "xhr:progress",
            "xhr:on" + event,
            "xhr:" + event
        ];
        var eventOrder = [];

        function observe(name) {
            return function (e) {
                assertProgressEvent(e, progress);
                eventOrder.push(name);
            };
        }

        this.xhr.open("GET", "/");
        this.xhr.send();

        this.xhr.upload.addEventListener("progress", observe("upload:progress"));
        this.xhr.upload.addEventListener("loadend", observe("upload:loadend"));
        this.xhr.addEventListener("progress", observe("xhr:progress"));
        this.xhr.addEventListener("loadend", function (e) {
            assertProgressEvent(e, progress);

            // finish next tick to allow any events that might fire
            // after loadend to trigger
            setTimeout(function () {
                assert.equals(eventOrder, expectedOrder);

                done();
            }, 1);
        });

        // listen for abort, error, and load events to make sure only
        // the expected events fire
        ["abort", "error", "load"].forEach(
            function (name) {
                this.xhr.upload.addEventListener(name, observe("upload:" + name));
                this.xhr.addEventListener(name, observe("xhr:" + name));
                this.xhr["on" + name] = observe("xhr:on" + name);
            },
            this
        );

        callback(this.xhr);
    });
};

if (typeof window !== "undefined") {
    describe("sinon.FakeXMLHttpRequest", function () {

        afterEach(function () {
            delete sinon.FakeXMLHttpRequest.onCreate;
        });

        it("is constructor", function () {
            assert.isFunction(sinon.FakeXMLHttpRequest);
            assert.same(sinon.FakeXMLHttpRequest.prototype.constructor, sinon.FakeXMLHttpRequest);
{'fraction_non_alphanumeric': 0.13503649635036497, 'fraction_numerical': 0.001694473409801877, 'mean_word_length': 2.7723697148475908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6801834', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2098, 'n_words': 467}