Validation tests for Backbone models

This commit is contained in:
David Baumgold
2013-07-02 14:24:28 -04:00
parent eba5928009
commit cfa094e424

View File

@@ -8,87 +8,119 @@ describe "CMS.Models.Textbook", ->
beforeEach -> beforeEach ->
@model = new CMS.Models.Textbook() @model = new CMS.Models.Textbook()
it "should have an empty name by default", -> describe "Basic", ->
expect(@model.get("name")).toEqual("") it "should have an empty name by default", ->
expect(@model.get("name")).toEqual("")
it "should not show chapters by default", -> it "should not show chapters by default", ->
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", ->
chapters = @model.get("chapters") chapters = @model.get("chapters")
expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet) expect(chapters).toBeInstanceOf(CMS.Collections.ChapterSet)
expect(chapters.length).toEqual(1) expect(chapters.length).toEqual(1)
expect(chapters.at(0).isEmpty()).toBeTruthy() expect(chapters.at(0).isEmpty()).toBeTruthy()
it "should be empty by default", -> it "should be empty by default", ->
expect(@model.isEmpty()).toBeTruthy() expect(@model.isEmpty()).toBeTruthy()
it "should have a URL set", -> it "should have a URL set", ->
expect(_.result(@model, "url")).toBeTruthy() expect(_.result(@model, "url")).toBeTruthy()
it "should be able to reset itself", -> it "should be able to reset itself", ->
@model.set("name", "foobar") @model.set("name", "foobar")
@model.reset() @model.reset()
expect(@model.get("name")).toEqual("") expect(@model.get("name")).toEqual("")
it "should not be dirty by default", -> it "should not be dirty by default", ->
expect(@model.isDirty()).toBeFalsy() expect(@model.isDirty()).toBeFalsy()
it "should be dirty after it's been changed", -> it "should be dirty after it's been changed", ->
@model.set("name", "foobar") @model.set("name", "foobar")
expect(@model.isDirty()).toBeTruthy() expect(@model.isDirty()).toBeTruthy()
it "should not be dirty after calling setOriginalAttributes", -> it "should not be dirty after calling setOriginalAttributes", ->
@model.set("name", "foobar") @model.set("name", "foobar")
@model.setOriginalAttributes() @model.setOriginalAttributes()
expect(@model.isDirty()).toBeFalsy() expect(@model.isDirty()).toBeFalsy()
describe "Input/Output", ->
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
describe "CMS.Models.Textbook input/output", -> it "should match server model to client model", ->
# replace with Backbone.Assocations.deepAttributes when serverModelSpec = {
# https://github.com/dhruvaray/backbone-associations/pull/43 is merged "tab_title": "My Textbook",
deepAttributes = (obj) -> "chapters": [
if obj instanceof Backbone.Model {"title": "Chapter 1", "url": "/ch1.pdf"},
deepAttributes(obj.attributes) {"title": "Chapter 2", "url": "/ch2.pdf"},
else if obj instanceof Backbone.Collection ]
obj.map(deepAttributes); }
else if _.isArray(obj) clientModelSpec = {
_.map(obj, deepAttributes); "name": "My Textbook",
else if _.isObject(obj) "showChapters": false,
attributes = {}; "editing": false,
for own prop, val of obj "chapters": [{
attributes[prop] = deepAttributes(val) "name": "Chapter 1",
attributes "asset_path": "/ch1.pdf",
else "order": 1
obj }, {
"name": "Chapter 2",
"asset_path": "/ch2.pdf",
"order": 2
}
]
}
it "should match server model to client model", -> model = new CMS.Models.Textbook(serverModelSpec, {parse: true})
serverModelSpec = { expect(deepAttributes(model)).toEqual(clientModelSpec)
"tab_title": "My Textbook", expect(model.toJSON()).toEqual(serverModelSpec)
"chapters": [
{"title": "Chapter 1", "url": "/ch1.pdf"}, describe "Validation", ->
{"title": "Chapter 2", "url": "/ch2.pdf"}, it "requires a name", ->
] model = new CMS.Models.Textbook({name: ""})
} expect(model.isValid()).toBeFalsy()
clientModelSpec = {
"name": "My Textbook", it "requires at least one chapter", ->
"showChapters": false, model = new CMS.Models.Textbook({name: "foo"})
"editing": false, model.get("chapters").reset()
"chapters": [{ expect(model.isValid()).toBeFalsy()
"name": "Chapter 1",
"asset_path": "/ch1.pdf", it "requires a valid chapter", ->
"order": 1 chapter = new CMS.Models.Chapter()
}, { chapter.isValid = -> false
"name": "Chapter 2", model = new CMS.Models.Textbook({name: "foo"})
"asset_path": "/ch2.pdf", model.get("chapters").reset([chapter])
"order": 2 expect(model.isValid()).toBeFalsy()
}
] it "requires all chapters to be valid", ->
} chapter1 = new CMS.Models.Chapter()
chapter1.isValid = -> true
chapter2 = new CMS.Models.Chapter()
chapter2.isValid = -> false
model = new CMS.Models.Textbook({name: "foo"})
model.get("chapters").reset([chapter1, chapter2])
expect(model.isValid()).toBeFalsy()
it "can pass validation", ->
chapter = new CMS.Models.Chapter()
chapter.isValid = -> true
model = new CMS.Models.Textbook({name: "foo"})
model.get("chapters").reset([chapter])
expect(model.isValid()).toBeTruthy()
model = new CMS.Models.Textbook(serverModelSpec, {parse: true})
expect(deepAttributes(model)).toEqual(clientModelSpec)
expect(model.toJSON()).toEqual(serverModelSpec)
describe "CMS.Collections.TextbookSet", -> describe "CMS.Collections.TextbookSet", ->
beforeEach -> beforeEach ->
@@ -111,17 +143,31 @@ describe "CMS.Models.Chapter", ->
beforeEach -> beforeEach ->
@model = new CMS.Models.Chapter() @model = new CMS.Models.Chapter()
it "should have a name by default", -> describe "Basic", ->
expect(@model.get("name")).toEqual("") it "should have a name by default", ->
expect(@model.get("name")).toEqual("")
it "should have an asset_path by default", -> it "should have an asset_path by default", ->
expect(@model.get("asset_path")).toEqual("") expect(@model.get("asset_path")).toEqual("")
it "should have an order by default", -> it "should have an order by default", ->
expect(@model.get("order")).toEqual(1) expect(@model.get("order")).toEqual(1)
it "should be empty by default", -> it "should be empty by default", ->
expect(@model.isEmpty()).toBeTruthy() expect(@model.isEmpty()).toBeTruthy()
describe "Validation", ->
it "requires a name", ->
model = new CMS.Models.Chapter({name: "", asset_path: "a.pdf"})
expect(model.isValid()).toBeFalsy()
it "requires an asset_path", ->
model = new CMS.Models.Chapter({name: "a", asset_path: ""})
expect(model.isValid()).toBeFalsy()
it "can pass validation", ->
model = new CMS.Models.Chapter({name: "a", asset_path: "a.pdf"})
expect(model.isValid()).toBeTruthy()
describe "CMS.Collections.ChapterSet", -> describe "CMS.Collections.ChapterSet", ->