diff --git a/app/assets/javascripts/app/collections/notifications.js b/app/assets/javascripts/app/collections/notifications.js index 0bb4a2f97..151410fe0 100644 --- a/app/assets/javascripts/app/collections/notifications.js +++ b/app/assets/javascripts/app/collections/notifications.js @@ -11,12 +11,8 @@ app.collections.Notifications = Backbone.Collection.extend({ timeout: 300000, // 5 minutes initialize: function() { - this.pollNotifications(); - - setTimeout(function() { - setInterval(this.pollNotifications.bind(this), this.timeout); - }.bind(this), this.timeout); - + this.fetch(); + setInterval(this.pollNotifications.bind(this), this.timeout); Diaspora.BrowserNotification.requestPermission(); }, diff --git a/spec/javascripts/app/collections/notifications_collection_spec.js b/spec/javascripts/app/collections/notifications_collection_spec.js index d5f8cd522..c6c8eeb7a 100644 --- a/spec/javascripts/app/collections/notifications_collection_spec.js +++ b/spec/javascripts/app/collections/notifications_collection_spec.js @@ -1,9 +1,9 @@ describe("app.collections.Notifications", function() { describe("initialize", function() { - it("calls pollNotifications", function() { - spyOn(app.collections.Notifications.prototype, "pollNotifications"); + it("calls fetch", function() { + spyOn(app.collections.Notifications.prototype, "fetch"); new app.collections.Notifications(); - expect(app.collections.Notifications.prototype.pollNotifications).toHaveBeenCalled(); + expect(app.collections.Notifications.prototype.fetch).toHaveBeenCalled(); }); it("calls Diaspora.BrowserNotification.requestPermission", function() { @@ -23,6 +23,16 @@ describe("app.collections.Notifications", function() { expect(target.unreadCount).toBe(0); expect(target.unreadCountByType).toEqual({}); }); + + it("repeatedly calls pollNotifications", function() { + spyOn(app.collections.Notifications.prototype, "pollNotifications").and.callThrough(); + var collection = new app.collections.Notifications(); + expect(app.collections.Notifications.prototype.pollNotifications).not.toHaveBeenCalled(); + jasmine.clock().tick(collection.timeout); + expect(app.collections.Notifications.prototype.pollNotifications).toHaveBeenCalledTimes(1); + jasmine.clock().tick(collection.timeout); + expect(app.collections.Notifications.prototype.pollNotifications).toHaveBeenCalledTimes(2); + }); }); describe("pollNotifications", function() { @@ -31,9 +41,9 @@ describe("app.collections.Notifications", function() { }); it("calls fetch", function() { - spyOn(app.collections.Notifications.prototype, "fetch"); + spyOn(this.target, "fetch"); this.target.pollNotifications(); - expect(app.collections.Notifications.prototype.fetch).toHaveBeenCalled(); + expect(this.target.fetch).toHaveBeenCalled(); }); it("doesn't call Diaspora.BrowserNotification.spawnNotification when there are no new notifications", function() { @@ -52,14 +62,6 @@ describe("app.collections.Notifications", function() { this.target.trigger("finishedLoading"); expect(Diaspora.BrowserNotification.spawnNotification).toHaveBeenCalled(); }); - - it("refreshes after timeout", function() { - spyOn(app.collections.Notifications.prototype, "pollNotifications").and.callThrough(); - this.target.pollNotifications(); - expect(app.collections.Notifications.prototype.pollNotifications).toHaveBeenCalledTimes(1); - jasmine.clock().tick(2 * this.target.timeout); - expect(app.collections.Notifications.prototype.pollNotifications).toHaveBeenCalledTimes(2); - }); }); describe("fetch", function() {