-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathassociation-hasmany-hooks.js
More file actions
127 lines (108 loc) · 2.39 KB
/
association-hasmany-hooks.js
File metadata and controls
127 lines (108 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var should = require('should');
var helper = require('../support/spec_helper');
var ORM = require('../../');
describe("hasMany hooks", function() {
var db = null;
var Person = null;
var Pet = null;
this.timeout(5000);
var setup = function (props, opts) {
return function (done) {
db.settings.set('instance.cache', false);
Person = db.define('person', {
name : String,
});
Pet = db.define('pet', {
name : String
});
Person.hasMany('pets', Pet, props || {}, opts || {});
return helper.dropSync([ Person, Pet ], done);
};
};
before(function(done) {
helper.connect(function (connection) {
db = connection;
done();
});
});
describe("beforeSave", function () {
var had_extra = false;
before(setup({
born : Date
}, {
hooks : {
beforeSave: function (extra, next) {
had_extra = (typeof extra == "object");
return next();
}
}
}));
it("should pass extra data to hook if extra defined", function (done) {
Person.create({
name : "John"
}, function (err, John) {
Pet.create({
name : "Deco"
}, function (err, Deco) {
John.addPets(Deco, function (err) {
should.not.exist(err);
had_extra.should.be.true;
return done();
});
});
});
});
});
describe("beforeSave", function () {
var had_extra = false;
before(setup({}, {
hooks : {
beforeSave: function (next) {
next.should.be.a("function");
return next();
}
}
}));
it("should not pass extra data to hook if extra defined", function (done) {
Person.create({
name : "John"
}, function (err, John) {
Pet.create({
name : "Deco"
}, function (err, Deco) {
John.addPets(Deco, function (err) {
should.not.exist(err);
return done();
});
});
});
});
});
describe("beforeSave", function () {
var had_extra = false;
before(setup({}, {
hooks : {
beforeSave: function (next) {
setTimeout(function () {
return next(new Error('blocked'));
}, 100);
}
}
}));
it("should block if error returned", function (done) {
Person.create({
name : "John"
}, function (err, John) {
Pet.create({
name : "Deco"
}, function (err, Deco) {
John.addPets(Deco, function (err) {
should.exist(err);
err.message.should.equal('blocked');
return done();
});
});
});
});
});
});