-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanopticonSpec.js
More file actions
165 lines (147 loc) · 5.05 KB
/
panopticonSpec.js
File metadata and controls
165 lines (147 loc) · 5.05 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var panopticon = require('panopticon'),
mongoose = require('mongoose');
/*
* clearCollections
* - Removes documents from collections
* - Used after tests to reset db
* - https://github.com/elliotf/mocha-mongoose/blob/master/index.js
*/
var clearCollections = function(db, callback) {
db.collections(function(err, collections){
if (err) return callback(err);
var todo = collections.length;
if (!todo) return callback(null);
collections.forEach(function(collection){
if (collection.collectionName.match(/^system\./)) return --todo;
collection.remove({},{safe: true}, function(){
if (--todo === 0) callback(null);
});
});
});
};
var personSchema = mongoose.Schema({
name: String,
email: String,
address: {
line1: String,
line2: String
},
cities_visited: [String],
pets: [{
name: String,
age: Number
}]
});
var rules = {
'name': function() {},
'email': function() {},
'address': {
line1: function() {},
line2: function() {}
},
'cities_visited': function() {},
'pets': {
'name': function() {}
}
};
panopticon.watch(personSchema, rules);
var Person = mongoose.model('Person', personSchema);
describe("Panopticon", function() {
beforeEach(function(done){
// Connect to the DB
var mongoUri = 'mongodb://localhost/test';
mongoose.connect(mongoUri, done);
});
afterEach(function(done){
clearCollections(mongoose.connection.db, function(){
mongoose.connection.close(done);
});
});
describe("#watch", function(){
var person;
beforeEach(function(done){
Person.create({
name: 'Adam',
address: {
line1: '123 Street Lane'
},
cities_visited: ['Copenhagen'],
pets: [{name: 'Fido', age: 4},
{name: 'Lucy', age: 8}]
}, function(err, p){
// Get for 'init'
Person.findById(p.id, function(err, p){
person = p;
done();
});
});
});
describe("calls rule handler after", function(){
it("property addition", function(done){
spyOn(rules, 'email');
person.email = 'adam@email.com';
person.save(function(err, person){
expect(rules.email).toHaveBeenCalledWith('adam@email.com');
done();
});
});
it("property update", function(done){
spyOn(rules, 'name');
person.name = 'Bert';
person.save(function(err, person){
expect(rules.name).toHaveBeenCalledWith('Bert');
done();
});
});
it("property deletion", function(done){
spyOn(rules, 'name');
person.name = null;
person.save(function(err, person){
expect(rules.name).toHaveBeenCalledWith(null);
done();
});
});
it("nested property addition", function(done){
spyOn(rules.address, 'line2');
person.address.line2 = 'Townville';
person.save(function(err, person){
expect(rules.address.line2).toHaveBeenCalledWith('Townville');
done();
});
});
it("nested property update", function(done){
spyOn(rules.address, 'line1');
person.address.line1 = '56 Another Street';
person.save(function(err, person){
expect(rules.address.line1).toHaveBeenCalledWith('56 Another Street');
done();
});
});
it("nested property deletion", function(done){
spyOn(rules.address, 'line1');
person.address.line1 = null;
person.save(function(err, person){
expect(rules.address.line1).toHaveBeenCalledWith(null);
done();
});
});
it("array item addition - returning diff", function(done){
spyOn(rules, 'cities_visited');
person.cities_visited.push('Paris');
person.save(function(err, person){
expect(rules.cities_visited).toHaveBeenCalledWith({'_t': 'a', '1': ['Paris']});
done();
});
});
it("object array item addition - returning diff", function(done){
spyOn(rules, 'pets');
person.pets.push({name: 'Coco'});
var id = person.pets[person.pets.length - 1]._id.toString();
person.save(function(err, person){
expect(rules.pets).toHaveBeenCalledWith({'_t': 'a', '2': [{name: 'Coco', _id: id}]});
done();
});
});
});
});
});