codecombat/test/app/models/CocoModel.spec.coffee
2014-07-04 19:27:37 +02:00

120 lines
4 KiB
CoffeeScript

CocoModel = require 'models/CocoModel'
class BlandClass extends CocoModel
@className: 'Bland'
@schema: {
type: 'object'
additionalProperties: false
properties:
number: {type: 'number'}
object: {type: 'object'}
string: {type: 'string'}
_id: {type: 'string'}
}
urlRoot: '/db/bland'
describe 'CocoModel', ->
describe 'save', ->
it 'saves to db/<urlRoot>', ->
b = new BlandClass({})
res = b.save()
request = jasmine.Ajax.requests.mostRecent()
expect(res).toBeDefined()
expect(request.url).toBe(b.urlRoot)
expect(request.method).toBe('POST')
it 'does not save if the data is invalid based on the schema', ->
b = new BlandClass({number: 'NaN'})
res = b.save()
expect(res).toBe(false)
request = jasmine.Ajax.requests.mostRecent()
expect(request).toBeUndefined()
it 'uses PUT when _id is included', ->
b = new BlandClass({_id: 'test'})
b.save()
request = jasmine.Ajax.requests.mostRecent()
expect(request.method).toBe('PUT')
describe 'patch', ->
it 'PATCHes only properties that have changed', ->
b = new BlandClass({_id: 'test', number:1})
b.loaded = true
b.set('string', 'string')
b.patch()
request = jasmine.Ajax.requests.mostRecent()
params = JSON.parse request.params
expect(params.string).toBeDefined()
expect(params.number).toBeUndefined()
it 'collates all changes made over several sets', ->
b = new BlandClass({_id: 'test', number:1})
b.loaded = true
b.set('string', 'string')
b.set('object', {4:5})
b.patch()
request = jasmine.Ajax.requests.mostRecent()
params = JSON.parse request.params
expect(params.string).toBeDefined()
expect(params.object).toBeDefined()
expect(params.number).toBeUndefined()
it 'does not include data from previous patches', ->
b = new BlandClass({_id: 'test', number:1})
b.loaded = true
b.set('object', {1:2})
b.patch()
request = jasmine.Ajax.requests.mostRecent()
attrs = JSON.stringify(b.attributes) # server responds with all
request.response({status: 200, responseText: attrs})
b.set('number', 3)
b.patch()
request = jasmine.Ajax.requests.mostRecent()
params = JSON.parse request.params
expect(params.object).toBeUndefined()
it 'does nothing when there\'s nothing to patch', ->
b = new BlandClass({_id: 'test', number:1})
b.loaded = true
b.set('number', 1)
b.patch()
request = jasmine.Ajax.requests.mostRecent()
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: {}})
_.delay (->
collection = []
model =
_id: "5390f7637b4d6f2a074a7bb4"
achievement: "537ce4855c91b8d1dda7fda8"
collection.push model
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