Commit cfa094e4 by David Baumgold

Validation tests for Backbone models

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