gpt4 book ai didi

javascript - AfterEach 需要 Jasmine 测试帮助

转载 作者:行者123 更新时间:2023-12-01 05:15:23 24 4
gpt4 key购买 nike

我有一个使用 jQuery 构建的逻辑,它使用两个each 循环。我不知道如何为其编写 Jasmine 测试。

这是我的代码/逻辑:

<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 值匹配。如果匹配,则应删除该元素。

这是我的 Jasmine 测试代码。 (补充部分)

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/

最佳答案

试试这个,

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

关于javascript - AfterEach 需要 Jasmine 测试帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com