Commit 207453a3 by David Baumgold

Further basic tests for Backbone models

parent 001cd5d0
beforeEach ->
@addMatchers
toBeInstanceOf: (expected) ->
return @actual instanceof expected
describe "CMS.Models.Textbook", -> describe "CMS.Models.Textbook", ->
beforeEach -> beforeEach ->
@model = new CMS.Models.Textbook() @model = new CMS.Models.Textbook()
...@@ -9,4 +15,55 @@ describe "CMS.Models.Textbook", -> ...@@ -9,4 +15,55 @@ describe "CMS.Models.Textbook", ->
expect(@model.get("showChapters")).toBeFalsy() expect(@model.get("showChapters")).toBeFalsy()
it "should have a ChapterSet with one chapter by default", -> it "should have a ChapterSet with one chapter by default", ->
expect(@model.get("chapters").length).toEqual(1) chapters = @model.get("chapters")
expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet)
expect(chapters.length).toEqual(1)
it "should be empty by default", ->
expect(@model.isEmpty()).toBeTruthy()
describe "CMS.Models.Textbook input/output", ->
# replace with Backbone.Assocations.deepAttributes when
# https://github.com/dhruvaray/backbone-associations/pull/43 is merged
deepAttributes = (obj) ->
if obj instanceof Backbone.Model
deepAttributes(obj.attributes)
else if obj instanceof Backbone.Collection
obj.map(deepAttributes);
else if _.isArray(obj)
_.map(obj, deepAttributes);
else if _.isObject(obj)
attributes = {};
for own prop, val of obj
attributes[prop] = deepAttributes(val)
attributes
else
obj
it "should match server model to client model", ->
serverModelSpec = {
"tab_title": "My Textbook",
"chapters": [
{"title": "Chapter 1", "url": "/ch1.pdf"},
{"title": "Chapter 2", "url": "/ch2.pdf"},
]
}
clientModelSpec = {
"name": "My Textbook",
"showChapters": false,
"chapters": [{
"name": "Chapter 1",
"asset_path": "/ch1.pdf",
"order": 1
}, {
"name": "Chapter 2",
"asset_path": "/ch2.pdf",
"order": 2
}
]
}
model = new CMS.Models.Textbook(serverModelSpec, {parse: true})
expect(deepAttributes(model)).toEqual(clientModelSpec)
expect(model.toJSON()).toEqual(serverModelSpec)
...@@ -16,18 +16,19 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ ...@@ -16,18 +16,19 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
return !this.get('name') && this.get('chapters').isEmpty(); return !this.get('name') && this.get('chapters').isEmpty();
}, },
parse: function(response) { parse: function(response) {
if("tab_title" in response && !("name" in response)) { var ret = $.extend(true, {}, response);
response.name = response.tab_title; if("tab_title" in ret && !("name" in ret)) {
delete response.tab_title; ret.name = ret.tab_title;
delete ret.tab_title;
} }
if("url" in response && !("chapters" in response)) { if("url" in ret && !("chapters" in ret)) {
response.chapters = {"url": response.url}; ret.chapters = {"url": ret.url};
delete response.url; delete ret.url;
} }
_.each(response.chapters, function(chapter, i) { _.each(ret.chapters, function(chapter, i) {
chapter.order = chapter.order || i+1; chapter.order = chapter.order || i+1;
}); });
return response; return ret;
}, },
toJSON: function() { toJSON: function() {
return { return {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment