javascript - Testing anchor links call with Mocha & Sinon -
i writing test case spy on google tracking calls on click event. there 2 scenarios covered in code. one, traverse hyperlinks on page , call click method inside loop. second, write independent test cases individual elements.
describe("container tags automated test suite", function() { var spy; var assert = sinon.assert; beforeeach(function() { spy = sinon.spy(window.spyconfig.object, window.spyconfig.method); }); aftereach(function() { spy.restore(); }); // trackable links & total links count var links = $("a.wrapper-link"), linkscount = links.length; //first appraoch : make array of links for(var i=0; < linkscount ; i++) { ("track page links", function() { link = $(links[i]); link.click(); assert.called(spy); }); } //second approach : invidual selectors ("track single link", function() { link = $('a.wrapper-link'); link.click(); assert.called(spy); }); }); problem: working fine if use independent jquery selectors each test case. eg. test case "track single link" got pass on every execution.
but, first approach make array on links , bind click event elements forcing test case fail. trying hard last couple of hours didn't luck find root cause of issue. please help.
this common error loops @ end i === linkscount when test running. have wrap call test function , passing i save in scope of function.
//first appraoch : make array of links for(var i=0; < linkscount ; i++) { (function(cnt){ ("track page " + cnt + ". link", function() { link = $(links[cnt]); link.click(); assert.called(spy); }); })(i) }
Comments
Post a Comment