Skip to content

Instantly share code, notes, and snippets.

@akb
Forked from dmuskat/image-model
Created August 2, 2012 19:52
Show Gist options
  • Save akb/3240088 to your computer and use it in GitHub Desktop.
Save akb/3240088 to your computer and use it in GitHub Desktop.
MODEL COFFEE FILE image.coffee :
window.models = {} unless window.models?
JSV = require("./jsv").JSV
jsvEnv = JSV.createEnvironment()
imageSchema =
"type":"object"
"id":"#image"
"properties":
"name": { "type":"string" }
"encoding": { "type":"string" }
"drcid": { "type":"string" }
"type": { "type":"string" }
"environment": { "type":"string" }
"content": { "type":"string" }
class models.Image extends Backbone.Model
idAttribute: "name"
urlRoot: "/images"
defaults:
name: ""
encoding: ""
drcid: ""
type: ""
environment: ""
content: ""
validate: (attrs) ->
unless jsvEnv.validate(this, imageSchema) is 0
return "Image must follow schema."
unless typeof attrs.encoding is "string"
return "Encoding must be a string."
unless typeof attrs.drcid is "string"
return "Drcid must be a string."
unless typeof attrs.type is "string"
return "Type must be a string."
unless typeof attrs.environment is "string"
return "Environment must be a string."
SPEC COFFEE FILE image.spec.coffee:
describe "Image testing environment", ->
it "is sane", ->
expect(true).toBe(true)
describe "Image model", ->
myImage = null
beforeEach ->
myImage = new models.Image
afterEach ->
myImage = null
it "follows schema", ->
expect(myImage.get 'name').toBeDefined()
expect(myImage.get 'encoding').toBeDefined()
expect(myImage.get 'drcid').toBeDefined()
expect(myImage.get 'type').toBeDefined()
expect(myImage.get 'environment').toBeDefined()
expect(myImage.get 'content').toBeDefined()
it "knows that drcid is its identifier", ->
myImageWithData = new models.Image({name: "test1"})
expect(myImageWithData.id).toBe('test1')
it "knows that /images is its urlRoot", ->
myImageWithUrl = new models.Image({name: "test1"})
expect(myImageWithUrl.urlRoot).toBe("/images")
it "generates error when data with invalid schema is bound", ->
errorEventHasBeenFired = false
myImage.on "error", ->
errorEventHasBeenFired = true
model.set({ badData: "somebaddata" })
expect(errorEventHasBeenFired).toBe(true)
it "generates error when data with invalid schema is bound and is asynchronous-friendly", (done) ->
myImage.on "error", ->
expect(true).toBe(true)
done()
model.set({ badData: "somebaddata" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment