2011-11-17 04:24:43 +00:00
|
|
|
juggling = require('../index')
|
|
|
|
Schema = juggling.Schema
|
|
|
|
AbstractClass = juggling.AbstractClass
|
|
|
|
Hookable = juggling.Hookable
|
|
|
|
|
|
|
|
require('./spec_helper').init module.exports
|
|
|
|
|
|
|
|
schema = new Schema 'memory'
|
|
|
|
User = schema.define 'User',
|
|
|
|
email: String
|
|
|
|
name: String
|
|
|
|
password: String
|
|
|
|
state: String
|
|
|
|
age: Number
|
|
|
|
gender: String
|
|
|
|
domain: String
|
|
|
|
pendingPeriod: Number
|
|
|
|
createdByAdmin: Boolean
|
|
|
|
|
2012-06-02 18:41:28 +00:00
|
|
|
it "should trigger after initialize", (test) ->
|
|
|
|
User.afterInitialize = ->
|
|
|
|
User.afterInitialize = null
|
|
|
|
test.done()
|
|
|
|
user = new User
|
2011-11-17 04:24:43 +00:00
|
|
|
|
|
|
|
|
2012-06-02 18:41:28 +00:00
|
|
|
it "should trigger before create", (test) ->
|
|
|
|
User.beforeCreate = () ->
|
|
|
|
User.beforeCreate = null
|
|
|
|
test.done()
|
|
|
|
User.create -> test.ok "saved"
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2011-12-11 19:25:13 +00:00
|
|
|
it "should trigger after create", (test) ->
|
|
|
|
User.afterCreate = (next) ->
|
|
|
|
User.afterCreate = null
|
|
|
|
next()
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2012-06-02 18:41:28 +00:00
|
|
|
User.create ->
|
2011-12-11 19:25:13 +00:00
|
|
|
test.ok "saved"
|
|
|
|
test.done()
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2011-12-11 19:25:13 +00:00
|
|
|
it 'should trigger before save', (test) ->
|
|
|
|
test.expect(3)
|
|
|
|
User.beforeSave = (next) ->
|
|
|
|
User.beforeSave = null
|
|
|
|
@name = 'mr. ' + @name
|
|
|
|
next()
|
|
|
|
user = new User name: 'Jonathan'
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2011-12-11 19:25:13 +00:00
|
|
|
user.save ->
|
|
|
|
test.equals User.schema.adapter.cache.User[user.id].name, user.name
|
|
|
|
test.equals user.name, 'mr. Jonathan'
|
|
|
|
test.ok 'saved'
|
|
|
|
test.done()
|
|
|
|
|
2012-06-02 18:41:28 +00:00
|
|
|
it 'should trigger after save', (test) ->
|
|
|
|
User.afterSave = (next) ->
|
2011-12-11 19:25:13 +00:00
|
|
|
User.afterSave = null
|
|
|
|
next()
|
|
|
|
|
|
|
|
user = new User
|
|
|
|
user.save ->
|
|
|
|
test.ok "saved"
|
|
|
|
test.done()
|
|
|
|
|
|
|
|
it "should trigger before update", (test) ->
|
2012-06-02 18:41:28 +00:00
|
|
|
User.beforeUpdate = () ->
|
|
|
|
User.beforeUpdate = null
|
|
|
|
test.done()
|
|
|
|
User.create {}, (err, user) ->
|
|
|
|
user.updateAttributes email:"1@1.com", -> test.ok "updated"
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2011-12-11 19:25:13 +00:00
|
|
|
it "should trigger after update", (test) ->
|
2012-06-02 18:41:28 +00:00
|
|
|
User.afterUpdate = () ->
|
2011-12-11 19:25:13 +00:00
|
|
|
User.afterUpdate = null
|
|
|
|
test.done()
|
2012-06-02 18:41:28 +00:00
|
|
|
User.create (err, user) ->
|
|
|
|
user.updateAttributes email: "1@1.com", -> test.ok "updated"
|
2011-11-17 04:24:43 +00:00
|
|
|
|
|
|
|
it "should trigger before destroy", (test)->
|
2012-06-02 18:41:28 +00:00
|
|
|
User.beforeDestroy = () ->
|
|
|
|
User.beforeDestroy = null
|
|
|
|
test.done()
|
|
|
|
User.create {}, (err, user) ->
|
|
|
|
user.destroy()
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2012-06-02 18:41:28 +00:00
|
|
|
it "should trigger after destroy", (test) ->
|
|
|
|
User.afterDestroy = () ->
|
|
|
|
User.afterDestroy = null
|
|
|
|
test.done()
|
|
|
|
User.create (err, user) ->
|
|
|
|
user.destroy()
|
2011-11-18 18:59:46 +00:00
|
|
|
|
2012-06-02 18:42:22 +00:00
|
|
|
it 'allows me to modify attributes before saving', (test) ->
|
|
|
|
test.done()
|