<commit_before>/*jshint multistr: true */
require([
    'tests/options',
    'tests/utils',
    'chai',
    'jquery',
    'underscore',
    'vellum/itemset',
    'vellum/form',
    'text!static/itemset/test1.xml',
    'text!static/itemset/inner-filters.xml'
], function (
    options,
    util,
    chai,
    $,
    _,
    itemset,
    form,
    TEST_XML_1,
    INNER_FILTERS_XML
) {

    // see note about controlling time in formdesigner.lock.js
    var assert = chai.assert,
        call = util.call,
        clickQuestion = util.clickQuestion,
        plugins = _.union(util.options.options.plugins || [], ["itemset"]);

    describe("The Dynamic Itemset plugin", function () {
        function beforeFn(done) {
            util.init({
                plugins: plugins,
                javaRosa: {langs: ['en']},
                core: {onReady: done}
            });
        }

        describe("parsing and serializing", function () {
            before(beforeFn);
            it("preserves XML with itemsets in <select>s", function () {
                assert(TEST_XML_1.indexOf('select1') !== -1);
                util.loadXML(TEST_XML_1);
                util.assertXmlEqual(call('createXML'), TEST_XML_1);
            });

            it("preserves XML with itemsets in <select1>s", function () {
                var newXml = TEST_XML_1.replace(/select1/g, 'select');
                util.loadXML(newXml);
                util.assertXmlEqual(call('createXML'), newXml);
            });
        });

        describe("UI", function () {
            before(beforeFn);
            it("adds a new instance node to the form when necessary", function () {
                util.loadXML(TEST_XML_1);
                clickQuestion("question1/itemset");
                $("[name='data_source']").val("somefixture");
                $("[name='value_ref'], [name='label_ref'], [name='filter_condition']")
                    .val("dummy").change();

                var xml = call('createXML');
                assert(xml.indexOf(
                        '<instance src="jr://fixture/some-fixture" id="somefixture" />'
                    ) !== -1 ||
                    xml.indexOf(
                        '<instance id="somefixture" src="jr://fixture/some-fixture" />'
                    ) !== -1);
            });

            it("preserves inner filters if you never change the data source", function () {
                util.loadXML(INNER_FILTERS_XML);
                clickQuestion("question2/itemset");
                $("[name='label_ref']").val("dummy").change();

                util.assertXmlEqual(
                    INNER_FILTERS_XML.replace('case_name', 'dummy'),
                    call('createXML')
                );
            });
            
            it("hides the copy button for itemsets", function () {
                util.loadXML(TEST_XML_1);
                clickQuestion("question1/itemset");
                var $but = $("button:contains(Copy)");
                assert($but.length === 0);
            });

            it("allows copying a select with an itemset", function () {
                util.loadXML(TEST_XML_1);
                clickQuestion("question1");
                var $but = $("button:contains(Copy)");
                $but.click();

                assert.equal(4, (call('createXML').match(/itemset/g) || []).length);
            });
        });

    });
});<commit_msg>Test model instead of UI; UI is too fragile<commit_after>/*jshint multistr: true */
require([
    'tests/options',
    'tests/utils',
    'chai',
    'jquery',
    'underscore',
    'vellum/itemset',
    'vellum/form',
    'text!static/itemset/test1.xml',
    'text!static/itemset/inner-filters.xml'
], function (
    options,
    util,
    chai,
    $,
    _,
    itemset,
    form,
    TEST_XML_1,
    INNER_FILTERS_XML
) {

    // see note about controlling time in formdesigner.lock.js
    var assert = chai.assert,
        call = util.call,
        clickQuestion = util.clickQuestion,
        plugins = _.union(util.options.options.plugins || [], ["itemset"]);

    describe("The Dynamic Itemset plugin", function () {
        function beforeFn(done) {
            util.init({
                plugins: plugins,
                javaRosa: {langs: ['en']},
                core: {onReady: done}
            });
        }
        before(beforeFn);

        it("adds a new instance node to the form when necessary", function () {
            util.loadXML(TEST_XML_1);
            var itemset = util.getMug("question1/itemset");
            itemset.p.itemsetData = {
                instance: {id: "somefixture", src: "jr://somefixture"},
                nodeset: "instance('somefixture')/some/items",
                labelRef: "label",
                valueRef: "value",
            };

            var xml = call('createXML'),
                $xml = $(xml);
            assert($xml.find("instance[id=somefixture]").length,
                   "somefixture instance not found:\n" + xml);
        });


        describe("parsing and serializing", function () {
            before(beforeFn);
            it("preserves XML with itemsets in <select>s", function () {
                assert(TEST_XML_1.indexOf('select1') !== -1);
                util.loadXML(TEST_XML_1);
                util.assertXmlEqual(call('createXML'), TEST_XML_1);
            });

            it("preserves XML with itemsets in <select1>s", function () {
                var newXml = TEST_XML_1.replace(/select1/g, 'select');
                util.loadXML(newXml);
                util.assertXmlEqual(call('createXML'), newXml);
            });
        });

        describe("UI", function () {
            before(beforeFn);
            it("preserves inner filters if you never change the data source", function () {
                util.loadXML(INNER_FILTERS_XML);
                clickQuestion("question2/itemset");
                $("[name='label_ref']").val("dummy").change();

                util.assertXmlEqual(
                    INNER_FILTERS_XML.replace('case_name', 'dummy'),
                    call('createXML')
                );
            });
            
            it("hides the copy button for itemsets", function () {
                util.loadXML(TEST_XML_1);
                clickQuestion("question1/itemset");
                var $but = $("button:contains(Copy)");
                assert($but.length === 0);
            });

            it("allows copying a select with an itemset", function () {
                util.loadXML(TEST_XML_1);
                clickQuestion("question1");
                var $but = $("button:contains(Copy)");
                $but.click();

                assert.equal(4, (call('createXML').match(/itemset/g) || []).length);
            });
        });
    });
});
{'fraction_non_alphanumeric': 0.14375917767988253, 'fraction_numerical': 0.005873715124816446, 'mean_word_length': 1.656396255850234, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '4286520', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1846, 'n_words': 352}