Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 4c7948c7 rédigé par Steffen van Bergerem's avatar Steffen van Bergerem
Parcourir les fichiers

Use id in stream comparator as fallback

parent 757a5fbd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -13,8 +13,16 @@ app.models.Stream = Backbone.Collection.extend({ ...@@ -13,8 +13,16 @@ app.models.Stream = Backbone.Collection.extend({
}, },
collectionOptions :function(){ collectionOptions :function(){
var order = this.sortOrder(); var order = this.sortOrder();
return { comparator : function(item) { return -item[order](); } }; return {
comparator: function(item1, item2) {
if (item1[order]() < item2[order]()) { return 1; }
if (item1[order]() > item2[order]()) { return -1; }
if (item1.id < item2.id) { return 1; }
if (item1.id > item2.id) { return -1; }
return 0;
}
};
}, },
url : function(){ url : function(){
......
...@@ -7,6 +7,38 @@ describe("app.models.Stream", function() { ...@@ -7,6 +7,38 @@ describe("app.models.Stream", function() {
expectedPath = document.location.pathname; expectedPath = document.location.pathname;
}); });
describe("collectionOptions", function() {
beforeEach(function() {
this.post1 = new app.models.Post({"id": 1, "created_at": 12, "interacted_at": 123});
this.post2 = new app.models.Post({"id": 2, "created_at": 13, "interacted_at": 123});
this.post3 = new app.models.Post({"id": 3, "created_at": 13, "interacted_at": 122});
this.post4 = new app.models.Post({"id": 4, "created_at": 10, "interacted_at": 100});
});
it("returns a comparator for posts that compares created_at and ids by default", function() {
this.options = stream.collectionOptions();
expect(this.options.comparator(this.post1, this.post2)).toBe(1);
expect(this.options.comparator(this.post2, this.post1)).toBe(-1);
expect(this.options.comparator(this.post2, this.post3)).toBe(1);
expect(this.options.comparator(this.post3, this.post2)).toBe(-1);
expect(this.options.comparator(this.post1, this.post4)).toBe(-1);
expect(this.options.comparator(this.post4, this.post1)).toBe(1);
expect(this.options.comparator(this.post1, this.post1)).toBe(0);
});
it("returns a comparator for posts that compares interacted_at and ids for the activity stream", function() {
spyOn(stream, "basePath").and.returnValue("activity");
this.options = stream.collectionOptions();
expect(this.options.comparator(this.post1, this.post2)).toBe(1);
expect(this.options.comparator(this.post2, this.post1)).toBe(-1);
expect(this.options.comparator(this.post2, this.post3)).toBe(-1);
expect(this.options.comparator(this.post3, this.post2)).toBe(1);
expect(this.options.comparator(this.post1, this.post4)).toBe(-1);
expect(this.options.comparator(this.post4, this.post1)).toBe(1);
expect(this.options.comparator(this.post1, this.post1)).toBe(0);
});
});
describe("#_fetchOpts", function() { 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() {
expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath}); expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath});
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter