From c81379d38f3f066f508abeb545e05dd98ce6308f Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Sat, 1 Mar 2014 13:04:31 +0100 Subject: [PATCH] port some more JS specs to jasmine 2.0... still a lot to do --- app/assets/javascripts/app/models/stream.js | 5 +- .../app/collections/aspects_spec.js | 28 +++---- .../app/models/post/interacations_spec.js | 38 +++++---- .../app/models/stream_aspects_spec.js | 2 +- spec/javascripts/app/models/stream_spec.js | 80 ++++++++++--------- spec/javascripts/helpers/factory.js | 4 +- 6 files changed, 79 insertions(+), 78 deletions(-) diff --git a/app/assets/javascripts/app/models/stream.js b/app/assets/javascripts/app/models/stream.js index 4cf5ff737..d38806f51 100644 --- a/app/assets/javascripts/app/models/stream.js +++ b/app/assets/javascripts/app/models/stream.js @@ -19,13 +19,12 @@ app.models.Stream = Backbone.Collection.extend({ var defaultOpts = { remove: false // tell backbone to keep existing items in the collection }; - return _.extend({}, defaultOpts, opts); + return _.extend({ url: this.url() }, defaultOpts, opts); }, fetch: function() { if( this.isFetching() ) return false; - var url = this.url(); - this.deferred = this.items.fetch(this._fetchOpts({url : url})) + this.deferred = this.items.fetch( this._fetchOpts() ) .done(_.bind(this.triggerFetchedEvents, this)); }, diff --git a/spec/javascripts/app/collections/aspects_spec.js b/spec/javascripts/app/collections/aspects_spec.js index 280dd7592..ab89a4206 100644 --- a/spec/javascripts/app/collections/aspects_spec.js +++ b/spec/javascripts/app/collections/aspects_spec.js @@ -1,13 +1,17 @@ describe("app.collections.Aspects", function(){ beforeEach(function(){ - Diaspora.I18n.load({ - 'and' : "and", - 'comma' : ",", - 'my_aspects' : "My Aspects" - }); - var my_aspects = [{ name: 'Work', selected: true }, - { name: 'Friends', selected: false }, - { name: 'Acquaintances', selected: false }] + var locale = { + and: 'and', + comma: ',', + my_aspects: 'My Aspects' + }; + var my_aspects = [ + { name: 'Work', selected: true }, + { name: 'Friends', selected: false }, + { name: 'Acquaintances', selected: false } + ]; + + Diaspora.I18n.load(locale); this.aspects = new app.collections.Aspects(my_aspects); }); @@ -44,25 +48,21 @@ describe("app.collections.Aspects", function(){ describe("#toSentence", function(){ describe('without aspects', function(){ beforeEach(function(){ - this.aspects = new app.collections.Aspects({ name: 'Work', selected: false }) - spyOn(this.aspects, 'selectedAspects').andCallThrough(); + this.aspects = new app.collections.Aspects([{ name: 'Work', selected: false }]); }); it("returns the name of the aspect", function(){ expect(this.aspects.toSentence()).toEqual('My Aspects'); - expect(this.aspects.selectedAspects).toHaveBeenCalled(); }); }); describe("with one aspect", function(){ beforeEach(function(){ - this.aspects = new app.collections.Aspects({ name: 'Work', selected: true }) - spyOn(this.aspects, 'selectedAspects').andCallThrough(); + this.aspects = new app.collections.Aspects([{ name: 'Work', selected: true }]); }); it("returns the name of the aspect", function(){ expect(this.aspects.toSentence()).toEqual('Work'); - expect(this.aspects.selectedAspects).toHaveBeenCalled(); }); }); diff --git a/spec/javascripts/app/models/post/interacations_spec.js b/spec/javascripts/app/models/post/interacations_spec.js index f6a5be3ad..a4e601727 100644 --- a/spec/javascripts/app/models/post/interacations_spec.js +++ b/spec/javascripts/app/models/post/interacations_spec.js @@ -1,45 +1,43 @@ describe("app.models.Post.Interactions", function(){ beforeEach(function(){ - this.interactions = factory.post() - this.interactions = this.interactions.interactions + this.interactions = factory.post().interactions; this.author = factory.author({guid: "loggedInAsARockstar"}) loginAs({guid: "loggedInAsARockstar"}) this.userLike = new app.models.Like({author : this.author}) - }) - + }); + describe("toggleLike", function(){ it("calls unliked when the user_like exists", function(){ + spyOn(this.interactions, "unlike").and.returnValue(true); this.interactions.likes.add(this.userLike) - spyOn(this.interactions, "unlike").andReturn(true); this.interactions.toggleLike(); + expect(this.interactions.unlike).toHaveBeenCalled(); - }) + }); it("calls liked when the user_like does not exist", function(){ + spyOn(this.interactions, "like").and.returnValue(true); this.interactions.likes.reset([]); - spyOn(this.interactions, "like").andReturn(true); this.interactions.toggleLike(); + expect(this.interactions.like).toHaveBeenCalled(); - }) - }) + }); + }); describe("like", function(){ it("calls create on the likes collection", function(){ - spyOn(this.interactions.likes, "create"); - this.interactions.like(); - expect(this.interactions.likes.create).toHaveBeenCalled(); - }) - }) + expect(this.interactions.likes.length).toEqual(1); + }); + }); describe("unlike", function(){ it("calls destroy on the likes collection", function(){ this.interactions.likes.add(this.userLike) - spyOn(this.userLike, "destroy"); - this.interactions.unlike(); - expect(this.userLike.destroy).toHaveBeenCalled(); - }) - }) -}) \ No newline at end of file + + expect(this.interactions.likes.length).toEqual(0); + }); + }); +}); diff --git a/spec/javascripts/app/models/stream_aspects_spec.js b/spec/javascripts/app/models/stream_aspects_spec.js index f6d99195c..35ba94a4e 100644 --- a/spec/javascripts/app/models/stream_aspects_spec.js +++ b/spec/javascripts/app/models/stream_aspects_spec.js @@ -6,7 +6,7 @@ describe("app.models.StreamAspects", function() { beforeEach(function(){ fetch = new $.Deferred(); stream = new app.models.StreamAspects([], {aspects_ids: [1,2]}); - spyOn(stream.items, "fetch").andCallFake(function(options){ + spyOn(stream.items, "fetch").and.callFake(function(options){ stream.items.set([{name: 'a'}, {name: 'b'}, {name: 'c'}], options); fetch.resolve(); return fetch; diff --git a/spec/javascripts/app/models/stream_spec.js b/spec/javascripts/app/models/stream_spec.js index 2b0094234..0778dbf1f 100644 --- a/spec/javascripts/app/models/stream_spec.js +++ b/spec/javascripts/app/models/stream_spec.js @@ -1,55 +1,59 @@ describe("app.models.Stream", function() { + var stream, + expectedPath; + beforeEach(function(){ - this.stream = new app.models.Stream(), - this.expectedPath = document.location.pathname; - }) - - describe(".fetch", function() { - var postFetch - beforeEach(function(){ - postFetch = new $.Deferred() - - spyOn(this.stream.items, "fetch").andCallFake(function(){ - return postFetch - }) - }) + stream = new app.models.Stream(); + expectedPath = document.location.pathname; + }); + describe("#_fetchOpts", function() { it("it fetches posts from the window's url, and ads them to the collection", function() { - this.stream.fetch() - expect(this.stream.items.fetch).toHaveBeenCalledWith({ remove: false, url: this.expectedPath}); + expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath}); }); it("returns the json path with max_time if the collection has models", function() { - var post = new app.models.Post(); - spyOn(post, "createdAt").andReturn(1234); - this.stream.add(post); + var post = new app.models.Post({created_at: 1234000}); + stream.add(post); - this.stream.fetch() - expect(this.stream.items.fetch).toHaveBeenCalledWith({ remove: false, url: this.expectedPath + "?max_time=1234"}); + expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath + "?max_time=1234"}); + }); + }); + + describe("events", function() { + var postFetch, + fetchedSpy; + + beforeEach(function(){ + postFetch = new $.Deferred(); + fetchedSpy = jasmine.createSpy(); + spyOn(stream.items, "fetch").and.callFake(function(){ + return postFetch; + }); }); it("triggers fetched on the stream when it is fetched", function(){ - var fetchedSpy = jasmine.createSpy() - this.stream.bind('fetched', fetchedSpy) - this.stream.fetch() - postFetch.resolve([1,2,3]) - expect(fetchedSpy).toHaveBeenCalled() - }) + stream.bind('fetched', fetchedSpy); + stream.fetch(); + postFetch.resolve([1,2,3]); + + expect(fetchedSpy).toHaveBeenCalled(); + }); it("triggers allItemsLoaded on the stream when zero posts are returned", function(){ - var fetchedSpy = jasmine.createSpy() - this.stream.bind('allItemsLoaded', fetchedSpy) - this.stream.fetch() - postFetch.resolve([]) - expect(fetchedSpy).toHaveBeenCalled() - }) + stream.bind('allItemsLoaded', fetchedSpy); + stream.fetch(); + postFetch.resolve([]); + + expect(fetchedSpy).toHaveBeenCalled(); + }); it("triggers allItemsLoaded on the stream when a Post is returned", function(){ - var fetchedSpy = jasmine.createSpy() - this.stream.bind('allItemsLoaded', fetchedSpy) - this.stream.fetch() - postFetch.resolve(factory.post().attributes) - expect(fetchedSpy).toHaveBeenCalled() - }) + stream.bind('allItemsLoaded', fetchedSpy); + stream.fetch(); + postFetch.resolve(factory.post().attributes); + + expect(fetchedSpy).toHaveBeenCalled(); + }); }); }); diff --git a/spec/javascripts/helpers/factory.js b/spec/javascripts/helpers/factory.js index b5dd63068..8f6be8b89 100644 --- a/spec/javascripts/helpers/factory.js +++ b/spec/javascripts/helpers/factory.js @@ -29,7 +29,7 @@ factory = { "id" : this.id.next(), "text" : "This is a comment!" } - + return new app.models.Comment(_.extend(defaultAttrs, overrides)) }, @@ -162,4 +162,4 @@ factory = { } } -factory.author = factory.userAttrs +factory.author = factory.userAttrs;