From fe5b675d18abbb3071e43f8b73ecf8e474e3c3f2 Mon Sep 17 00:00:00 2001 From: Ruben Vereecken Date: Mon, 16 Jun 2014 14:27:16 +0200 Subject: [PATCH] Achievement polling now successfully tested Intermediate --- app/models/Achievement.coffee | 2 +- app/models/CocoModel.coffee | 4 ++- app/models/EarnedAchievement.coffee | 7 +++++ app/views/kinds/RootView.coffee | 4 +-- test/app/models/CocoModel.spec.coffee | 30 ++++++++++++++----- .../achievement/achievement_get.demo.coffee | 6 ++++ 6 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 app/models/EarnedAchievement.coffee create mode 100644 test/demo/views/achievement/achievement_get.demo.coffee diff --git a/app/models/Achievement.coffee b/app/models/Achievement.coffee index b4f0ed99b..094f46ea5 100644 --- a/app/models/Achievement.coffee +++ b/app/models/Achievement.coffee @@ -1,5 +1,5 @@ CocoModel = require './CocoModel' -util = require '../lib/utils' +utils = require '../lib/utils' module.exports = class Achievement extends CocoModel @className: 'Achievement' diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index 245eccd79..27a1af2a7 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -293,10 +293,12 @@ class CocoModel extends Backbone.Model @pollAchievements: -> achievements = new NewAchievementCollection - console.log 'ohai' achievements.fetch( success: (collection) -> me.fetch (success: -> Backbone.Mediator.publish('achievements:new', collection)) unless _.isEmpty(collection.models) + error: (collection, res, options) -> + console.error 'Miserably failed to fetch unnotified achievements' + console.log res ) diff --git a/app/models/EarnedAchievement.coffee b/app/models/EarnedAchievement.coffee new file mode 100644 index 000000000..28588db81 --- /dev/null +++ b/app/models/EarnedAchievement.coffee @@ -0,0 +1,7 @@ +CocoModel = require './CocoModel' +util = require '../lib/utils' + +module.exports = class EarnedAchievement extends CocoModel + @className: 'EarnedAchievement' + @schema: require 'schemas/models/earned_achievement' + urlRoot: '/db/earnedachievement' diff --git a/app/views/kinds/RootView.coffee b/app/views/kinds/RootView.coffee index 899c0aaa7..503656156 100644 --- a/app/views/kinds/RootView.coffee +++ b/app/views/kinds/RootView.coffee @@ -30,7 +30,7 @@ module.exports = class RootView extends CocoView 'achievements:new': 'handleNewAchievements' - showNewAchievement: (achievement, earnedAchievement) -> + @showNewAchievement: (achievement, earnedAchievement) -> currentLevel = me.level() nextLevel = currentLevel + 1 currentLevelExp = User.expForLevel(currentLevel) @@ -83,7 +83,7 @@ module.exports = class RootView extends CocoView achievement = new Achievement(_id: earnedAchievement.get('achievement')) console.log achievement achievement.fetch( - success: (achievement) => @showNewAchievement(achievement, earnedAchievement) + success: (achievement) => RootView.showNewAchievement(achievement, earnedAchievement) ) ) diff --git a/test/app/models/CocoModel.spec.coffee b/test/app/models/CocoModel.spec.coffee index 676336897..b671bedf7 100644 --- a/test/app/models/CocoModel.spec.coffee +++ b/test/app/models/CocoModel.spec.coffee @@ -84,21 +84,37 @@ describe 'CocoModel', -> expect(request).toBeUndefined() describe 'Achievement polling', -> + NewAchievementCollection = require 'collections/NewAchievementCollection' + EarnedAchievement = require 'models/EarnedAchievement' it 'achievements are polled upon saving a model', (done) -> #spyOn(CocoModel, 'pollAchievements') + Backbone.Mediator.subscribe 'achievements:new', (collection) -> + Backbone.Mediator.unsubscribe 'achievements:new' + expect(collection.constructor.name).toBe('NewAchievementCollection') + done() b = new BlandClass({}) res = b.save() request = jasmine.Ajax.requests.mostRecent() request.response({status: 200, responseText: {}}) - jasmine.Ajax.requests.reset() - #expect(CocoModel.pollAchievements).toHaveBeenCalled() - console.log jasmine.Ajax.requests.mostRecent() + _.delay (-> + collection = [] + model = + _id: "5390f7637b4d6f2a074a7bb4" + achievement: "537ce4855c91b8d1dda7fda8" + collection.push model - request = jasmine.Ajax.requests.mostRecent() - #expect(request.url).toBe("") - - done() + request = jasmine.Ajax.requests.mostRecent() + achievementURLMatch = (/.*achievements\?notified=false$/).exec request.url + expect(achievementURLMatch).not.toBeNull() + request.response {status: 200, responseText: JSON.stringify collection} + _.delay (-> + request = jasmine.Ajax.requests.mostRecent() + userURLMatch = (/^\/db\/user\/[a-zA-Z0-9]*$/).exec request.url + expect(userURLMatch).not.toBeNull() + request.response {status:200, responseText: JSON.stringify me} + ), 1000 + ), 1000 diff --git a/test/demo/views/achievement/achievement_get.demo.coffee b/test/demo/views/achievement/achievement_get.demo.coffee new file mode 100644 index 000000000..ff5e9927b --- /dev/null +++ b/test/demo/views/achievement/achievement_get.demo.coffee @@ -0,0 +1,6 @@ +CocoModel = require 'models/CocoModel' +RootView = require 'views/kinds/RootView' + +module.exports = -> + console.log jasmine.Ajax.requests.mostRecent() + -> console.log 'herp'