简体   繁体   中英

AngularJS ng-repeat track by $index and Protractor with Duplicates

I have an array of strings, which I need to allow for duplicates.

vm.list = ["item","item","item","item","item"]

this is handled in the html with

<ul class="listItems>
    <li ng-repeat="item in ctrl.list track by $index"></li>
</ul>

This displays fine in the DOM, no issues, but I run into problems when I try to protractor test the ng-repeat, since I can't unit test it.

So my test is something like.

Then("List items should contain {int} items.", function(listLength){
    return element(by.css(".listItems").all(by.repeater('item in ctrl.list track by $index')).then(function(list){
        return expect(list.length).equal(listLength);
    });
});

I run my tests and it fails with Expect 0 to be 5 But if I make them all unique it works fine, how can I fix this?

I believe this should work for you:

Then("List items should contain {int} items.", function(listLength){
    let list = element(by.css(".listItems").all(by.repeater('item in ctrl.list'));
    expect(list.count()).toBe(listLength);
});

Source: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.count

Edit: Sorry, I misinterpreted your question, it appears track by does not work with duplicate values in an array. See: https://docs.angularjs.org/api/ng/directive/ngRepeat . You can substitute your own tracking function.

Here is an example of a similar issue: https://stackoverflow.com/a/28232640/3111958

2nd Edit: After looking into it some more, protractor does not use the track by portion. I updated the code block to reflect that. Check out this issue: https://stackoverflow.com/a/37535098/3111958

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM