Skip to content

Commit

Permalink
Add tests for cleanup method
Browse files Browse the repository at this point in the history
  • Loading branch information
fiznool committed Jan 12, 2016
1 parent 53e7c4e commit 1749b4f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/fixtures/cleanup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="delayed-image-load" data-src="base/test/fixtures/media/A-320.jpg"></div>
<div class="delayed-image-load" data-src="base/test/fixtures/media/B-320.jpg"></div>
<div class="delayed-image-load" data-src="base/test/fixtures/media/B-640.jpg"></div>
<div class="delayed-image-load" data-src="base/test/fixtures/media/C-320.jpg"></div>
68 changes: 68 additions & 0 deletions test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,74 @@ describe('Imager.js', function () {
});
});

describe('cleanup', function() {
var removeEvtStub, getExpectedEvtName;

beforeEach(function() {
if(document.removeEventListener) {
removeEvtStub = sandbox.stub(window, 'removeEventListener');
getExpectedEvtName = function(name) {
return name;
};

} else {
removeEvtStub = sandbox.stub(window, 'detachEvent');
getExpectedEvtName = function(name) {
return 'on' + name;
};
}
});

it('should cleanup after using onResize option', function(done) {
fixtures = loadFixtures('cleanup');
var imgr = new Imager({
onResize: true
});
imgr.ready(function () {
expect(imgr.resizeListeners.length).to.be(1);
expect(imgr.scrollListeners.length).to.be(0);

imgr.cleanup();

expect(imgr.resizeListeners.length).to.be(0);
expect(imgr.scrollListeners.length).to.be(0);

expect(removeEvtStub.called).to.be(true);
expect(removeEvtStub.callCount).to.be(1);
expect(removeEvtStub.firstCall.args[0]).to.be(getExpectedEvtName('resize'));
done();
});
});

it('should cleanup after using lazyload option', function(done) {
sandbox.stub(window, 'clearInterval');
fixtures = loadFixtures('cleanup');
var imgr = new Imager({
lazyload: true
});
imgr.ready(function () {
expect(imgr.resizeListeners.length).to.be(2);
expect(imgr.scrollListeners.length).to.be(1);
expect(imgr.interval).to.be.defined;

imgr.cleanup();

expect(imgr.resizeListeners.length).to.be(0);
expect(imgr.scrollListeners.length).to.be(0);
expect(imgr.interval).to.be.null;

expect(removeEvtStub.called).to.be(true);
expect(removeEvtStub.callCount).to.be(3);
expect(removeEvtStub.firstCall.args[0]).to.be(getExpectedEvtName('resize'));
expect(removeEvtStub.secondCall.args[0]).to.be(getExpectedEvtName('resize'));
expect(removeEvtStub.thirdCall.args[0]).to.be(getExpectedEvtName('scroll'));
expect(window.clearInterval.called).to.be(true);

done();
});
});
});

describe('isThisElementOnScreen', function(){
var offsetStub, imgr;

Expand Down

0 comments on commit 1749b4f

Please sign in to comment.