Switch to mocha/chai for tests with gulp-mocha-phantomjs

JSDom's getComputedStyle wasn't working.
pull/111/head
Glen Cheney 8 years ago
parent 6adb041ba1
commit cfa68c76de

@ -1,10 +1,5 @@
{% if page.jquery != false %}
<!--[if lt IE 9]>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!--<![endif]-->
{% endif %}
{% if page.shuffle != false %}
@ -19,8 +14,8 @@
<script>var site_url = "{{ site.baseurl }}";</script>
{% if page.pagejs != false %}
<script src="{{ site.baseurl }}/js/page.js"></script>
<script src="{{ site.baseurl }}/js/evenheights.js"></script>
<script src="{{ site.baseurl }}/js/page.js"></script>
{% endif %}
{% if page.extraJS && page.extraJS.length %}

@ -3,7 +3,7 @@ layout: default
title: Bootstrap 3 Grid Demo
description: Demonstrating how Shuffle can be used with a grid system which uses padding for gutters instead of margins.
image: /demos/bootstrap3grid.jpg
externalCSS: [ "http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" ]
externalCSS: [ "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" ]
extraJS: [ "demos/padding-grid.js" ]
---
<style>
@ -32,11 +32,6 @@ extraJS: [ "demos/padding-grid.js" ]
.grid__brick:nth-child(n+3) {
margin-top: 15px;
}
/* Padding on the grid column is keep it from getting smaller than 30px */
.shuffle--fluid .shuffle__sizer {
padding: 0;
}
}
.aside {
@ -54,7 +49,7 @@ extraJS: [ "demos/padding-grid.js" ]
<div class="col-xs-12">
<h2>Bootstrap 3 Grid</h2>
<p>
On this page, I have added the minified bootstrap css file from the <a href="http://www.bootstrapcdn.com/">NetDNA CDN</a> (which is also why some of the site-styles are being overriden).
On this page, I have added the minified bootstrap css file from the <a href="https://www.bootstrapcdn.com/">Bootstrap CDN</a> (which is also why some of the site-styles are being overriden).
<br>
The <a href="http://getbootstrap.com/css/#grid">Bootstrap 3 grid</a> system uses padding for gutters instead of margins. This page demonstrates one way to use Shuffle with these types of grids, as well as showing you don't have to use the full width of the page with Shuffle.
</p>

@ -24,7 +24,7 @@ pagejs: false
}
#grid .img-item {
margin-top: 10px;
margin-top: 16px;
margin-left: 0;
}
@ -37,7 +37,7 @@ pagejs: false
<div id="grid" class="shuffle--container row">
{% for item in site.items %}
<figure class="js-item img-item col-3@sm col-3@xs">
<div class="aspect aspect--4x3">
<div class="aspect aspect--16x9">
<div class="aspect__inner">
<img src="{{ site.baseurl }}/img/{{ item.img }}" alt="{{item.title}}"/>
</div>
@ -61,4 +61,4 @@ pagejs: false
{% include credit-jake.html %}
<script data-main="{{ site.baseurl }}/js/require-main.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.11/require.min.js"></script>
<script data-main="{{ site.baseurl }}/js/require-main.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>

12
dist/shuffle.js vendored

@ -645,10 +645,11 @@ return /******/ (function(modules) { // webpackBootstrap
value: function _dispatch(name) {
var details = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
console.log('dispatch:', name);
details.shuffle = this;
return !this.element.dispatchEvent(new CustomEvent(name, {
bubbles: false,
cancelable: true,
bubbles: true,
cancelable: false,
detail: details
}));
}
@ -701,6 +702,7 @@ return /******/ (function(modules) { // webpackBootstrap
return;
}
console.log('from:', item.point, 'to:', pos, item.element.id);
item.point = pos;
item.scale = _shuffleItem2.default.Scale.VISIBLE;
@ -862,6 +864,7 @@ return /******/ (function(modules) { // webpackBootstrap
return;
}
console.log('shrink:', item.element.id);
item.scale = _shuffleItem2.default.Scale.FILTERED;
_this4._queue.push({
@ -943,6 +946,7 @@ return /******/ (function(modules) { // webpackBootstrap
var reference = {
item: item,
handler: function handler(evt) {
console.log('transition end handler', evt.target === evt.currentTarget, evt.currentTarget.id);
var element = evt.target;
// Make sure this event handler has not bubbled up from a child.
@ -1009,6 +1013,7 @@ return /******/ (function(modules) { // webpackBootstrap
// Set flag that shuffle is currently in motion.
this.isTransitioning = true;
console.log('transitions to wait for:', promises.length);
Promise.all(promises).then(this._movementFinished.bind(this));
// A call to layout happened, but none of the newly filtered items will
@ -1385,14 +1390,13 @@ return /******/ (function(modules) { // webpackBootstrap
// Null DOM references
this.items = null;
this.$el = null;
this.options.sizer = null;
this.element = null;
this._transitions = null;
// Set a flag so if a debounced resize has been triggered,
// it can first check if it is actually isDestroyed and not doing anything
this.destroyed = true;
this.isDestroyed = true;
}
/**

File diff suppressed because one or more lines are too long

@ -3,9 +3,11 @@
const gulp = require('gulp');
const config = require('../config');
const cssTask = require('./css');
const testTask = require('./test');
module.exports = function setWatching(done) {
config.watch = true;
gulp.watch('_scss/*.scss', cssTask);
gulp.watch('test/*', testTask);
done();
};

@ -0,0 +1,24 @@
'use strict';
const gulp = require('gulp');
const mochaPhantomJS = require('gulp-mocha-phantomjs');
const config = require('../config');
module.exports = function () {
return gulp.src('test/runner.html', {
read: false,
})
.pipe(mochaPhantomJS({
phantomjs: {
useColors: true,
},
}))
// https://github.com/gulpjs/gulp/issues/259#issuecomment-61976830
.on('error', function (err) {
if (config.watch) {
console.error(err.message);
this.emit('end');
}
});
};

@ -6,6 +6,7 @@ gulp.task('scripts', require('./gulp/tasks/compile'));
gulp.task('set-watching', require('./gulp/tasks/set-watching'));
gulp.task('css', require('./gulp/tasks/css'));
gulp.task('jekyll', require('./gulp/tasks/jekyll'));
gulp.task('test', require('./gulp/tasks/test'));
gulp.task('watch', gulp.series(
'set-watching',
@ -13,4 +14,4 @@ gulp.task('watch', gulp.series(
'jekyll'
));
gulp.task('default', gulp.series('scripts'));
gulp.task('default', gulp.series('scripts', 'test'));

@ -254,14 +254,10 @@ Modules.Favicon = (function (doc) {
return Favicon;
}(document));
document.addEventListener('DOMContentLoaded', function () {
Modules.NavTray.initialize();
Modules.NavTray.initialize();
// Only animate the favicon on the homepage so that
// timeline tests aren't filled with junk
if (window.location.pathname === '/Shuffle/') {
var src = site_url + '/img/favicon-sprite.png';
new Modules.Favicon(src, 21, 7, 3000 * 1);
}
});
// Only animate the favicon on the homepage so that
// timeline tests aren't filled with junk
if (window.location.pathname === '/Shuffle/') {
new Modules.Favicon(site_url + '/img/favicon-sprite.png', 21, 7, 3000 * 1);
}

@ -16,30 +16,17 @@ requirejs.config({
define(function (require) {
'use strict';
// Get Shuffle.
var Shuffle = require('shuffle');
// Page-level JavaScript used for the demo pages.
require('evenheights');
require('page');
// Get Shuffle.
var Shuffle = require('shuffle');
// Create a new shuffle instance.
var element = document.getElementById('grid');
var shuffle = new Shuffle(element, {
window.myShuffle = new Shuffle(element, {
itemSelector: '.js-item',
sizer: document.getElementById('js-sizer'),
});
// DO NOT use this for determining when images load.
// See http://vestride.github.io/Shuffle/images/
// Use something like imagesLoaded.
var imgs = this.element.querySelectorAll('img');
var handler = function () {
shuffle.update();
};
for (var i = imgs.length - 1; i >= 0; i--) {
imgs[i].addEventListener('load', handler, false);
}
});

@ -3,7 +3,6 @@
"version": "4.0.0",
"description": "Categorize, sort, and filter a responsive grid of items",
"keywords": [
"jquery-plugin",
"gallery",
"shuffle",
"layout",
@ -20,20 +19,7 @@
"compile": "gulp scripts",
"css": "gulp css",
"watch": "gulp watch",
"test": "jest"
},
"jest": {
"verbose": true,
"scriptPreprocessor": "<rootDir>/test/preprocessor.js",
"testDirectoryName": "test",
"testPathDirs": [
"<rootDir>/test"
],
"testPathIgnorePatterns": [
"/node_modules/",
"preprocessor.js"
],
"testRunner": "<rootDir>/node_modules/jest-cli/src/testRunners/jasmine/jasmine2.js"
"test": "gulp test"
},
"repository": {
"type": "git",
@ -54,17 +40,19 @@
"devDependencies": {
"autoprefixer": "^6.3.4",
"babel-core": "^6.5.2",
"babel-jest": "^6.0.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-register": "^6.5.2",
"chai": "^3.5.0",
"chai-dom": "^1.4.0",
"es6-promise": "^3.1.2",
"gulp": "gulpjs/gulp.git#4.0",
"gulp-mocha-phantomjs": "^0.11.0",
"gulp-postcss": "^6.1.0",
"gulp-sass": "^2.2.0",
"gulp-shell": "^0.5.2",
"gulp-util": "^3.0.7",
"jest-cli": "^0.8.2",
"jest-webpack-alias": "^2.2.0",
"mocha": "^2.4.5",
"webpack": "^1.12.13"
}
}

@ -476,10 +476,10 @@ class Shuffle {
_dispatch(name, details = {}) {
details.shuffle = this;
return !this.element.dispatchEvent(new CustomEvent(name, {
bubbles: false,
cancelable: true,
detail: details,
}));
bubbles: true,
cancelable: false,
detail: details,
}));
}
/**
@ -1111,14 +1111,13 @@ class Shuffle {
// Null DOM references
this.items = null;
this.$el = null;
this.options.sizer = null;
this.element = null;
this._transitions = null;
// Set a flag so if a debounced resize has been triggered,
// it can first check if it is actually isDestroyed and not doing anything
this.destroyed = true;
this.isDestroyed = true;
}
/**

@ -1,16 +0,0 @@
'use strict';
require('babel-register');
var babelJest = require('babel-jest');
var webpackAlias = require('jest-webpack-alias');
module.exports = {
process: function (src, filename) {
if (filename.indexOf('node_modules') === -1 && filename.match(/\.jsx?$/)) {
src = babelJest.process(src, filename);
src = webpackAlias.process(src, filename);
}
return src;
},
};

@ -0,0 +1,31 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="../node_modules/mocha/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<!-- Polyfills -->
<script src="../node_modules/es6-promise/dist/es6-promise.min.js"></script>
<script>ES6Promise.polyfill();</script>
<!-- Testing Framework -->
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/chai-dom/chai-dom.js"></script>
<!-- Source -->
<script src="../dist/shuffle.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test.js"></script>
<script>
mocha.checkLeaks();
mocha.globals([]);
mocha.run();
</script>
</body>
</html>

@ -0,0 +1,288 @@
/* globals describe, it, beforeEach, afterEach */
/* jshint expr: true */
'use strict';
var expect = window.chai.expect;
describe('shuffle', function () {
var Shuffle = window.shuffle;
var fixtures = {};
var fixture;
var instance;
function getFixture(id) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.responseText);
};
xhr.onerror = reject;
xhr.open('GET', 'fixtures/' + id + '.html');
xhr.send();
});
}
function appendFixture(id) {
function insert(content) {
content = content.trim();
var holder = document.createElement('div');
holder.innerHTML = content;
var element = holder.firstChild;
document.body.appendChild(element);
fixture = element;
}
if (fixtures[id]) {
insert(fixtures[id]);
return Promise.resolve();
} else {
return getFixture(id).then(function (html) {
fixtures[id] = html;
insert(html);
});
}
}
function cleanup() {
if (instance.element) {
instance.destroy();
}
if (fixture) {
fixture.parentNode.removeChild(fixture);
}
instance = null;
fixture = null;
}
function removeFixture(done) {
if (instance.isInitialized) {
cleanup();
done();
} else {
instance.element.addEventListener(Shuffle.EventType.DONE, function onDone() {
instance.element.removeEventListener(Shuffle.EventType.DONE, onDone);
cleanup();
done();
});
}
}
function once(element, eventType, fn) {
var handler = function (e) {
element.removeEventListener(eventType, handler);
fn(e);
};
element.addEventListener(eventType, handler);
}
function id(id) {
return document.getElementById(id);
}
describe('regular fixture', function () {
beforeEach(function (done) {
appendFixture('regular').then(done);
});
afterEach(removeFixture);
it('should have default options', function (done) {
instance = new Shuffle(fixture);
expect(instance.items.length).to.equal(10);
expect(instance.visibleItems).to.equal(10);
expect(instance.options.group).to.equal('all');
expect(instance.options.speed).to.equal(250);
expect(instance.options.itemSelector).to.equal('*');
expect(instance.options.sizer).to.equal(null);
expect(instance.options.columnWidth).to.equal(0);
expect(instance.options.gutterWidth).to.equal(0);
expect(instance.options.delimeter).to.equal(null);
expect(instance.options.initialSort).to.equal(null);
expect(instance.options.throttleTime).to.equal(300);
expect(instance.options.sequentialFadeDelay).to.equal(150);
expect(instance.useSizer).to.equal(false);
expect(instance.id).to.equal('shuffle_0');
expect(instance.isInitialized).to.be.false;
instance.element.addEventListener(Shuffle.EventType.DONE, function onDone() {
instance.element.removeEventListener(Shuffle.EventType.DONE, onDone);
expect(instance.isInitialized).to.be.true;
done();
});
});
it('should add classes and default styles', function () {
instance = new Shuffle(fixture);
expect(instance.element).to.have.class('shuffle');
var styles = window.getComputedStyle(instance.element, null);
expect(styles.position).to.equal('relative');
expect(styles.overflow).to.equal('hidden');
expect(instance.containerWidth).not.to.equal(0);
instance.items.forEach(function (item) {
expect(item.element).to.have.class('shuffle-item');
expect(item.element).to.have.class('filtered');
expect(item.element.style.opacity).to.be.defined;
expect(item.element.style.position).to.equal('absolute');
expect(item.element.style.visibility).to.equal('visible');
expect(item.isVisible).to.equal(true);
expect(item.scale).to.equal(Shuffle.ShuffleItem.Scale.VISIBLE);
expect(item.point.x).to.be.defined;
expect(item.point.y).to.be.defined;
});
});
it('should be 3 columns with gutters', function () {
fixture.style.width = '1000px';
for (var i = 0; i < fixture.children.length; i++) {
fixture.children[i].style.width = '300px';
fixture.children[i].style.height = '150px';
}
instance = new Shuffle(fixture, {
columnWidth: 300,
gutterWidth: 50,
});
expect(instance.colWidth).to.equal(350);
expect(instance.cols).to.equal(3);
expect(instance.positions).to.deep.equal([600, 450, 450]);
});
it('can have a function for columns and gutters', function () {
fixture.style.width = '1000px';
for (var i = 0; i < fixture.children.length; i++) {
fixture.children[i].style.width = '300px';
fixture.children[i].style.height = '150px';
}
instance = new Shuffle(fixture, {
columnWidth: function (containerWidth) {
expect(containerWidth).to.equal(1000);
return 300;
},
gutterWidth: function () {
return 50;
},
});
expect(instance._getGutterSize(1000)).to.equal(50);
expect(instance._getColumnSize(1000, 50)).to.equal(350);
expect(instance.colWidth).to.equal(350);
expect(instance.cols).to.equal(3);
expect(instance.positions).to.deep.equal([600, 450, 450]);
});
it('can have a function for columns/gutters and span multiple columns', function () {
fixture.style.width = '1200px';
for (var i = 0; i < fixture.children.length; i++) {
fixture.children[i].style.width = '300px';
fixture.children[i].style.height = '10px';
}
fixture.children[1].style.width = '600px';
fixture.children[5].style.width = '600px';
fixture.children[6].style.width = '900px';
instance = new Shuffle(fixture, {
columnWidth: function (containerWidth) {
expect(containerWidth).to.equal(1200);
return 300;
},
gutterWidth: function () {
return 0;
},
});
expect(instance._getGutterSize(1200)).to.equal(0);
expect(instance._getColumnSize(1200, 0)).to.equal(300);
expect(instance.colWidth).to.equal(300);
expect(instance.cols).to.equal(4);
expect(instance.positions).to.deep.equal([40, 40, 30, 30]);
});
/*
it('can filter by the data attribute', function (done) {
console.log('new');
instance = new Shuffle(fixture, {
speed: 0,
});
function first() {
console.log('1');
once(fixture, Shuffle.EventType.LAYOUT, second);
console.log('filter. wait.');
instance.filter('design');
}
function second() {
console.log('2');
expect(instance.visibleItems).to.equal(3);
var concealed = [3, 4, 5, 6, 7, 8, 10].map(function (num) {
return id('item' + num);
});
var filtered = [1, 2, 9].map(function (num) {
return id('item' + num);
});
concealed.forEach(function (element) {
expect(element).to.have.class(Shuffle.ClassName.CONCEALED);
expect(element.style.visibility).to.equal('hidden');
});
filtered.forEach(function (element) {
expect(element).to.have.class(Shuffle.ClassName.FILTERED);
expect(element.style.visibility).to.equal('visible');
});
once(fixture, Shuffle.EventType.LAYOUT, third);
// Filter by green.
instance.filter('green');
}
function third() {
console.log('3');
expect(instance.visibleItems).to.equal(2);
var concealed = [1, 2, 5, 6, 7, 8, 9, 10].map(function (num) {
return id('item' + num);
});
var filtered = [3, 4].map(function (num) {
return id('item' + num);
});
concealed.forEach(function (element) {
expect(element).to.have.class(Shuffle.ClassName.CONCEALED);
expect(element.style.visibility).to.equal('hidden');
});
filtered.forEach(function (element) {
expect(element).to.have.class(Shuffle.ClassName.FILTERED);
expect(element.style.visibility).to.equal('visible');
});
done();
}
once(fixture, Shuffle.EventType.DONE, first);
});
*/
});
});
Loading…
Cancel
Save