mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-20 15:26:06 +08:00
fix(model): add specs around serializing bad API data
This commit is contained in:
parent
2e9ab2cc23
commit
c811cc5ce6
|
@ -84,6 +84,8 @@ describe "Model", ->
|
|||
|
||||
describe "fromJSON", ->
|
||||
beforeEach ->
|
||||
class SubmodelItem extends Model
|
||||
|
||||
class Submodel extends Model
|
||||
@attributes: _.extend {}, Model.attributes,
|
||||
'testNumber': Attributes.Number
|
||||
|
@ -92,6 +94,10 @@ describe "Model", ->
|
|||
'testBoolean': Attributes.Boolean
|
||||
modelKey: 'testBoolean'
|
||||
jsonKey: 'test_boolean'
|
||||
'testCollection': Attributes.Collection
|
||||
modelKey: 'testCollection'
|
||||
jsonKey: 'test_collection'
|
||||
itemClass: SubmodelItem
|
||||
|
||||
@json =
|
||||
'id': '1234'
|
||||
|
@ -134,6 +140,20 @@ describe "Model", ->
|
|||
@m.fromJSON('test_number': 0)
|
||||
expect(@m.testNumber).toBe(0)
|
||||
|
||||
describe "Attributes.Collection", ->
|
||||
it "should parse and inflate items", ->
|
||||
@m.fromJSON('test_collection': [{id: '123'}])
|
||||
expect(@m.testCollection.length).toBe(1)
|
||||
expect(@m.testCollection[0].id).toBe('123')
|
||||
expect(@m.testCollection[0].constructor.name).toBe('SubmodelItem')
|
||||
|
||||
it "should be fine with malformed arrays", ->
|
||||
@m.fromJSON('test_collection': [null])
|
||||
expect(@m.testCollection.length).toBe(0)
|
||||
@m.fromJSON('test_collection': [])
|
||||
expect(@m.testCollection.length).toBe(0)
|
||||
@m.fromJSON('test_collection': null)
|
||||
expect(@m.testCollection.length).toBe(0)
|
||||
|
||||
describe "Attributes.Boolean", ->
|
||||
it "should read `true` or true and coerce everything else to false", ->
|
||||
|
|
|
@ -56,12 +56,13 @@ class AttributeCollection extends Attribute
|
|||
return [] unless json && json instanceof Array
|
||||
objs = []
|
||||
|
||||
# Note: It's possible for a malformed API request to return an array
|
||||
# of null values. N1 is tolerant to this type of error, but shouldn't
|
||||
# happen on the API end.
|
||||
json = _.compact(json)
|
||||
|
||||
for objJSON in json
|
||||
# Note: It's possible for a malformed API request to return an array
|
||||
# of null values. N1 is tolerant to this type of error, but shouldn't
|
||||
# happen on the API end.
|
||||
if not objJSON
|
||||
continue
|
||||
|
||||
if @itemClass.prototype.fromJSON?
|
||||
obj = new @itemClass
|
||||
# Important: if no ids are in the JSON, don't make them up
|
||||
|
|
Loading…
Reference in a new issue