Skip to content

Commit

Permalink
Add jasmine for JS testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Vague authored and Matt Vague committed Dec 8, 2011
1 parent 5ee02e5 commit 2fad9b1
Show file tree
Hide file tree
Showing 10 changed files with 3,456 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ group :test do
gem 'launchy'
gem 'jslint_on_rails', '~> 1.0.6'
gem 'guard-rspec'
gem 'jasmine'
end
11 changes: 10 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "bundler"
require 'rake'
Bundler.setup
Bundler::GemHelper.install_tasks

require 'rake'

def cmd(command)
puts command
Expand All @@ -16,3 +16,12 @@ FileList['tasks/**/*.rake'].each { |task| import task }

# Run the specs & cukes
task :default => :test

begin
require 'jasmine'
load 'jasmine/tasks/jasmine.rake'
rescue LoadError
task :jasmine do
abort "Jasmine is not available. In order to run jasmine, you must: (sudo) gem install jasmine"
end
end
3 changes: 3 additions & 0 deletions spec/javascripts/helpers/SpecHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
beforeEach(function() {
window.inject = $.jasmine.inject;
});
108 changes: 108 additions & 0 deletions spec/javascripts/helpers/vendor/jasmine-fixture-0.0.5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* jasmine-fixture Makes injecting HTML snippets into the DOM easy & clean!
* site: https://github.com/searls/jasmine-fixture */
(function($){
var originalJasmineFixture = window.jasmineFixture,
defaultConfiguration = {
el:'div',
cssClass:'',
id:'',
text: '',
html: '',
defaultAttribute: 'class',
attrs: {}
};

window.jasmineFixture = function($) {
var isReady = false,
rootId = 'specContainer',
defaults = $.extend({},defaultConfiguration);

$.jasmine = {
inject: function(arg,context) {
if(isReady !== true) init();
var parent = context ? context : $('#'+rootId),
$toInject;
if(itLooksLikeHtml(arg)) {
$toInject = $(arg);
} else {
var config = $.extend({},defaults,arg,{ userString: arg });
$toInject = $('<'+config.el+'></'+config.el+'>');
applyAttributes($toInject,config);
injectContents($toInject,config);
}
return $toInject.appendTo(parent);
},
configure: function(config) {
$.extend(defaults,config);
},
restoreDefaults: function(){
defaults = $.extend({},defaultConfiguration);
},
noConflict: function() {
window.jasmineFixture = originalJasmineFixture;
return this;
}
};

$.fn.inject = function(html){
return $.jasmine.inject(html,$(this));
};

var applyAttributes = function($html,config) {
var attrs = $.extend({},{
id: config.id,
'class': config['class'] || config.cssClass
}, config.attrs);
if(isString(config.userString)) {
attrs[config.defaultAttribute] = config.userString;
}
for(var key in attrs) {
if(attrs[key]) {
$html.attr(key,attrs[key]);
}
}
};

var injectContents = function($el,config){
if(config.text && config.html) {
throw "Error: because they conflict, you may only configure inject() to set `html` or `text`, not both! \n\nHTML was: "+config.html+" \n\n Text was: "+config.text
} else if(config.text) {
$el.text(config.text);
} else if(config.html) {
$el.html(config.html);
}
}

var itLooksLikeHtml = function(arg) {
return isString(arg) && arg.indexOf('<') !== -1
};

var isString = function(arg) {
return arg && arg.constructor === String;
};

var init = function() {
$('body').append('<div id="'+rootId+'"></div>');
isReady = true;
};

var tidyUp = function() {
$('#'+rootId).remove();
isReady = false;
};

$(function(jQuery){
init();
});
afterEach(function(){
tidyUp();
});

return $.jasmine;
};

if(jQuery) {
var jasmineFixture = window.jasmineFixture(jQuery);
window.inject = window.inject || jasmineFixture.inject;
}
})(jQuery);
Loading

0 comments on commit 2fad9b1

Please sign in to comment.