jeudi 22 octobre 2015

Qunit code for JS function revision

Im working in a test in QUinit but Im like learning on the road. I have a function and I made a test, I just want to know if Im doing in it right? I dont get errors but I just want to make sure that Im not doing like nothing in test. Thank you everyone and if the test is ok I hope it can help someone on the same situation!

This is my tesy:

    test('tsrSelect', function () {    
    var fixture = $('#qunit-fixture'),
        Element = $('<select id="tsrSelect" readonly="true"></select>'),
        element2 =  $('<option value="1"></option><option value="2"></option>');
    fixture.append(Element).append(element2);
    ok($(Element).filter(":selected"), "element select is Selected");
    ok($(Element).find('option'), "there is an option tag in the element select");
    ok($(element2).after("select2-hidden-accessible"), "element hass class selected");
});

and This is the function

        tsrSelect: function () {
        return $(this).each(function() {
            var $this = $(this), 
            span = $this.next("span.selectSpan").get(0),
            selectedText = $this.children().filter(":selected").text();
            if ($this.attr('readonly') || $this.find('option').length < 2) {
                if (span) $(span).text(selectedText);
                else $this.after($("<span>").text(selectedText).addClass("selectSpan"));
                if ($this.hasClass("select2-hidden-accessible")) $this.select2('destroy');
                $this.hide();
            } else {
                if (!$this.hasClass("select2-hidden-accessible")) {
                    if (span) $(span).remove();
                    $this.select2({
                        minimumResultsForSearch: 15,
                        width: '100%'
                    });
                }
            }
        });
    }

Aucun commentaire:

Enregistrer un commentaire