port some more JS specs to jasmine 2.0... still a lot to do

This commit is contained in:
Florian Staudacher 2014-03-01 13:04:31 +01:00 committed by Jonne Haß
parent 43f156420d
commit c81379d38f
6 changed files with 79 additions and 78 deletions

View file

@ -19,13 +19,12 @@ app.models.Stream = Backbone.Collection.extend({
var defaultOpts = { var defaultOpts = {
remove: false // tell backbone to keep existing items in the collection remove: false // tell backbone to keep existing items in the collection
}; };
return _.extend({}, defaultOpts, opts); return _.extend({ url: this.url() }, defaultOpts, opts);
}, },
fetch: function() { fetch: function() {
if( this.isFetching() ) return false; if( this.isFetching() ) return false;
var url = this.url(); this.deferred = this.items.fetch( this._fetchOpts() )
this.deferred = this.items.fetch(this._fetchOpts({url : url}))
.done(_.bind(this.triggerFetchedEvents, this)); .done(_.bind(this.triggerFetchedEvents, this));
}, },

View file

@ -1,13 +1,17 @@
describe("app.collections.Aspects", function(){ describe("app.collections.Aspects", function(){
beforeEach(function(){ beforeEach(function(){
Diaspora.I18n.load({ var locale = {
'and' : "and", and: 'and',
'comma' : ",", comma: ',',
'my_aspects' : "My Aspects" my_aspects: 'My Aspects'
}); };
var my_aspects = [{ name: 'Work', selected: true }, var my_aspects = [
{ name: 'Friends', selected: false }, { name: 'Work', selected: true },
{ name: 'Acquaintances', selected: false }] { name: 'Friends', selected: false },
{ name: 'Acquaintances', selected: false }
];
Diaspora.I18n.load(locale);
this.aspects = new app.collections.Aspects(my_aspects); this.aspects = new app.collections.Aspects(my_aspects);
}); });
@ -44,25 +48,21 @@ describe("app.collections.Aspects", function(){
describe("#toSentence", function(){ describe("#toSentence", function(){
describe('without aspects', function(){ describe('without aspects', function(){
beforeEach(function(){ beforeEach(function(){
this.aspects = new app.collections.Aspects({ name: 'Work', selected: false }) this.aspects = new app.collections.Aspects([{ name: 'Work', selected: false }]);
spyOn(this.aspects, 'selectedAspects').andCallThrough();
}); });
it("returns the name of the aspect", function(){ it("returns the name of the aspect", function(){
expect(this.aspects.toSentence()).toEqual('My Aspects'); expect(this.aspects.toSentence()).toEqual('My Aspects');
expect(this.aspects.selectedAspects).toHaveBeenCalled();
}); });
}); });
describe("with one aspect", function(){ describe("with one aspect", function(){
beforeEach(function(){ beforeEach(function(){
this.aspects = new app.collections.Aspects({ name: 'Work', selected: true }) this.aspects = new app.collections.Aspects([{ name: 'Work', selected: true }]);
spyOn(this.aspects, 'selectedAspects').andCallThrough();
}); });
it("returns the name of the aspect", function(){ it("returns the name of the aspect", function(){
expect(this.aspects.toSentence()).toEqual('Work'); expect(this.aspects.toSentence()).toEqual('Work');
expect(this.aspects.selectedAspects).toHaveBeenCalled();
}); });
}); });

View file

@ -1,45 +1,43 @@
describe("app.models.Post.Interactions", function(){ describe("app.models.Post.Interactions", function(){
beforeEach(function(){ beforeEach(function(){
this.interactions = factory.post() this.interactions = factory.post().interactions;
this.interactions = this.interactions.interactions
this.author = factory.author({guid: "loggedInAsARockstar"}) this.author = factory.author({guid: "loggedInAsARockstar"})
loginAs({guid: "loggedInAsARockstar"}) loginAs({guid: "loggedInAsARockstar"})
this.userLike = new app.models.Like({author : this.author}) this.userLike = new app.models.Like({author : this.author})
}) });
describe("toggleLike", function(){ describe("toggleLike", function(){
it("calls unliked when the user_like exists", function(){ it("calls unliked when the user_like exists", function(){
spyOn(this.interactions, "unlike").and.returnValue(true);
this.interactions.likes.add(this.userLike) this.interactions.likes.add(this.userLike)
spyOn(this.interactions, "unlike").andReturn(true);
this.interactions.toggleLike(); this.interactions.toggleLike();
expect(this.interactions.unlike).toHaveBeenCalled(); expect(this.interactions.unlike).toHaveBeenCalled();
}) });
it("calls liked when the user_like does not exist", function(){ it("calls liked when the user_like does not exist", function(){
spyOn(this.interactions, "like").and.returnValue(true);
this.interactions.likes.reset([]); this.interactions.likes.reset([]);
spyOn(this.interactions, "like").andReturn(true);
this.interactions.toggleLike(); this.interactions.toggleLike();
expect(this.interactions.like).toHaveBeenCalled(); expect(this.interactions.like).toHaveBeenCalled();
}) });
}) });
describe("like", function(){ describe("like", function(){
it("calls create on the likes collection", function(){ it("calls create on the likes collection", function(){
spyOn(this.interactions.likes, "create");
this.interactions.like(); this.interactions.like();
expect(this.interactions.likes.create).toHaveBeenCalled(); expect(this.interactions.likes.length).toEqual(1);
}) });
}) });
describe("unlike", function(){ describe("unlike", function(){
it("calls destroy on the likes collection", function(){ it("calls destroy on the likes collection", function(){
this.interactions.likes.add(this.userLike) this.interactions.likes.add(this.userLike)
spyOn(this.userLike, "destroy");
this.interactions.unlike(); this.interactions.unlike();
expect(this.userLike.destroy).toHaveBeenCalled();
}) expect(this.interactions.likes.length).toEqual(0);
}) });
}) });
});

View file

@ -6,7 +6,7 @@ describe("app.models.StreamAspects", function() {
beforeEach(function(){ beforeEach(function(){
fetch = new $.Deferred(); fetch = new $.Deferred();
stream = new app.models.StreamAspects([], {aspects_ids: [1,2]}); 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); stream.items.set([{name: 'a'}, {name: 'b'}, {name: 'c'}], options);
fetch.resolve(); fetch.resolve();
return fetch; return fetch;

View file

@ -1,55 +1,59 @@
describe("app.models.Stream", function() { describe("app.models.Stream", function() {
var stream,
expectedPath;
beforeEach(function(){ beforeEach(function(){
this.stream = new app.models.Stream(), stream = new app.models.Stream();
this.expectedPath = document.location.pathname; expectedPath = document.location.pathname;
}) });
describe(".fetch", function() {
var postFetch
beforeEach(function(){
postFetch = new $.Deferred()
spyOn(this.stream.items, "fetch").andCallFake(function(){
return postFetch
})
})
describe("#_fetchOpts", function() {
it("it fetches posts from the window's url, and ads them to the collection", function() { it("it fetches posts from the window's url, and ads them to the collection", function() {
this.stream.fetch() expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath});
expect(this.stream.items.fetch).toHaveBeenCalledWith({ remove: false, url: this.expectedPath});
}); });
it("returns the json path with max_time if the collection has models", function() { it("returns the json path with max_time if the collection has models", function() {
var post = new app.models.Post(); var post = new app.models.Post({created_at: 1234000});
spyOn(post, "createdAt").andReturn(1234); stream.add(post);
this.stream.add(post);
this.stream.fetch() expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath + "?max_time=1234"});
expect(this.stream.items.fetch).toHaveBeenCalledWith({ remove: false, url: this.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(){ it("triggers fetched on the stream when it is fetched", function(){
var fetchedSpy = jasmine.createSpy() stream.bind('fetched', fetchedSpy);
this.stream.bind('fetched', fetchedSpy) stream.fetch();
this.stream.fetch() postFetch.resolve([1,2,3]);
postFetch.resolve([1,2,3])
expect(fetchedSpy).toHaveBeenCalled() expect(fetchedSpy).toHaveBeenCalled();
}) });
it("triggers allItemsLoaded on the stream when zero posts are returned", function(){ it("triggers allItemsLoaded on the stream when zero posts are returned", function(){
var fetchedSpy = jasmine.createSpy() stream.bind('allItemsLoaded', fetchedSpy);
this.stream.bind('allItemsLoaded', fetchedSpy) stream.fetch();
this.stream.fetch() postFetch.resolve([]);
postFetch.resolve([])
expect(fetchedSpy).toHaveBeenCalled() expect(fetchedSpy).toHaveBeenCalled();
}) });
it("triggers allItemsLoaded on the stream when a Post is returned", function(){ it("triggers allItemsLoaded on the stream when a Post is returned", function(){
var fetchedSpy = jasmine.createSpy() stream.bind('allItemsLoaded', fetchedSpy);
this.stream.bind('allItemsLoaded', fetchedSpy) stream.fetch();
this.stream.fetch() postFetch.resolve(factory.post().attributes);
postFetch.resolve(factory.post().attributes)
expect(fetchedSpy).toHaveBeenCalled() expect(fetchedSpy).toHaveBeenCalled();
}) });
}); });
}); });

View file

@ -29,7 +29,7 @@ factory = {
"id" : this.id.next(), "id" : this.id.next(),
"text" : "This is a comment!" "text" : "This is a comment!"
} }
return new app.models.Comment(_.extend(defaultAttrs, overrides)) return new app.models.Comment(_.extend(defaultAttrs, overrides))
}, },
@ -162,4 +162,4 @@ factory = {
} }
} }
factory.author = factory.userAttrs factory.author = factory.userAttrs;