Skip to content

Commit

Permalink
Changed the getters for functions, and chai for expect.js to achieve …
Browse files Browse the repository at this point in the history
…IE7+ support.
  • Loading branch information
jpka committed Dec 13, 2012
1 parent 86e591f commit 81f4fdb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 63 deletions.
10 changes: 5 additions & 5 deletions fixtures.js
Expand Up @@ -5,18 +5,18 @@ var fixtures = fixtures || new function(){

self.containerId = 'js-fixtures';
self.path = 'spec/javascripts/fixtures';
self.__defineGetter__("window", function(){
self.window = function(){
var iframe = document.getElementById(self.containerId);
if (!iframe) return null;

return iframe.contentWindow || iframe.contentDocument;
});
self.__defineGetter__("body", function(){
};
self.body = function(){
return $('#' + self.containerId).contents().find('body').html();
});
};
self.set = function(html){
self.cleanUp();
addToContainer(html)
addToContainer(html);
};
self.appendSet = function(html){
addToContainer(html);
Expand Down
14 changes: 9 additions & 5 deletions package.json
Expand Up @@ -2,7 +2,13 @@
"author": "Duncan Wong <baduncaduncan@gmail.com>",
"name": "js-fixtures",
"description": "jasmine-like fixtures without testing framework dependencies",
"keywords": [ "test", "fixture", "mocha", "testing", "jasmine" ],
"keywords": [
"test",
"fixture",
"mocha",
"testing",
"jasmine"
],
"version": "1.1.1",
"repository": {
"type": "git",
Expand All @@ -16,10 +22,8 @@
"node": ">= 0.4.0"
},
"devDependencies": {
"chai": ">= 1.0.0",
"mocha": ">= 1.0.0",
"sinon": ">= 1.3.4",
"sinon-chai": ">= 1.3.4",
"chai-jquery": ">= 1.0.0"
"mocha": "git://github.com/visionmedia/mocha.git",
"expect.js": "~0.2.0"
}
}
96 changes: 49 additions & 47 deletions test/fixtures-spec.js
Expand Up @@ -2,18 +2,23 @@ describe("fixtures.Fixtures", function(){
var ajaxData = "some ajax data";
var fixtureUrl = "some_url";
var anotherFixtureUrl = "another_url";
var server = sinon.fakeServer.create();;
var server = sinon.fakeServer.create();
var xhr = sinon.useFakeXMLHttpRequest();
var fixturesBody = function(){
return $('#' + fixtures.containerId).contents().find('body');
};
var appendFixturesContainerToDom = function(){
fixtures.set('old content');
};
beforeEach(function(){
var text = 'some text';
var html = '<div>' + text + '</div>';

beforeEach(function(done){
server = sinon.fakeServer.create();
server.respondWith(ajaxData);
server.autoRespondAfter = 1;

setTimeout(done, 0); //Workaround for Mocha bug in IE https://github.com/visionmedia/mocha/issues/502
});
afterEach(function(){
server.restore();
Expand All @@ -23,81 +28,81 @@ describe("fixtures.Fixtures", function(){

describe("default initial config values", function(){
it("should set 'js-fixtures' as the default container id", function(){
fixtures.containerId.should.equal('js-fixtures');
expect(fixtures.containerId).to.equal('js-fixtures');
});
it("should set 'spec/javascripts/fixtures/' as the default fixtures path", function(){
fixtures.path.should.equal('spec/javascripts/fixtures');
expect(fixtures.path).to.equal('spec/javascripts/fixtures');
});
it("should set body to null", function(){
expect(fixtures.body).to.be.null;
expect(fixtures.body()).to.be(null);
});
it("should set window to null", function(){
expect(fixtures.window).to.be.null;
expect(fixtures.window()).to.be(null);
});
});
describe("body", function(){
it("should not be null when initialized properly", function(){
fixtures.set('test');
fixtures.body.should.not.be.null;
expect(fixtures.body()).to.not.be(null);
});
it("should return the body contents of the iframe", function(){
var sillyString = 'some silly string';
fixtures.set(sillyString);
fixtures.body.should.equal(sillyString);
expect(fixtures.body()).to.equal(sillyString);
});
});
describe("window", function(){
it("should return the window object of the iframe", function(){
fixtures.set('test');
expect(fixtures.window).to.not.equal(window);
expect(fixtures.window()).to.not.equal(window);
});
it("should contain global vars injected into frame", function(){
fixtures.set('<scr' + 'ipt>' + 'var test = "globalVar"' + '</scr' + 'ipt>');
fixtures.window.test.should.equal('globalVar');
expect(fixtures.window().test).to.equal('globalVar');
});
});
describe("read", function(){
it("should return fixture HTML", function(){
var html = fixtures.read(fixtureUrl);

html.should.equal(ajaxData);
expect(html).to.equal(ajaxData);
});
it("should return duplicated HTML of a fixture when its url is provided twice in a single call", function(){
var html = fixtures.read(fixtureUrl, fixtureUrl);
html.should.equal(ajaxData + ajaxData);
expect(html).to.equal(ajaxData + ajaxData);
});
it("should return merged HTML of two fixtures when two different urls are provided in a single call", function(){
var html = fixtures.read(fixtureUrl, anotherFixtureUrl);
html.should.equal(ajaxData + ajaxData);
expect(html).to.equal(ajaxData + ajaxData);
});
it("should use the configured fixtures path concatenating it to the requested url (without concatenating a slash if it already has an ending one)", function(){
fixtures.path = 'a path ending with slash/';
fixtures.read(fixtureUrl);
server.requests[0].url.should.have.string('a path ending with slash/' + fixtureUrl);
expect(server.requests[0].url).to.have.string('a path ending with slash/' + fixtureUrl);
});
it("should use the configured fixtures path concatening it to the requested url (concatenating a slash if it doesn't have an ending one)", function(){
fixtures.path = "a path without an ending slash"
fixtures.path = "a path without an ending slash";
fixtures.read(fixtureUrl);
server.requests[0].url.should.have.string("a path without an ending slash/" + fixtureUrl);
expect(server.requests[0].url).to.have.string("a path without an ending slash/" + fixtureUrl);
});
});
describe("load", function(){
it("should insert fixture HTML into container", function(){
fixtures.load(fixtureUrl);
fixturesBody().html().should.equal(ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData);
});
it("should insert duplicated fixture HTML into container when the same url is provided twice in a single call", function(){
fixtures.load(fixtureUrl, fixtureUrl);
fixturesBody().html().should.equal(ajaxData + ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should insert merged HTML of two fixtures into container when two different urls are provided in a single call", function(){
fixtures.load(fixtureUrl, anotherFixtureUrl);
fixturesBody().html().should.equal(ajaxData + ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
describe("when fixture container does not exist", function(){
it("should automatically create fixtures container and append it to the DOM", function(){
fixtures.load(fixtureUrl);
fixturesBody().size().should.equal(1);
expect(fixturesBody().size()).to.equal(1);
});
});
describe("when fixture container exists", function(){
Expand All @@ -106,17 +111,17 @@ describe("fixtures.Fixtures", function(){
});
it("should replace it with new content", function(){
fixtures.load(fixtureUrl);
fixtures.body.should.equal(ajaxData);
expect(fixtures.body()).to.equal(ajaxData);
});
});
xdescribe("when fixture contains an inline script tag", function(){
beforeEach(function(){
ajaxData = "<script>document.writeln('test')</script>"
ajaxData = "<script>document.writeln('test')</script>";
server.respondWith(ajaxData);
});
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
fixtures.load(fixtureUrl);
fixturesBody().html().should.equal('test');
expect(fixturesBody().html()).to.equal('test');
});
});
});
Expand All @@ -127,19 +132,19 @@ describe("fixtures.Fixtures", function(){
});
it("should insert fixture HTML into container", function(){
fixtures.appendLoad(fixtureUrl);
fixturesBody().html().should.equal(ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData);
});
it("should insert duplicated fixture html into container when the same url is provided twice in a single call", function(){
fixtures.appendLoad(fixtureUrl, anotherFixtureUrl);
fixturesBody().html().should.equal(ajaxData + ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should insert merged HTML of two fixtures into container when two different urls are provided in a single call", function(){
fixtures.appendLoad(fixtureUrl, anotherFixtureUrl);
fixturesBody().html().should.equal(ajaxData + ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should automatically create fixtures container and append it to the DOM", function(){
fixtures.appendLoad(fixtureUrl);
fixturesBody().size().should.equal(1);
expect(fixturesBody().size()).to.equal(1);
});
describe("with a prexisting fixture",function(){
beforeEach(function() {
Expand All @@ -148,12 +153,12 @@ describe("fixtures.Fixtures", function(){

it("should add new content", function() {
fixtures.appendLoad(fixtureUrl);
fixturesBody().html().should.equal(ajaxData + ajaxData);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});

it("should not add a new fixture container", function(){
fixtures.appendLoad(fixtureUrl);
fixturesBody().size().should.equal(1);
expect(fixturesBody().size()).to.equal(1);
});
});
xdescribe("when fixture contains an inline script tag", function(){
Expand All @@ -164,43 +169,41 @@ describe("fixtures.Fixtures", function(){

it("should execute the inline javascript after the fixture has been inserted into the body", function(){
fixtures.appendLoad(fixtureUrl);
$.trim(fixturesBody().html()).should.equal('test');
})
expect($.trim(fixturesBody().html())).to.equal('test');
});
});
});
describe("preload", function() {
describe("read after preload", function() {
it("should go from cache", function() {
fixtures.preload(fixtureUrl, anotherFixtureUrl);
fixtures.read(fixtureUrl, anotherFixtureUrl);
server.requests.length.should.equal(2);
})
expect(server.requests.length).to.equal(2);
});

it("should return correct HTMLs", function() {
fixtures.preload(fixtureUrl, anotherFixtureUrl);
var html = fixtures.read(fixtureUrl, anotherFixtureUrl);
html.should.equal(ajaxData + ajaxData);
expect(html).to.equal(ajaxData + ajaxData);
});
});

it("should not preload the same fixture twice", function() {
fixtures.preload(fixtureUrl, fixtureUrl);
server.requests.length.should.equal(1);
expect(server.requests.length).to.equal(1);
});
});

describe("set", function() {
var html = '<div>some HTML</div>';

it("should insert HTML into container", function() {
fixtures.set(html);
fixturesBody().html().should.equal(html);
expect(fixturesBody().html().toLowerCase()).to.equal(html);
});

describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
fixtures.set(html);
fixturesBody().size().should.equal(1);
expect(fixturesBody().size()).to.equal(1);
});
});

Expand All @@ -211,22 +214,21 @@ describe("fixtures.Fixtures", function(){

it("should replace it with new content", function() {
fixtures.set(html);
fixturesBody().html().should.equal(html);
expect(fixturesBody().html().toLowerCase()).to.equal(html);
});
});
});

describe("appendSet",function(){
var html = '<div>some HTML</div>';
describe("appendSet",function(){
it("should insert HTML into container", function() {
fixtures.appendSet(html);
fixturesBody().html().should.equal(html);
expect(fixturesBody().html().toLowerCase()).to.equal(html);
});

describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
fixtures.appendSet(html);
fixturesBody().size().should.equal(1);
expect(fixturesBody().size()).to.equal(1);
});
});

Expand All @@ -237,7 +239,7 @@ describe("fixtures.Fixtures", function(){

it("should add new content", function() {
fixtures.appendSet(html);
fixturesBody().html().should.equal(html+html);
expect(fixturesBody().text()).to.equal(text+text);
});
});
});
Expand All @@ -246,7 +248,7 @@ describe("fixtures.Fixtures", function(){
it("should remove fixtures container from DOM", function() {
appendFixturesContainerToDom();
fixtures.cleanUp();
fixturesBody().size().should.equal(0);
expect(fixturesBody().size()).to.equal(0);
});
});
});
Expand Down Expand Up @@ -279,7 +281,7 @@ describe("fixtures.Fixtures using mock AJAX call", function() {
it("should load content of fixture file", function() {
var fixtureUrl = "real_non_mocked_fixture.html";
var fixtureContent = fixtures.read(fixtureUrl);
fixtureContent.should.equal('<div id="real_non_mocked_fixture"></div>');
expect(fixtureContent).to.equal('<div id="real_non_mocked_fixture"></div>');
});
});
});
11 changes: 5 additions & 6 deletions test/index.html
Expand Up @@ -11,14 +11,13 @@
<script src="../node_modules/sinon/lib/sinon/stub.js"></script>
<script src="../node_modules/sinon/lib/sinon/util/fake_xml_http_request.js"></script>
<script src="../node_modules/sinon/lib/sinon/util/fake_server.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/chai-jquery/chai-jquery.js"></script>
<script src="../node_modules/sinon-chai/lib/sinon-chai.js"></script>
<script src="../node_modules/expect.js/expect.js"></script>
<script src="../fixtures.js"></script>
<script>
mocha.setup('bdd');
var should = chai.should();
var expect = chai.expect;
mocha.setup({
ui: 'bdd',
globals: ['js-fixtures'] //for IE not to complain
});
window.onload = function () {
var runner = mocha.run();
};
Expand Down

0 comments on commit 81f4fdb

Please sign in to comment.