Achievement polling now successfully tested

Intermediate
This commit is contained in:
Ruben Vereecken 2014-06-16 14:27:16 +02:00
parent 1fe2c67ffe
commit fe5b675d18
6 changed files with 42 additions and 11 deletions

View file

@ -1,5 +1,5 @@
CocoModel = require './CocoModel'
util = require '../lib/utils'
utils = require '../lib/utils'
module.exports = class Achievement extends CocoModel
@className: 'Achievement'

View file

@ -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
)

View file

@ -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'

View file

@ -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)
)
)

View file

@ -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

View file

@ -0,0 +1,6 @@
CocoModel = require 'models/CocoModel'
RootView = require 'views/kinds/RootView'
module.exports = ->
console.log jasmine.Ajax.requests.mostRecent()
-> console.log 'herp'