-
Notifications
You must be signed in to change notification settings - Fork 34
/
collection-helpers_tests.js
146 lines (121 loc) · 3.35 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
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
const meteorVersion = Meteor.release
? parseFloat(Meteor.release.split("@")[1])
: null;
if (meteorVersion && meteorVersion >= 3) {
Tinytest.addAsync("works", async function (test) {
const Books = new Mongo.Collection(
Meteor.isClient ? null : "books" + test.id
);
const Authors = new Mongo.Collection(
Meteor.isClient ? null : "authors" + test.id
);
const author1 = await Authors.insertAsync({
firstName: "Charles",
lastName: "Darwin",
});
const author2 = await Authors.insertAsync({
firstName: "Carl",
lastName: "Sagan",
});
const book1 = await Books.insertAsync({
authorId: author1,
name: "On the Origin of Species",
});
const book2 = await Books.insertAsync({
authorId: author2,
name: "Contact",
});
Books.helpers({
author() {
return Authors.findOneAsync(this.authorId);
},
});
// We should be able to apply more if we wish
Books.helpers({
foo: "bar",
});
Authors.helpers({
fullName() {
return this.firstName + " " + this.lastName;
},
books() {
return Books.find({ authorId: this._id });
},
});
const book = await Books.findOneAsync(book1);
const author = await book.author();
test.equal(author.firstName, "Charles");
test.equal(author.fullName(), "Charles Darwin");
test.equal(book.foo, "bar");
const authorCarl = await Authors.findOneAsync(author2);
const booksByCarl = await authorCarl.books().fetchAsync();
test.equal(booksByCarl.length, 1);
});
} else {
// Meteor < 3.0
Tinytest.add("works", function (test) {
const Books = new Mongo.Collection("books" + test.id);
const Authors = new Mongo.Collection("authors" + test.id);
const author1 = Authors.insert({
firstName: "Charles",
lastName: "Darwin",
});
const author2 = Authors.insert({
firstName: "Carl",
lastName: "Sagan",
});
const book1 = Books.insert({
authorId: author1,
name: "On the Origin of Species",
});
const 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 });
},
});
const book = Books.findOne(book1);
const author = book.author();
test.equal(author.firstName, "Charles");
test.equal(book.foo, "bar");
const authorCarl = Authors.findOne(author1);
const booksByCarl = author.books();
test.equal(booksByCarl.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;
},
});
});
}
);