2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-17 01:55:09 +00:00
iconify/tests/browser/observer_test.test.js

190 lines
6.7 KiB
JavaScript
Raw Normal View History

2017-02-22 15:25:57 +00:00
(function() {
"use strict";
var expect = chai.expect,
should = chai.should();
/**
* Skip 2 ticks. Observer class uses 2 ticks to do its magic, so test must skip 2 ticks before asserting
*
* @param callback
*/
function skipTicks(callback) {
setTimeout(function() {
setTimeout(callback, 0);
}, 0);
}
/* Observer() */
describe('Testing observer', function() {
it('inactive observer', function(done) {
var context, SimpleSVG, local,
2017-02-22 15:25:57 +00:00
tempId = 'observer-dummy',
called = false;
// Check if jQuery is present
expect(typeof jQuery).to.be.equal('function', 'jQuery is missing');
// Add temporary element
jQuery('#debug').append('<div id="' + tempId + '" />');
// Setup fake global scope and SimpleSVG instance
context = {
MutationObserver: MutationObserver
2017-02-22 15:25:57 +00:00
};
SimpleSVG = {
isReady: true
};
local = {
2017-02-22 15:25:57 +00:00
config: {
_root: document.getElementById(tempId)
2017-02-22 15:25:57 +00:00
},
initQueue: [],
nodesAdded: function(nodes) {
2017-02-22 15:25:57 +00:00
called = true;
},
};
// Load observer
Observer(SimpleSVG, local, context);
2017-02-22 15:25:57 +00:00
expect(typeof SimpleSVG.pauseObserving).to.be.equal('function', 'SimpleSVG.pauseObserving is missing');
expect(typeof SimpleSVG.resumeObserving).to.be.equal('function', 'SimpleSVG.resumeObserving is missing');
// Do not initialize observer!
2017-02-22 15:25:57 +00:00
// Check if observer is working
expect(called).to.be.equal(false, 'Observer callback was triggered too early');
jQuery('#' + tempId).append('<div />');
skipTicks(function() {
expect(called).to.be.equal(false, 'Observer callback was triggered, but it was supposed to be dummy function');
done();
});
});
it('setting up working observer', function(done) {
var context, SimpleSVG, local,
2017-02-22 15:25:57 +00:00
tempId = 'observer-basic',
called = false;
// Check if MutationObserver and jQuery are present
expect(!!MutationObserver).to.be.equal(true, 'This browser does not support MutationObserver. Test with modern browser');
expect(!!jQuery).to.be.equal(true, 'jQuery is missing');
// Add temporary element
jQuery('#debug').append('<div id="' + tempId + '" />');
// Setup fake global scope and SimpleSVG instance
context = {
MutationObserver: MutationObserver
};
SimpleSVG = {
isReady: true
};
local = {
2017-02-22 15:25:57 +00:00
config: {
_root: document.getElementById(tempId)
2017-02-22 15:25:57 +00:00
},
initQueue: [],
nodesAdded: function(nodes) {
2017-02-22 15:25:57 +00:00
called = true;
}
};
// Load observer
Observer(SimpleSVG, local, context);
2017-02-22 15:25:57 +00:00
expect(typeof SimpleSVG.pauseObserving).to.be.equal('function', 'SimpleSVG.pauseObserving is missing');
expect(typeof SimpleSVG.resumeObserving).to.be.equal('function', 'SimpleSVG.resumeObserving is missing');
// Init observer
local.initQueue.forEach(function(callback) {
callback();
});
2017-02-22 15:25:57 +00:00
// Check if observer is working
expect(called).to.be.equal(false, 'Observer callback was triggered too early');
jQuery('#' + tempId).append('<span />');
skipTicks(function() {
expect(called).to.be.equal(true, 'Observer callback was not triggered');
done();
});
});
it('pause and resume observer', function(done) {
var context, SimpleSVG, local,
2017-02-22 15:25:57 +00:00
tempId = 'observer-pause',
callbackResult = false;
// Check if MutationObserver and jQuery are present
expect(!!MutationObserver).to.be.equal(true, 'This browser does not support MutationObserver. Test with modern browser');
expect(!!jQuery).to.be.equal(true, 'jQuery is missing');
// Add temporary element
jQuery('#debug').append('<div id="' + tempId + '" />');
// Setup fake global scope and SimpleSVG instance
context = {
MutationObserver: MutationObserver
};
SimpleSVG = {
isReady: true
};
local = {
2017-02-22 15:25:57 +00:00
config: {
_root: document.getElementById(tempId)
2017-02-22 15:25:57 +00:00
},
initQueue: [],
nodesAdded: function(nodes) {
2017-02-22 15:25:57 +00:00
callbackResult = nodes;
}
};
// Load observer
Observer(SimpleSVG, local, context);
2017-02-22 15:25:57 +00:00
expect(typeof SimpleSVG.pauseObserving).to.be.equal('function', 'SimpleSVG.pauseObserving is missing');
expect(typeof SimpleSVG.resumeObserving).to.be.equal('function', 'SimpleSVG.resumeObserving is missing');
// Init observer
local.initQueue.forEach(function(callback) {
callback();
});
2017-02-22 15:25:57 +00:00
// Call observer
expect(callbackResult).to.be.equal(false, 'Observer callback was triggered too early');
jQuery('#' + tempId).append('<p />');
skipTicks(function() {
expect(callbackResult).to.not.be.equal(false, 'Observer callback was not triggered');
expect(callbackResult.length).to.be.equal(1, 'Observer was supposed to return 1 node');
// Pause observer
callbackResult = false;
SimpleSVG.pauseObserving();
// Add element
jQuery('#' + tempId).append('<i />');
skipTicks(function () {
expect(callbackResult).to.be.equal(false, 'Observer was supposed to be paused');
// Resume observer
SimpleSVG.resumeObserving();
// Add elements
jQuery('#' + tempId).append('<strong /><span /><i />');
skipTicks(function () {
expect(callbackResult).to.not.be.equal(false, 'Observer was supposed to have resumed working');
expect(callbackResult.length).to.be.equal(3, 'Observer was supposed to return 3 nodes');
done();
});
});
});
});
});
})();