Permalink
Browse files

added amd module loading (haven't tested besides regression tests), r…

…emoved all jQuery dependencies from test harness, specs, and library (tested on FF, Chrome, Safari)
  • Loading branch information...
1 parent 885e8c6 commit d0e9bdfe0a522b0317e79ebe4b059cc4042e9389 @badunk committed Jan 11, 2013
Showing with 129 additions and 103 deletions.
  1. +101 −80 fixtures.js
  2. +28 −22 test/fixtures-spec.js
  3. +0 −1 test/index.html
View
181 fixtures.js
@@ -1,86 +1,107 @@
"use strict";
-var fixtures = fixtures || new function(){
- var fixturesCache = {};
- var self = this;
+(function(fixtures){
+ if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
+ // NodeJS
+ module.exports = fixtures;
+ } else if (typeof define === 'function' && define.amd){
+ define(function(){
+ return fixtures;
+ });
+ } else{
+ var global = (false || eval)("this");
+ global.fixtures = fixtures;
+ }
- self.containerId = 'js-fixtures';
- self.path = 'spec/javascripts/fixtures';
- self.window = function(){
- var iframe = document.getElementById(self.containerId);
- if (!iframe) return null;
+}(new function(){
+ var fixturesCache = {};
+ var self = this;
- return iframe.contentWindow || iframe.contentDocument;
- };
- self.body = function(){
- return $('#' + self.containerId).contents().find('body').html();
- };
- self.set = function(html){
- self.cleanUp();
- addToContainer(html);
- };
- self.appendSet = function(html){
- addToContainer(html);
- };
- self.preload = function(){
- self.read.apply(self, arguments);
- };
- self.load = function(){
- self.cleanUp();
- createContainer(self.read.apply(self, arguments));
- };
- self.appendLoad = function(){
- addToContainer(self.read.apply(self, arguments));
- };
- self.read = function(){
- var htmlChunks = [];
+ self.containerId = 'js-fixtures';
+ self.path = 'spec/javascripts/fixtures';
+ self.window = function(){
+ var iframe = document.getElementById(self.containerId);
+ if (!iframe) return null;
- var fixtureUrls = arguments;
- for (var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++){
- htmlChunks.push(getFixtureHtml(fixtureUrls[urlIndex]));
- }
- return htmlChunks.join('');
- };
- self.clearCache = function(){
- fixturesCache = {};
- };
- self.cleanUp = function(){
- jQuery('#' + self.containerId).remove();
- };
- var createContainer = function(html){
+ return iframe.contentWindow || iframe.contentDocument;
+ };
+ self.body = function(){
+ if (!self.window()) return null;
- var iframe = document.createElement('iframe');
- iframe.setAttribute("id", self.containerId);
- $('body').append(iframe);
- var doc = iframe.contentWindow || iframe.contentDocument;
- doc = doc.document ? doc.document : doc;
+ var content = self.window().document.body.innerHTML;
+ return content;
+ };
+ self.set = function(html){
+ self.cleanUp();
+ addToContainer(html);
+ };
+ self.appendSet = function(html){
+ addToContainer(html);
+ };
+ self.preload = function(){
+ self.read.apply(self, arguments);
+ };
+ self.load = function(){
+ self.cleanUp();
+ createContainer(self.read.apply(self, arguments));
+ };
+ self.appendLoad = function(){
+ addToContainer(self.read.apply(self, arguments));
+ };
+ self.read = function(){
+ var htmlChunks = [];
- doc.open();
- doc.write(html);
- doc.close();
- };
- var addToContainer = function(html){
- var container = jQuery('#' + self.containerId);
- if (!container.length){
- createContainer(html);
- }
- else{
- container.contents().find('body').append(html);
- }
- };
- var getFixtureHtml = function(url){
- if (typeof fixturesCache[url] === 'undefined'){
- loadFixtureIntoCache(url);
- }
- return fixturesCache[url];
- };
- var loadFixtureIntoCache = function(relativeUrl){
- var url = makeFixtureUrl(relativeUrl);
- var request = new XMLHttpRequest();
- request.open("GET", url + "?" + new Date().getTime(), false);
- request.send(null);
- fixturesCache[relativeUrl] = request.responseText;
- };
- var makeFixtureUrl = function(relativeUrl){
- return self.path.match('/$') ? self.path + relativeUrl : self.path + '/' + relativeUrl;
- };
-};
+ var fixtureUrls = arguments;
+ for (var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++){
+ htmlChunks.push(getFixtureHtml(fixtureUrls[urlIndex]));
+ }
+ return htmlChunks.join('');
+ };
+ self.clearCache = function(){
+ fixturesCache = {};
+ };
+ self.cleanUp = function(){
+ var iframe = document.getElementById(self.containerId);
+ if(!iframe) return null;
+
+ iframe.parentNode.removeChild(iframe);
+ };
+ var createContainer = function(html){
+
+ 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;
+
+ doc.open();
+ 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;
+ }
+ };
+ var getFixtureHtml = function(url){
+ if (typeof fixturesCache[url] === 'undefined'){
+ loadFixtureIntoCache(url);
+ }
+ return fixturesCache[url];
+ };
+ var loadFixtureIntoCache = function(relativeUrl){
+ var url = makeFixtureUrl(relativeUrl);
+ var request = new XMLHttpRequest();
+ request.open("GET", url + "?" + new Date().getTime(), false);
+ request.send(null);
+ fixturesCache[relativeUrl] = request.responseText;
+ };
+ var makeFixtureUrl = function(relativeUrl){
+ return self.path.match('/$') ? self.path + relativeUrl : self.path + '/' + relativeUrl;
+ };
+ }
+));
View
50 test/fixtures-spec.js
@@ -1,11 +1,14 @@
-describe("fixtures.Fixtures", function(){
+describe("fixtures", function(){
var ajaxData = "some ajax data";
var fixtureUrl = "some_url";
var anotherFixtureUrl = "another_url";
var server = sinon.fakeServer.create();
var xhr = sinon.useFakeXMLHttpRequest();
var fixturesBody = function(){
- return $('#' + fixtures.containerId).contents().find('body');
+ var iframe = document.getElementById(fixtures.containerId);
+ var iframeWindow = iframe.contentWindow || iframe.contentDocument;
+
+ return iframeWindow.document.body;
};
var appendFixturesContainerToDom = function(){
fixtures.set('old content');
@@ -89,20 +92,20 @@ describe("fixtures.Fixtures", function(){
describe("load", function(){
it("should insert fixture HTML into container", function(){
fixtures.load(fixtureUrl);
- expect(fixturesBody().html()).to.equal(ajaxData);
+ expect(fixturesBody().innerHTML).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);
- expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
+ expect(fixturesBody().innerHTML).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);
- expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
+ expect(fixturesBody().innerHTML).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);
- expect(fixturesBody().size()).to.equal(1);
+ expect(document.getElementById(fixtures.containerId)).to.not.be.null;
});
});
describe("when fixture container exists", function(){
@@ -121,7 +124,7 @@ describe("fixtures.Fixtures", function(){
});
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
fixtures.load(fixtureUrl);
- expect(fixturesBody().html()).to.equal('test');
+ expect(fixturesBody().innerHTML).to.equal('test');
});
});
});
@@ -132,19 +135,19 @@ describe("fixtures.Fixtures", function(){
});
it("should insert fixture HTML into container", function(){
fixtures.appendLoad(fixtureUrl);
- expect(fixturesBody().html()).to.equal(ajaxData);
+ expect(fixturesBody().innerHTML).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);
- expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
+ expect(fixturesBody().innerHTML).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);
- expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
+ expect(fixturesBody().innerHTML).to.equal(ajaxData + ajaxData);
});
it("should automatically create fixtures container and append it to the DOM", function(){
fixtures.appendLoad(fixtureUrl);
- expect(fixturesBody().size()).to.equal(1);
+ expect(document.getElementById(fixtures.containerId)).to.not.be.null;
});
describe("with a prexisting fixture",function(){
beforeEach(function() {
@@ -153,12 +156,12 @@ describe("fixtures.Fixtures", function(){
it("should add new content", function() {
fixtures.appendLoad(fixtureUrl);
- expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
+ expect(fixturesBody().innerHTML).to.equal(ajaxData + ajaxData);
});
it("should not add a new fixture container", function(){
fixtures.appendLoad(fixtureUrl);
- expect(fixturesBody().size()).to.equal(1);
+ expect(document.getElementById(fixtures.containerId)).to.not.be.null;
});
});
describe("when fixture contains an inline script tag", function(){
@@ -169,7 +172,7 @@ describe("fixtures.Fixtures", function(){
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
fixtures.appendLoad(fixtureUrl);
- expect(fixturesBody().html()).to.equal('test');
+ expect(fixturesBody().innerHTML).to.equal('test');
});
});
});
@@ -197,13 +200,13 @@ describe("fixtures.Fixtures", function(){
describe("set", function() {
it("should insert HTML into container", function() {
fixtures.set(html);
- expect(fixturesBody().html().toLowerCase()).to.equal(html);
+ expect(fixturesBody().innerHTML.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);
- expect(fixturesBody().size()).to.equal(1);
+ expect(document.getElementById(fixtures.containerId)).to.not.be.null;
});
});
@@ -214,21 +217,21 @@ describe("fixtures.Fixtures", function(){
it("should replace it with new content", function() {
fixtures.set(html);
- expect(fixturesBody().html().toLowerCase()).to.equal(html);
+ expect(fixturesBody().innerHTML.toLowerCase()).to.equal(html);
});
});
});
describe("appendSet",function(){
it("should insert HTML into container", function() {
fixtures.appendSet(html);
- expect(fixturesBody().html().toLowerCase()).to.equal(html);
+ expect(fixturesBody().innerHTML.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);
- expect(fixturesBody().size()).to.equal(1);
+ expect(document.getElementById(fixtures.containerId)).to.not.be.null;
});
});
@@ -239,7 +242,7 @@ describe("fixtures.Fixtures", function(){
it("should add new content", function() {
fixtures.appendSet(html);
- expect(fixturesBody().text()).to.equal(text+text);
+ expect(fixturesBody().innerHTML).to.equal(html+html);
});
});
});
@@ -248,12 +251,15 @@ describe("fixtures.Fixtures", function(){
it("should remove fixtures container from DOM", function() {
appendFixturesContainerToDom();
fixtures.cleanUp();
- expect(fixturesBody().size()).to.equal(0);
+ expect(document.getElementById(fixtures.containerId)).to.be.null;
+ });
+ it('should succeed even if cleanup is called without loading', function(){
+ fixtures.cleanUp();
});
});
});
-describe("fixtures.Fixtures using mock AJAX call", function() {
+describe("fixtures using mock AJAX call", function() {
var defaultFixturesPath;
beforeEach(function() {
View
1 test/index.html
@@ -3,7 +3,6 @@
<title>Mocha</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/sinon/lib/sinon.js"></script>
<script src="../node_modules/sinon/lib/sinon/util/event.js"></script>

0 comments on commit d0e9bdf

Please sign in to comment.