lundi 1 août 2016

JavaScript Jasmine looping through object causes beforeEach to execute more than it

Basic explanation

I have a object named allFeeds that has a url in each part of the object. To which the loadFeed function performs an ajax request on the index of allFeeds specified as its argument and effectively fills a element with class feed full of feeds related to that url in that argument portion of the allFeeds object. I am trying to test whether for each of the objects's url after completing the ajax request that there will be elements displayed in the feed class.

var testLoad = function(key) {
        beforeEach(function(done) {
            loadFeed(key, done);
        });
        it('loads correctly', function(done) {
            expect($('.feed')[0].length).not.toBe(0);
            done();
        });
    };
    for(var i = 0; i < allFeeds.length ; i++) {
            testLoad(i);
    }

Problem

The problem is when I execute this code the beforeEach section of the code executes 16 times where 4 is the size of allFeeds the it portion performs only 4 times as well as the function testLoad. In case anybody was wondering I did install jasmine-jquery, and also correct me if I'm wrong yet I doubt that it is a problem with closures since the same problem persisted when I wrapped testLoad in an Immediately-invoked function expression like the following

var testLoad = function(key) {
        beforeEach(function(done) {
            loadFeed(key, done);
        });
        it('loads correctly', function(done) {
            expect($('.feed')[0].length).not.toBe(0);
            done();
        });
    };
    for(var i = 0; i < allFeeds.length ; i++) {
        (function(j) {
            testLoad(j);
        })(i)
    }

Aucun commentaire:

Enregistrer un commentaire