<commit_before>/* Tests for BaseFileFormViewModel.
 * Copyright (c) 2014 University of Oxford
 */
/* global window:false */
define([
    "shared/app/BaseFileFormViewModel",
    "squire"
], function (BaseFileFormViewModel, Squire) {
    "use strict";

    describe("BaseFileFormViewModel", function () {

        describe("has a 'file' field, which", function () {
            it("is an observable", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.file).toBeObservable();
            });

            it("starts empty", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.file()).toBeUndefined();
            });

            it("is a required field", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.file).toHaveValidationRule({ name: "required", params: true });
            });
        });

        describe("has a 'useFormData' field, which", function () {
            it("gets a browser appropriate value", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.useFormData).toBe(window.FormData !== undefined);
            });
        });

        describe("extends BaseFormViewModel", function () {
            it("with appropriate arguments", function (done) {
                // Squire is going to load js files via ajax, so get rid of the jasmine mock ajax stuff first
                jasmine.Ajax.uninstall();
                var injector = new Squire();
                var formSpy = jasmine.createSpy("formSpy");
                injector.mock("shared/app/BaseFormViewModel", formSpy);

                injector.require(["shared/app/BaseFileFormViewModel"],
                    function (BaseFileFormViewModel) {
                        new BaseFileFormViewModel("baseUrl", "targetUrl", { messages: true }, true); // jshint ignore:line

                        expect(formSpy).toHaveBeenCalledWith(
                            false, true, "baseUrl", "targetUrl", { messages: true }, true);

                        jasmine.Ajax.install();
                        done();
                    }
                );
            });

            describe("and overrides buildAjaxArgs", function () {
                it("to prevent jquery from messing up the AJAX request", function () {
                    var vm = new BaseFileFormViewModel("a", "b");
                    var args = vm.buildAjaxArgs();
                    expect(args.method).toBe("POST");
                    expect(args.url).toBe("ab");
                    expect(args.processData).toBe(false);
                    expect(args.contentType).toBe(false);
                });

                it("to use FormData if available", function () {
                    var vm = new BaseFileFormViewModel("a", "b");
                    vm.buildSubmissionData = function () { return { a: "b" }; };
                    vm.useFormData = true;
                    var args = vm.buildAjaxArgs();
                    expect(Object.getPrototypeOf(args.data).constructor).toBe(window.FormData);
                    expect(args.iframe).toBeUndefined();
                });

                it("to use iframe-transport if FormData not available", function () {
                    var vm = new BaseFileFormViewModel("a", "b");
                    vm.buildSubmissionData = function () { return { a: "b" }; };
                    vm.useFormData = false;
                    var args = vm.buildAjaxArgs();
                    expect(args.iframe).toBe(true);
                    expect(args.files).toBeDefined();
                    expect(args.data).toEqual({ a: "b" });
                });
            });

            describe("and overrides successHandler", function () {
                it("to add a postSuccessAction which is only called after 'true' successes", function () {
                    var vm = new BaseFileFormViewModel();
                    vm.postSuccessAction = jasmine.createSpy();
                    vm.successHandler("<div>" + JSON.stringify({ status: "FAIL", messages: ["a", "b"]}) + "</div>");
                    expect(vm.postSuccessAction).not.toHaveBeenCalled();
                    vm.successHandler("<div>" + JSON.stringify({ status: "SUCCESS", messages: ["a", "b"]}) + "</div>");
                    expect(vm.postSuccessAction).toHaveBeenCalled();<commit_msg>Workaround for test that can't function in phantom<commit_after>/* Tests for BaseFileFormViewModel.
 * Copyright (c) 2014 University of Oxford
 */
/* global window:false */
define([
    "shared/app/BaseFileFormViewModel",
    "squire"
], function (BaseFileFormViewModel, Squire) {
    "use strict";

    describe("BaseFileFormViewModel", function () {

        describe("has a 'file' field, which", function () {
            it("is an observable", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.file).toBeObservable();
            });

            it("starts empty", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.file()).toBeUndefined();
            });

            it("is a required field", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.file).toHaveValidationRule({ name: "required", params: true });
            });
        });

        describe("has a 'useFormData' field, which", function () {
            it("gets a browser appropriate value", function () {
                var vm = new BaseFileFormViewModel();
                expect(vm.useFormData).toBe(window.FormData !== undefined);
            });
        });

        describe("extends BaseFormViewModel", function () {
            it("with appropriate arguments", function (done) {
                // Squire is going to load js files via ajax, so get rid of the jasmine mock ajax stuff first
                jasmine.Ajax.uninstall();
                var injector = new Squire();
                var formSpy = jasmine.createSpy("formSpy");
                injector.mock("shared/app/BaseFormViewModel", formSpy);

                injector.require(["shared/app/BaseFileFormViewModel"],
                    function (BaseFileFormViewModel) {
                        new BaseFileFormViewModel("baseUrl", "targetUrl", { messages: true }, true); // jshint ignore:line

                        expect(formSpy).toHaveBeenCalledWith(
                            false, true, "baseUrl", "targetUrl", { messages: true }, true);

                        jasmine.Ajax.install();
                        done();
                    }
                );
            });

            describe("and overrides buildAjaxArgs", function () {
                it("to prevent jquery from messing up the AJAX request", function () {
                    var vm = new BaseFileFormViewModel("a", "b");
                    var args = vm.buildAjaxArgs();
                    expect(args.method).toBe("POST");
                    expect(args.url).toBe("ab");
                    expect(args.processData).toBe(false);
                    expect(args.contentType).toBe(false);
                });

                if (window.FormData !== undefined && !window._phantom) {
                    // Don't try run this test in environments that can support it

                    it("to use FormData if available", function () {
                        var vm = new BaseFileFormViewModel("a", "b");
                        vm.buildSubmissionData = function () {
                            return { a: "b" };
                        };
                        vm.useFormData = true;
                        var args = vm.buildAjaxArgs();
                        expect(Object.getPrototypeOf(args.data).constructor).toBe(window.FormData);
                        expect(args.iframe).toBeUndefined();
                    });
                }

                it("to use iframe-transport if FormData not available", function () {
                    var vm = new BaseFileFormViewModel("a", "b");
                    vm.buildSubmissionData = function () { return { a: "b" }; };
                    vm.useFormData = false;
                    var args = vm.buildAjaxArgs();
                    expect(args.iframe).toBe(true);
                    expect(args.files).toBeDefined();
                    expect(args.data).toEqual({ a: "b" });
                });
            });

            describe("and overrides successHandler", function () {
                it("to add a postSuccessAction which is only called after 'true' successes", function () {
                    var vm = new BaseFileFormViewModel();
                    vm.postSuccessAction = jasmine.createSpy();
                    vm.successHandler("<div>" + JSON.stringify({ status: "FAIL", messages: ["a", "b"]}) + "</div>");
                    expect(vm.postSuccessAction).not.toHaveBeenCalled();
                    vm.successHandler("<div>" + JSON.stringify({ status: "SUCCESS", messages: ["a", "b"]}) + "</div>");
                    expect(vm.postSuccessAction).toHaveBeenCalled();
{'fraction_non_alphanumeric': 0.12502737026494418, 'fraction_numerical': 0.0008758484782132691, 'mean_word_length': 1.6212338593974176, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5217201', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2117, 'n_words': 525}