diff --git a/Gemfile b/Gemfile index 265fc4e9d..759f9c4da 100644 --- a/Gemfile +++ b/Gemfile @@ -213,5 +213,6 @@ group :development, :test do # Jasmine (client side application tests (JS)) gem 'jasmine', '2.0.0' + gem 'jasmine-jquery-rails', '2.0.2' gem 'sinon-rails', '1.9.0' end diff --git a/Gemfile.lock b/Gemfile.lock index 21598ae56..ca6c3117b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -224,6 +224,7 @@ GEM rack (>= 1.2.1) rake jasmine-core (2.0.0) + jasmine-jquery-rails (2.0.2) jquery-rails (3.0.4) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) @@ -506,6 +507,7 @@ DEPENDENCIES http_accept_language (= 1.0.2) i18n-inflector-rails (= 1.0.7) jasmine (= 2.0.0) + jasmine-jquery-rails (= 2.0.2) jquery-rails (= 3.0.4) json (= 1.8.1) markerb (= 1.0.2) diff --git a/spec/javascripts/app/views/feedback_view_spec.js b/spec/javascripts/app/views/feedback_view_spec.js index a8e57d8af..728ddc96f 100644 --- a/spec/javascripts/app/views/feedback_view_spec.js +++ b/spec/javascripts/app/views/feedback_view_spec.js @@ -41,7 +41,7 @@ describe("app.views.Feedback", function(){ context("when the user likes the post", function(){ it("the like action should be 'Unlike'", function(){ - spyOn(this.post.interactions, "userLike").andReturn(factory.like()); + spyOn(this.post.interactions, "userLike").and.returnValue(factory.like()); this.view.render() expect(this.link().text()).toContain(Diaspora.I18n.t('stream.unlike')) }) @@ -137,8 +137,8 @@ describe("app.views.Feedback", function(){ }) it("reshares the model", function(){ - spyOn(window, "confirm").andReturn(true); - spyOn(this.view.model.reshare(), "save").andReturn(new $.Deferred) + spyOn(window, "confirm").and.returnValue(true); + spyOn(this.view.model.reshare(), "save").and.returnValue(new $.Deferred) this.view.$("a.reshare").first().click(); expect(this.view.model.reshare().save).toHaveBeenCalled(); }) diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js index 92a22e0bb..3b69d5dd5 100644 --- a/spec/javascripts/helpers/SpecHelper.js +++ b/spec/javascripts/helpers/SpecHelper.js @@ -1,5 +1,7 @@ // for docs, see http://jasmine.github.io +var realXMLHttpRequest = window.XMLHttpRequest; + beforeEach(function() { $('#jasmine_content').html(spec.readFixture("underscore_templates")); @@ -164,7 +166,7 @@ spec.retrieveFixture = function(fixtureName) { // retrieve the fixture markup via xhr request to jasmine server try { - xhr = new XMLHttpRequest(); + xhr = new realXMLHttpRequest(); xhr.open("GET", path, false); xhr.send(null); } catch(e) { diff --git a/spec/javascripts/helpers/jasmine-jquery.js b/spec/javascripts/helpers/jasmine-jquery.js deleted file mode 100644 index 5e659195e..000000000 --- a/spec/javascripts/helpers/jasmine-jquery.js +++ /dev/null @@ -1,203 +0,0 @@ -var readFixtures = function() { - return jasmine.getFixtures().proxyCallTo_('read', arguments); -}; - -var loadFixtures = function() { - jasmine.getFixtures().proxyCallTo_('load', arguments); -}; - -var setFixtures = function(html) { - jasmine.getFixtures().set(html); -} - -var sandbox = function(attributes) { - return jasmine.getFixtures().sandbox(attributes); -}; - -jasmine.getFixtures = function() { - return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures(); -}; - -jasmine.Fixtures = function() { - this.containerId = 'jasmine-fixtures'; - this.fixturesCache_ = {}; -}; - -jasmine.Fixtures.prototype.set = function(html) { - this.cleanUp(); - this.createContainer_(html); -}; - -jasmine.Fixtures.prototype.load = function() { - this.cleanUp(); - this.createContainer_(this.read.apply(this, arguments)); -}; - -jasmine.Fixtures.prototype.read = function() { - var htmlChunks = []; - - var fixtureUrls = arguments; - for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { - htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex])); - } - - return htmlChunks.join(''); -}; - -jasmine.Fixtures.prototype.clearCache = function() { - this.fixturesCache_ = {}; -}; - -jasmine.Fixtures.prototype.cleanUp = function() { - $('#' + this.containerId).remove(); -}; - -jasmine.Fixtures.prototype.sandbox = function(attributes) { - var attributesToSet = attributes || {}; - return $('
').attr(attributesToSet); -}; - -jasmine.Fixtures.prototype.createContainer_ = function(html) { - var container = $(''); - container.html(html); - $('body').append(container); -}; - -jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) { - if (typeof this.fixturesCache_[url] == 'undefined') { - this.loadFixtureIntoCache_(url); - } - return this.fixturesCache_[url]; -}; - -jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(url) { - var self = this; - $.ajax({ - async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded - cache: false, - dataType: 'html', - url: url, - success: function(data) { - self.fixturesCache_[url] = data; - } - }); -}; - -jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { - return this[methodName].apply(this, passedArguments); -}; - - -jasmine.JQuery = function() {}; - -jasmine.JQuery.browserTagCaseIndependentHtml = function(html) { - return $('').append(html).html(); -}; - -jasmine.JQuery.elementToString = function(element) { - return $('').append(element.clone()).html(); -}; - -jasmine.JQuery.matchersClass = {}; - - -(function(){ - var jQueryMatchers = { - toHaveClass: function(className) { - return this.actual.hasClass(className); - }, - - toBeVisible: function() { - return this.actual.is(':visible'); - }, - - toBeHidden: function() { - return this.actual.is(':hidden'); - }, - - toBeSelected: function() { - return this.actual.is(':selected'); - }, - - toBeChecked: function() { - return this.actual.is(':checked'); - }, - - toBeEmpty: function() { - return this.actual.is(':empty'); - }, - - toExist: function() { - return this.actual.size() > 0; - }, - - toHaveAttr: function(attributeName, expectedAttributeValue) { - return hasProperty(this.actual.attr(attributeName), expectedAttributeValue); - }, - - toHaveId: function(id) { - return this.actual.attr('id') == id; - }, - - toHaveHtml: function(html) { - return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html); - }, - - toHaveText: function(text) { - return this.actual.text() == text; - }, - - toHaveValue: function(value) { - return this.actual.val() == value; - }, - - toHaveData: function(key, expectedValue) { - return hasProperty(this.actual.data(key), expectedValue); - }, - - toBe: function(selector) { - return this.actual.is(selector); - }, - - toContain: function(selector) { - return this.actual.find(selector).size() > 0; - } - }; - - var hasProperty = function(actualValue, expectedValue) { - if (expectedValue === undefined) { - return actualValue !== undefined; - } - return actualValue == expectedValue; - }; - - var bindMatcher = function(methodName) { - var builtInMatcher = jasmine.Matchers.prototype[methodName]; - - jasmine.JQuery.matchersClass[methodName] = function() { - if (this.actual instanceof jQuery) { - var result = jQueryMatchers[methodName].apply(this, arguments); - this.actual = jasmine.JQuery.elementToString(this.actual); - return result; - } - - if (builtInMatcher) { - return builtInMatcher.apply(this, arguments); - } - - return false; - }; - }; - - for(var methodName in jQueryMatchers) { - bindMatcher(methodName); - } -})(); - -beforeEach(function() { - this.addMatchers(jasmine.JQuery.matchersClass); -}); - -afterEach(function() { - jasmine.getFixtures().cleanUp(); -}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 88ff13ea4..6b6ff197a 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -12,6 +12,7 @@ src_files: # Precompile all scripts together for the test environment - assets/jasmine-load-all.js + - assets/jasmine-jquery.js # stylesheets #