Skip to content

Commit

Permalink
use assert.async() in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
desandro committed Oct 28, 2015
1 parent e045d65 commit da75265
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 172 deletions.
8 changes: 2 additions & 6 deletions .jshintrc
Expand Up @@ -5,12 +5,8 @@
"strict": true,
"undef": true,
"unused": true,
"predef": {
"globals": {
"imagesLoaded": false,
"test": false,
"ok": false,
"equal": false,
"start": false,
"stop": false
"QUnit": false
}
}
10 changes: 6 additions & 4 deletions test/append.js
@@ -1,4 +1,6 @@
test( 'append', function() {
QUnit.test( 'append', function( assert ) {
'use strict';

var imgUrls = [
'http://i.imgur.com/bwy74ok.jpg',
'http://i.imgur.com/bAZWoqx.jpg',
Expand All @@ -18,11 +20,11 @@ test( 'append', function() {

var elem = document.querySelector('#append');
elem.appendChild( fragment );
var done = assert.async();

stop();
imagesLoaded( elem, { debug: true } ).on( 'always', function() {
ok( 'appended images loaded' );
start();
assert.ok( 'appended images loaded' );
done();
});

});
42 changes: 12 additions & 30 deletions test/basics.js
@@ -1,46 +1,28 @@
test( 'basics', function() {
QUnit.test( 'basics', function( assert ) {

'use strict';

var elem = document.querySelector('#basics');
var images = elem.querySelectorAll('img');
var isCallbacked, isDone, isAlways, isAllProgressed;
// stop();
var done = assert.async( 3 + images.length );

var imgLoader = new imagesLoaded( elem, function( obj ) {
ok( true, 'callback function triggered' );
equal( imgLoader, obj, 'callback argument and instance match' );
isCallbacked = true;
checkReady();
assert.ok( true, 'callback function triggered' );
assert.equal( imgLoader, obj, 'callback argument and instance match' );
done();
});
imgLoader.on( 'done', function() {
ok( true, 'done event triggered' );
isDone = true;
checkReady();
assert.ok( true, 'done event triggered' );
done();
});
imgLoader.on( 'always', function() {
ok( true, 'always event triggered' );
isAlways = true;
checkReady();
assert.ok( true, 'always event triggered' );
done();
});

var progressCount = 0;
imgLoader.on( 'progress', function( loader, image ) {
ok( image.isLoaded, 'image is loaded');
progressCount++;
if ( progressCount >= images.length ) {
equal( progressCount, images.length, 'progressed right amount of times' );
isAllProgressed = true;
checkReady();
}
assert.ok( image.isLoaded, 'image is loaded');
done();
});

stop();

function checkReady() {
if ( isCallbacked && isDone && isAlways && isAllProgressed ) {
start();
}
}


});
10 changes: 5 additions & 5 deletions test/data-uri.js
@@ -1,10 +1,10 @@
test( 'data-uri', function() {
QUnit.test( 'data-uri', function( assert ) {
'use strict';

stop();
var done = assert.async();
imagesLoaded('#data-uri', { debug: true }).on( 'done', function( obj ) {
ok( true, 'data-uri images loaded' );
equal( obj.images.length, 2, 'instance has 2 images' );
start();
assert.ok( true, 'data-uri images loaded' );
assert.equal( obj.images.length, 2, 'instance has 2 images' );
done();
});
});
45 changes: 15 additions & 30 deletions test/jquery-fail.js
@@ -1,44 +1,29 @@
test( 'jquery fail', function() {
QUnit.test( 'jquery fail', function( assert ) {

'use strict';

var $ = window.jQuery;
var isCallbacked, isFailed, isAlways, isAllProgressed;
var progressCount = 0;
var $images = $('#jquery-fail img');
stop();
var done = assert.async( 3 + $images.length );

$('#jquery-fail').imagesLoaded( function( instance ) {
ok( true, 'callback triggered' );
ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
isCallbacked = true;
checkReady();
assert.ok( true, 'callback triggered' );
assert.ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
done();
})
.fail( function( instance ) {
ok( true, 'done triggered' );
ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
isFailed = true;
checkReady();
assert.ok( true, 'fail triggered' );
assert.ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
done();
})
.always( function( instance ) {
ok( true, 'always triggered' );
ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
isAlways = true;
checkReady();
assert.ok( true, 'always triggered' );
assert.ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
done();
})
.progress( function() {
progressCount++;
if ( progressCount >= $images.length ) {
equal( progressCount, $images.length, 'progressed iterations matches images length' );
isAllProgressed = true;
checkReady();
}
.progress( function(/* instance, image */) {
assert.ok( true, 'progress trigged');
done();
});

function checkReady() {
if ( isCallbacked && isFailed && isAlways && isAllProgressed ) {
start();
}
}

});
});
42 changes: 13 additions & 29 deletions test/jquery-success.js
@@ -1,44 +1,28 @@
test( 'jquery success', function() {
QUnit.test( 'jquery success', function( assert ) {

'use strict';

var $ = window.jQuery;
var isCallbacked, isDone, isAlways, isAllProgressed;
var progressCount = 0;
stop();
var done = assert.async( 6 );

$('#jquery-success').imagesLoaded( function( instance ) {
ok( true, 'callback triggered' );
ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
isCallbacked = true;
checkReady();
assert.ok( true, 'callback triggered' );
assert.ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
done();
})
.done( function( instance ) {
ok( true, 'done triggered' );
ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
isDone = true;
checkReady();
assert.ok( true, 'done triggered' );
assert.ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
done();
})
.always( function( instance ) {
ok( true, 'always triggered' );
ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
isAlways = true;
checkReady();
assert.ok( true, 'always triggered' );
assert.ok( instance instanceof imagesLoaded, 'instance instanceof imagesLoaded' );
done();
})
.progress( function( instance, image ) {
ok( image.isLoaded, 'image is loaded');
progressCount++;
if ( progressCount >= 3 ) {
equal( progressCount, 3, 'progressed 3 times' );
isAllProgressed = true;
checkReady();
}
assert.ok( image.isLoaded, 'progress trigged, image is loaded');
done();
});

function checkReady() {
if ( isCallbacked && isDone && isAlways && isAllProgressed ) {
start();
}
}

});
46 changes: 14 additions & 32 deletions test/local-files.js
@@ -1,49 +1,31 @@
test( 'local files', function() {

QUnit.test( 'local files', function( assert ) {
'use strict';

var elem = document.querySelector('#locals');
var isCallbacked, isFailed, isAlways, isAllProgressed;
// stop();
var done = assert.async( 6 );

var imgLoader = new imagesLoaded( elem, function( obj ) {
ok( true, 'callback function triggered' );
equal( imgLoader, obj, 'callback argument and instance match' );
isCallbacked = true;
checkReady();
assert.ok( true, 'callback function triggered' );
assert.equal( imgLoader, obj, 'callback argument and instance match' );
done();
});
imgLoader.on( 'fail', function() {
ok( true, 'fail event triggered' );
isFailed = true;
checkReady();
assert.ok( true, 'fail event triggered' );
done();
});
imgLoader.on( 'always', function() {
ok( true, 'always event triggered' );
isAlways = true;
checkReady();
assert.ok( true, 'always event triggered' );
done();
});

var progressCount = 0;
imgLoader.on( 'progress', function( loader, image ) {
ok( true, 'image progressed');
assert.ok( true, 'image progressed');
if ( image.img.src.indexOf('img/not-there.jpg') !== -1 ) {
ok( !image.isLoaded, 'thunder cloud is not loaded' );
assert.ok( !image.isLoaded, 'thunder cloud is not loaded' );
} else {
ok( image.isLoaded, 'image is loaded' );
}
progressCount++;
if ( progressCount >= 3 ) {
equal( progressCount, 3, 'progressed 3 times' );
isAllProgressed = true;
checkReady();
assert.ok( image.isLoaded, 'image is loaded' );
}
done();
});

stop();

function checkReady() {
if ( isCallbacked && isFailed && isAlways && isAllProgressed ) {
start();
}
}

});
9 changes: 4 additions & 5 deletions test/no-images.js
@@ -1,12 +1,11 @@
test( 'no images', function() {

QUnit.test( 'no images', function( assert ) {
'use strict';

var elem = document.querySelector('#no-images');
stop();
var done = assert.async();
imagesLoaded( elem, function() {
ok( true, 'triggered with no images' );
start();
assert.ok( true, 'triggered with no images' );
done();
});

});
33 changes: 15 additions & 18 deletions test/non-element.js
@@ -1,27 +1,24 @@
test( 'dismiss non-element nodes', function() {

QUnit.test( 'dismiss non-element nodes', function( assert ) {
'use strict';

var $ = window.jQuery;
stop();
var done = assert.async( 2 );

$(' <img src="http://lorempixel.com/401/301/" /> <img src="http://lorempixel.com/402/302/" /> ')
.imagesLoaded(function() {
ok( true, 'elements from jQuery string ok' );
start();
testFrag();
assert.ok( true, 'elements from jQuery string ok' );
done();
});

function testFrag() {
stop();
var frag = document.createDocumentFragment();
var img = new Image();
img.src = 'http://lorempixel.com/403/303/';
frag.appendChild( img );
var imgLoad = imagesLoaded( frag, function() {
ok( true, 'document fragment ok' );
equal( imgLoad.images.length, 1, '1 image found' );
start();
});
}
// test fragment
var frag = document.createDocumentFragment();
var img = new Image();
img.src = 'http://lorempixel.com/403/303/';
frag.appendChild( img );
var imgLoad = imagesLoaded( frag, function() {
assert.ok( true, 'document fragment ok' );
assert.equal( imgLoad.images.length, 1, '1 image found' );
done();
});

});
15 changes: 8 additions & 7 deletions test/selector-string.js
@@ -1,11 +1,12 @@
test( 'selector string', function() {
stop();
QUnit.test( 'selector string', function( assert ) {
'use strict';
var images = document.querySelectorAll('#basics img');
var done = assert.async();
var imgLoad = imagesLoaded('#basics', { debug: true }).on( 'done', function( obj ) {
ok( true, 'selector string worked' );
ok( obj.images, 'argument has images' );
equal( obj.images.length, images.length, 'images.length matches' );
start();
assert.ok( true, 'selector string worked' );
assert.ok( obj.images, 'argument has images' );
assert.equal( obj.images.length, images.length, 'images.length matches' );
done();
});
ok( imgLoad.options.debug, 'debug option set' );
assert.ok( imgLoad.options.debug, 'debug option set' );
});
13 changes: 7 additions & 6 deletions test/single-element.js
@@ -1,10 +1,11 @@
test( 'single element', function() {
QUnit.test( 'single element', function( assert ) {
'use strict';
var elem = document.querySelector('#mario-with-shell');
stop();
var done = assert.async();
imagesLoaded( elem ).on( 'done', function( obj ) {
ok( true, 'single element worked' );
ok( obj.images, 'argument has images' );
equal( obj.images.length, 1, 'images.length = 1' );
start();
assert.ok( true, 'single element worked' );
assert.ok( obj.images, 'argument has images' );
assert.equal( obj.images.length, 1, 'images.length = 1' );
done();
});
});

0 comments on commit da75265

Please sign in to comment.