Skip to content

Commit

Permalink
adding cb feature to fixtures.load, Fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
badunk committed Nov 10, 2013
1 parent 7c5564d commit 8c5693e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
36 changes: 20 additions & 16 deletions fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
var content = self.window().document.body.innerHTML;
return content;
};
self.load = function(){
addToContainer(self.read.apply(self, arguments));
self.load = function(html, cb){
addToContainer(self.read.apply(self, arguments), cb);
};
self.set = function(html){
addToContainer(html);
Expand All @@ -45,10 +45,9 @@
self.read = function(){
var htmlChunks = [];

var fixtureUrls = arguments;
for (var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++){
htmlChunks.push(getFixtureHtml(fixtureUrls[urlIndex]));
}
var fixtureUrls = Array.prototype.slice.call(arguments, 0).forEach(function(argument){
if (typeof argument === 'string') htmlChunks.push(getFixtureHtml(argument));
});
return htmlChunks.join('');
};
self.clearCache = function(){
Expand All @@ -61,26 +60,32 @@
iframe.parentNode.removeChild(iframe);
};
var createContainer = function(html){

var cb = typeof arguments[arguments.length - 1] === 'function' ? arguments[arguments.length -1] : null;
var iframe = document.createElement('iframe');
iframe.setAttribute('id', self.containerId);
iframe.style.display = 'none';

document.body.appendChild(iframe);
var doc = iframe.contentWindow || iframe.contentDocument;
doc = doc.document ? doc.document : doc;

if (cb){
var iframeReady = setInterval(function(){
if (doc.readyState === 'complete'){
clearInterval(iframeReady);
cb();
}
}, 0);
}

doc.open();
doc.write(html);
doc.write(html)
doc.close();
};
var addToContainer = function(html){
var container = document.getElementById(self.containerId);
if (!container){
createContainer(html);
} else{
var iframeWindow = container.contentWindow || container.contentDocument;
iframeWindow.document.body.innerHTML += html;
}
if (!container) createContainer.apply(self, arguments);
else self.window().document.body.innerHTML += html;
};
var getFixtureHtml = function(url){
if (typeof fixturesCache[url] === 'undefined'){
Expand All @@ -101,8 +106,7 @@
var objToHTML = function(obj){
var divElem = document.createElement('div');
for (var key in obj){
// IE < 9 compatibility
if (key === 'class'){
if (key === 'class'){ // IE < 9 compatibility
divElem.className = obj[key];
continue;
}
Expand Down
53 changes: 20 additions & 33 deletions test/fixtures-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ define(function(require){
expect(fixtures.body()).to.equal('test');
});
});
describe('when callback is passed', function(){
var stub;
beforeEach(function(){
var xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function(xhr){
stub = sinon.stub(xhr, "send", function(something){
xhr.responseText = '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>';
});
};
});
afterEach(function(){
stub.restore();
});
it('executes callback upon iframe ready', function(done){
fixtures.load('some-fixture.html', function(){
expect(fixtures.window().$).to.exist;
done();
});
});
});
});
describe("cache", function() {
describe("read after cache", function() {
Expand Down Expand Up @@ -198,38 +218,5 @@ define(function(require){
});
});
});

describe("fixtures using mock AJAX call", function() {
var defaultFixturesPath;

beforeEach(function() {
defaultFixturesPath = fixtures.path;
fixtures.path = 'spec/fixtures';
});

afterEach(function() {
fixtures.path = defaultFixturesPath;
});

describe('when attempting to load an external dependency (like jQuery)', function(){
var stub;
beforeEach(function(){
var xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function(xhr){
stub = sinon.stub(xhr, "send", function(something){
console.log('asdf');
xhr.responseText = '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>';
});
};
});
afterEach(function(){
stub.restore();
});
it('jquery is immediately available after loading', function(){
fixtures.load('some-fixture.html');
expect(fixtures.window().$).to.exist;
});
});
});
};
});

0 comments on commit 8c5693e

Please sign in to comment.