-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollection-class-tests.js
116 lines (87 loc) · 2.72 KB
/
collection-class-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
import {Mongo} from "meteor/mongo";
import { assert } from 'meteor/practicalmeteor:chai';
class Book {
constructor() {
this.constructorWasCalled = true;
}
author() {
return Authors.findOne(this.authorId);
}
collectionClassOnInit() {
// all fields from the monogo document is on this
this.initWasCalled = true;
}
}
class Author {
fullName() {
return this.firstName + ' ' + this.lastName;
}
books() {
return Books.find({authorId: this._id});
}
get testGetter() {
return "testing";
}
documentFieldOverwritten() {
return 'oh noes this will be overwritten!';
}
}
describe('tuxbear:collection-class', function () {
if (Meteor.isClient) {
return;
}
let test = {};
beforeEach(function() {
test.id = new Date().getTime();
Books = new Meteor.Collection('books' + test.id);
Authors = new Meteor.Collection('authors' + test.id);
});
afterEach(function () {
Books.rawCollection().drop();
Authors.rawCollection().drop();
});
it('should transform collection documents', function () {
var authorWithGetterSaved = Authors.insert(new Author());
var author1 = Authors.insert({
firstName: 'Charles',
lastName: 'Darwin',
test: function() {return "test"}
});
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.setClass(Book);
Authors.setClass(Author);
var book = Books.findOne(book1);
var author = book.author();
assert.equal(author.firstName, 'Charles');
book = Books.findOne(book2);
author = book.author();
assert.equal(author.fullName(), 'Carl Sagan');
author = Authors.findOne(author1);
books = author.books();
book = books.fetch()[0];
assert.equal(books.count(), 1);
assert.isTrue(book.constructorWasCalled);
assert.isTrue(book.initWasCalled);
});
it("BEWARE: document fields overwrites properties from the class without warning", function () {
Authors.setClass(Author);
var author1 = Authors.insert({
firstName: 'Charles',
lastName: 'Darwin',
documentFieldOverwritten: 'this is what you get'
});
var author = Authors.findOne(author1);
assert.equal(author.documentFieldOverwritten, 'this is what you get');
});
});