Skip to content

Commit

Permalink
Merge branch 'paul-butcher-master'
Browse files Browse the repository at this point in the history
* paul-butcher-master:
  Add test.
  Fix #89, images not loaded when revealed by resize
  • Loading branch information
oncletom committed Oct 24, 2014
2 parents 7640402 + 851aace commit 98550f5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 61 deletions.
5 changes: 5 additions & 0 deletions Imager.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@
addEvent(window, 'scroll', function(){
self.scrolled = true;
});

addEvent(window, 'resize', function(){
self.viewportHeight = document.documentElement.clientHeight;
self.scrolled = true;
});
};

Imager.getPageOffsetGenerator = function getPageVerticalOffset(testCase){
Expand Down
131 changes: 70 additions & 61 deletions test/unit/options-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,96 @@

/* globals describe, it, beforeEach, afterEach, sinon, Imager */

describe('Imager.js Events', function(){
var fixtures, sandbox;
describe('Imager.js Events', function () {
var fixtures, sandbox;

beforeEach(function(){
fixtures = undefined;
sandbox = sinon.sandbox.create();
});
beforeEach(function () {
fixtures = undefined;
sandbox = sinon.sandbox.create();
});

afterEach(function(){
sandbox.restore();
cleanFixtures(fixtures);
});
afterEach(function () {
sandbox.restore();
cleanFixtures(fixtures);
});

describe('Constructor Options', function(){
it('should register the relevant events by default', function(){
var imgr = new Imager();
describe('Constructor Options', function () {
it('should register the relevant events by default', function () {
var imgr = new Imager();

expect(imgr.onResize).to.equal(true);
expect(imgr.lazyload).to.equal(false);
});
expect(imgr.onResize).to.equal(true);
expect(imgr.lazyload).to.equal(false);
});

it('should handle onResize only', function(done){
var imgr = new Imager({ onResize: true, lazyload: false });
var resizeSpy = sandbox.spy(imgr, 'registerResizeEvent');
var scrollSpy = sandbox.spy(imgr, 'registerScrollEvent');
it('should handle onResize only', function (done) {
var imgr = new Imager({ onResize: true, lazyload: false });
var resizeSpy = sandbox.spy(imgr, 'registerResizeEvent');
var scrollSpy = sandbox.spy(imgr, 'registerScrollEvent');

expect(resizeSpy.called).to.equal(false);
expect(scrollSpy.called).to.equal(false);
expect(resizeSpy.called).to.equal(false);
expect(scrollSpy.called).to.equal(false);

runAfterAnimationFrame(function(){
expect(resizeSpy.callCount).to.equal(1);
expect(scrollSpy.callCount).to.equal(0);
runAfterAnimationFrame(function () {
expect(resizeSpy.callCount).to.equal(1);
expect(scrollSpy.callCount).to.equal(0);

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

it('should handle onScroll only', function(done){
var imgr = new Imager({ onResize: false, lazyload: true });
var resizeSpy = sandbox.spy(imgr, 'registerResizeEvent');
var scrollSpy = sandbox.spy(imgr, 'registerScrollEvent');
it('should handle onScroll only', function (done) {
var imgr = new Imager({ onResize: false, lazyload: true });
var resizeSpy = sandbox.spy(imgr, 'registerResizeEvent');
var scrollSpy = sandbox.spy(imgr, 'registerScrollEvent');

expect(resizeSpy.called).to.equal(false);
expect(scrollSpy.called).to.equal(false);
expect(resizeSpy.called).to.equal(false);
expect(scrollSpy.called).to.equal(false);

runAfterAnimationFrame(function(){
expect(resizeSpy.callCount).to.equal(0);
expect(scrollSpy.callCount).to.equal(1);
runAfterAnimationFrame(function () {
expect(resizeSpy.callCount).to.equal(0);
expect(scrollSpy.callCount).to.equal(1);

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

describe('onImagesReplaced', function(){
it('should run during the init process', function(done){
var replaceImagesSpy = sandbox.spy();
var imgr = new Imager({ onImagesReplaced: replaceImagesSpy });
describe('onImagesReplaced', function () {
it('should run during the init process', function (done) {
var replaceImagesSpy = sandbox.spy();
var imgr = new Imager({ onImagesReplaced: replaceImagesSpy });

expect(replaceImagesSpy.called).to.equal(false);
expect(replaceImagesSpy.called).to.equal(false);

runAfterAnimationFrame(function(){
expect(replaceImagesSpy.callCount).to.equal(1);
runAfterAnimationFrame(function () {
expect(replaceImagesSpy.callCount).to.equal(1);

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

it('should receive the targeted list of images as sole argument', function(done){
loadFixtures('regular');
var replaceImagesSpy = sandbox.spy();
var imgr = new Imager({ selector: '#main .delayed-image-load', onImagesReplaced: replaceImagesSpy });
it('should receive the targeted list of images as sole argument', function (done) {
loadFixtures('regular');
var replaceImagesSpy = sandbox.spy();
var imgr = new Imager({ selector: '#main .delayed-image-load', onImagesReplaced: replaceImagesSpy });

runAfterAnimationFrame(function(){
var args = replaceImagesSpy.getCall(0).args;
runAfterAnimationFrame(function () {
var args = replaceImagesSpy.getCall(0).args;

expect(args).to.have.length(1);
expect(args[0]).to.have.length(3);

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

expect(args).to.have.length(1);
expect(args[0]).to.have.length(3);
describe('onresize', function () {
it('should update the viewportHeight internal on window resize if lazyloading is enabled', function(){
var imgr = new Imager({ lazyload: true });

done();
// we could do better but trigger window.onresize is not the most funny thing
expect(imgr.registerScrollEvent.toString()).to.match(/viewportHeight = document.documentElement.clientHeight/);
});
});
});
});
});
});

0 comments on commit 98550f5

Please sign in to comment.