2014-06-10 23:43:25 -04:00
|
|
|
CocoModel = require 'models/CocoModel'
|
2014-12-08 17:21:40 -05:00
|
|
|
utils = require 'core/utils'
|
2014-06-10 23:43:25 -04:00
|
|
|
|
|
|
|
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-14 14:12:17 -04:00
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
describe 'CocoModel', ->
|
2014-07-17 19:22:06 -04:00
|
|
|
describe 'setProjection', ->
|
|
|
|
it 'takes an array of properties to project and adds them as a query parameter', ->
|
|
|
|
b = new BlandClass({})
|
|
|
|
b.setProjection ['number', 'object']
|
|
|
|
b.fetch()
|
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
|
|
|
expect(decodeURIComponent(request.url).indexOf('project=number,object')).toBeGreaterThan(-1)
|
2014-08-25 18:39:47 -04:00
|
|
|
|
|
|
|
it 'can update its projection', ->
|
|
|
|
baseURL = '/db/bland/test?filter-creator=Mojambo&project=number,object&ignore-evil=false'
|
|
|
|
unprojectedURL = baseURL.replace /&project=number,object/, ''
|
|
|
|
b = new BlandClass({})
|
|
|
|
b.setURL baseURL
|
|
|
|
expect(b.getURL()).toBe baseURL
|
|
|
|
b.setProjection ['number', 'object']
|
|
|
|
expect(b.getURL()).toBe baseURL
|
|
|
|
b.setProjection ['number']
|
|
|
|
expect(b.getURL()).toBe baseURL.replace /,object/, ''
|
|
|
|
b.setProjection []
|
|
|
|
expect(b.getURL()).toBe unprojectedURL
|
|
|
|
b.setProjection null
|
|
|
|
expect(b.getURL()).toBe unprojectedURL
|
|
|
|
b.setProjection ['object', 'number']
|
|
|
|
expect(b.getURL()).toBe unprojectedURL + '&project=object,number'
|
|
|
|
|
2014-06-10 23:43:25 -04:00
|
|
|
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
|
2014-12-08 17:21:40 -05:00
|
|
|
request.respondWith({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()
|
2014-06-14 14:12:17 -04:00
|
|
|
|
2014-08-13 16:45:24 -04:00
|
|
|
xdescribe 'Achievement polling', ->
|
|
|
|
# TODO: Figure out how to do debounce in tests so that this test doesn't need to use keepDoingUntil
|
2014-06-14 14:12:17 -04:00
|
|
|
|
|
|
|
it 'achievements are polled upon saving a model', (done) ->
|
|
|
|
#spyOn(CocoModel, 'pollAchievements')
|
2014-06-16 08:27:16 -04:00
|
|
|
Backbone.Mediator.subscribe 'achievements:new', (collection) ->
|
|
|
|
Backbone.Mediator.unsubscribe 'achievements:new'
|
|
|
|
expect(collection.constructor.name).toBe('NewAchievementCollection')
|
|
|
|
done()
|
2014-06-14 14:12:17 -04:00
|
|
|
|
|
|
|
b = new BlandClass({})
|
|
|
|
res = b.save()
|
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
2014-12-08 17:21:40 -05:00
|
|
|
request.respondWith(status: 200, responseText: '{}')
|
2014-06-14 14:12:17 -04:00
|
|
|
|
2014-06-17 15:42:27 -04:00
|
|
|
collection = []
|
|
|
|
model =
|
|
|
|
_id: "5390f7637b4d6f2a074a7bb4"
|
|
|
|
achievement: "537ce4855c91b8d1dda7fda8"
|
|
|
|
collection.push model
|
2014-06-14 14:12:17 -04:00
|
|
|
|
2014-06-17 15:42:27 -04:00
|
|
|
utils.keepDoingUntil (ready) ->
|
2014-06-16 08:27:16 -04:00
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
|
|
|
achievementURLMatch = (/.*achievements\?notified=false$/).exec request.url
|
2014-06-17 15:42:27 -04:00
|
|
|
if achievementURLMatch
|
|
|
|
ready true
|
|
|
|
else return ready false
|
|
|
|
|
2014-12-08 17:21:40 -05:00
|
|
|
request.respondWith {status: 200, responseText: JSON.stringify collection}
|
2014-06-14 14:12:17 -04:00
|
|
|
|
2014-06-17 15:42:27 -04:00
|
|
|
utils.keepDoingUntil (ready) ->
|
2014-06-16 08:27:16 -04:00
|
|
|
request = jasmine.Ajax.requests.mostRecent()
|
|
|
|
userURLMatch = (/^\/db\/user\/[a-zA-Z0-9]*$/).exec request.url
|
2014-06-17 15:42:27 -04:00
|
|
|
if userURLMatch
|
|
|
|
ready true
|
|
|
|
else return ready false
|
|
|
|
|
2014-12-08 17:21:40 -05:00
|
|
|
request.respondWith {status:200, responseText: JSON.stringify me}
|
2014-10-17 10:11:26 -04:00
|
|
|
|
|
|
|
describe 'updateI18NCoverage', ->
|
|
|
|
class FlexibleClass extends CocoModel
|
|
|
|
@className: 'Flexible'
|
2016-08-30 17:17:40 -04:00
|
|
|
@schema: {
|
|
|
|
type: 'object'
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string' }
|
|
|
|
description: { type: 'string' }
|
|
|
|
innerObject: {
|
|
|
|
type: 'object'
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string' }
|
|
|
|
i18n: { type: 'object', format: 'i18n', props: ['name']}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i18n: { type: 'object', format: 'i18n', props: ['description', 'name', 'prop1']}
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 10:11:26 -04:00
|
|
|
|
|
|
|
it 'only includes languages for which all objects include a translation', ->
|
|
|
|
m = new FlexibleClass({
|
2016-08-30 17:17:40 -04:00
|
|
|
i18n: { es: { name: '+', description: '+' }, fr: { name: '+', description: '+' } }
|
|
|
|
name: 'Name'
|
|
|
|
description: 'Description'
|
2014-10-17 10:11:26 -04:00
|
|
|
innerObject: {
|
2016-08-30 17:17:40 -04:00
|
|
|
i18n: { es: { name: '+' }, de: { name: '+' }, fr: {} }
|
|
|
|
name: 'Name'
|
2014-10-17 10:11:26 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
m.updateI18NCoverage()
|
2016-08-30 17:17:40 -04:00
|
|
|
expect(_.isEqual(m.get('i18nCoverage'), ['es'])).toBe(true)
|
|
|
|
|
|
|
|
it 'ignores objects for which there is nothing to translate', ->
|
|
|
|
m = new FlexibleClass()
|
|
|
|
m.set({
|
|
|
|
name: 'Name'
|
|
|
|
i18n: {
|
|
|
|
'-': {'-':'-'}
|
|
|
|
'es': {name: 'Name in Spanish'}
|
|
|
|
}
|
|
|
|
innerObject: {
|
|
|
|
i18n: { '-': {'-':'-'} }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
m.updateI18NCoverage()
|
|
|
|
expect(_.isEqual(m.get('i18nCoverage'), ['es'])).toBe(true)
|
|
|
|
|
|
|
|
|