2014-06-10 23:43:25 -04:00
|
|
|
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'
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
describe 'CocoModel', ->
|
|
|
|
describe 'save', ->
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
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')
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
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()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
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', ->
|
2014-06-30 22:16:26 -04:00
|
|
|
b = new BlandClass({_id: 'test', number: 1})
|
2014-06-10 23:43:25 -04:00
|
|
|
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()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
it 'collates all changes made over several sets', ->
|
2014-06-30 22:16:26 -04:00
|
|
|
b = new BlandClass({_id: 'test', number: 1})
|
2014-06-10 23:43:25 -04:00
|
|
|
b.loaded = true
|
|
|
|
b.set('string', 'string')
|
2014-06-30 22:16:26 -04:00
|
|
|
b.set('object', {4: 5})
|
2014-06-10 23:43:25 -04:00
|
|
|
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', ->
|
2014-06-30 22:16:26 -04:00
|
|
|
b = new BlandClass({_id: 'test', number: 1})
|
2014-06-10 23:43:25 -04:00
|
|
|
b.loaded = true
|
2014-06-30 22:16:26 -04:00
|
|
|
b.set('object', {1: 2})
|
2014-06-10 23:43:25 -04:00
|
|
|
b.patch()
|
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
|
|
|
attrs = JSON.stringify(b.attributes) # server responds with all
|
|
|
|
request.response({status: 200, responseText: attrs})
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
b.set('number', 3)
|
|
|
|
b.patch()
|
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
|
|
|
params = JSON.parse request.params
|
|
|
|
expect(params.object).toBeUndefined()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-06-11 16:16:17 -04:00
|
|
|
it 'does nothing when there\'s nothing to patch', ->
|
2014-06-30 22:16:26 -04:00
|
|
|
b = new BlandClass({_id: 'test', number: 1})
|
2014-06-11 16:16:17 -04:00
|
|
|
b.loaded = true
|
|
|
|
b.set('number', 1)
|
|
|
|
b.patch()
|
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
|
|
|
expect(request).toBeUndefined()
|