forked from dburles/meteor-collection-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection-helpers_tests.js
74 lines (60 loc) · 1.64 KB
/
collection-helpers_tests.js
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
Tinytest.add("works", function(test) {
Books = new Mongo.Collection('books' + test.id);
Authors = new Mongo.Collection('authors' + test.id);
var author1 = Authors.insert({
firstName: 'Charles',
lastName: 'Darwin'
});
var author2 = Authors.insert({
firstName: 'Carl',
lastName: 'Sagan'
});
var book1 = Books.insert({
authorId: author1,
name: 'On the Origin of Species'
});
var book2 = Books.insert({
authorId: author2,
name: 'Contact'
});
Books.helpers({
author: function() {
return Authors.findOne(this.authorId);
}
});
// We should be able to apply more if we wish
Books.helpers({
foo: 'bar'
});
Authors.helpers({
fullName: function() {
return this.firstName + ' ' + this.lastName;
},
books: function() {
return Books.find({ authorId: this._id });
}
});
var book = Books.findOne(book1);
var author = book.author();
test.equal(author.firstName, 'Charles');
test.equal(book.foo, 'bar');
book = Books.findOne(book2);
author = book.author();
test.equal(author.fullName(), 'Carl Sagan');
author = Authors.findOne(author1);
books = author.books();
test.equal(books.count(), 1);
});
Tinytest.add("throw error if transform function already exists", function(test) {
Author = function(doc) { return _.extend(this, doc); };
Author.prototype.fullName = 'Charles Darwin';
Authors = new Meteor.Collection('authors' + test.id, {
transform: function(doc) { return new Author(doc); }});
test.throws(function() {
Authors.helpers({
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
});
});
});