每次都需要茉莉花测试帮助

时间:2018-04-23 03:45:45

标签: javascript jquery unit-testing jasmine jasmine-jquery

我有一个使用jQuery构建的逻辑,它使用两个循环。我不确定如何为它编写茉莉花测试。

这是我的代码/逻辑:

<div id="mj">
  <div class="commonclass">
    Item 1
  </div>
  <div class="commonclass">
    Item 2
  </div>
  <div class="commonclass">
    Item 3
  </div>
</div>

jQuery:

var filterArray = ['Item', 'Block', 'Item 3'];

$.each($('.commonclass'), function(i, v) {
  var matching = false;
  $.each(filterArray, function(j, w) {
    if ($(v).text().trim() == w)
      matching = true;
  });
  if (matching)
    $(v).remove();
});

逻辑:

div元素中的文本不应与filterarray值匹配。如果匹配,则应删除该元素。

这是我的茉莉花测试代码。 (已添加部分)

describe('Delete matching element', function() {
        beforeAll(function() {
            elementsEl = element(SelectorData.selectors.elementSelector);
            elementValueEl = element(SelectorData.selectors.elementTitleSelector);
            bodyDocumentEl = element(SelectorData.selectors.body);
            bodyDocumentValues = SelectorData.values;
        });

        afterEach( function() {

            if (elementValueEl.value == bodyDocumentValues.value) {
                document.body.removeChild(elementsEl);
            }

        });

        it('delete the element', function() {
            expect(elementsEl.isPresent()).toBe(false);

        });
    });

让我知道我在这里做错了什么。 JSfiddle: https://jsfiddle.net/dmahendranme/g1L2Lzm1/

1 个答案:

答案 0 :(得分:0)

试试这个,

$(function(){    
    var filterArray = ['Item', 'Block', 'Item 3'];
    $('.commonclass').each(function(k,v){        
        if($.inArray($.trim($(v).text()), filterArray) > 0){
            $(this).remove()
        }
    });
});