-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.coffee
211 lines (175 loc) · 6.87 KB
/
tests.coffee
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
if Meteor.isServer
TestCollection = new Mongo.Collection null
Meteor.methods
insertTest: (obj) ->
TestCollection.insert obj
updateTest: (selector, query) ->
TestCollection.update selector, query
removeTest: (selector) ->
TestCollection.remove selector
# "argument" is used so that we can subscribe multiple times with different arguments.
Meteor.publish 'testPublish', (disableMergebox, argument) ->
@disableMergebox() if disableMergebox
handle = TestCollection.find().observeChanges
added: (id, fields) =>
@added 'testCollection', id, fields
changed: (id, fields) =>
@changed 'testCollection', id, fields
removed: (id) =>
@removed 'testCollection', id
@onStop =>
handle.stop()
@ready()
# "argument" is used so that we can subscribe multiple times with different arguments.
Meteor.publish 'testPublishFullObserve', (argument) ->
@disableMergebox()
handle = TestCollection.find({}, {transform: null}).observe
added: (newDocument) =>
@added 'testCollection', newDocument._id, _.omit newDocument, '_id'
changed: (newDocument, oldDocument) =>
for field, value of oldDocument when field not of newDocument
newDocument[field] = undefined
@changed 'testCollection', newDocument._id, _.omit newDocument, '_id'
removed: (oldDocument) =>
@removed 'testCollection', oldDocument._id
@onStop =>
handle.stop()
@ready()
else
TestCollection = new Mongo.Collection 'testCollection'
class BasicTestCase extends ClassyTestCase
@testName: 'disable-mergebox - basic'
setUpServer: ->
TestCollection.remove {}
@publishTest: (disableMergebox) ->
[
->
@assertSubscribeSuccessful 'testPublish', disableMergebox, @expect()
,
->
@assertEqual TestCollection.find().fetch(), []
Meteor.call 'insertTest', {foo: 'test', bar: 123}, @expect (error, documentId) =>
@assertFalse error, error
@assertTrue documentId
@document1Id = documentId
,
@runOnServer ->
for sessionId, session of Meteor.server.sessions
if disableMergebox
@assertEqual session.getCollectionView('testCollection').documents, {}
else
@assertNotEqual session.getCollectionView('testCollection').documents, {}
,
->
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test', bar: 123}]
Meteor.call 'updateTest', {_id: @document1Id}, {$set: {foo: 'test2'}}, @expect (error, count) =>
@assertFalse error, error
@assertEqual count, 1
,
@runOnServer ->
for sessionId, session of Meteor.server.sessions
if disableMergebox
@assertEqual session.getCollectionView('testCollection').documents, {}
else
@assertNotEqual session.getCollectionView('testCollection').documents, {}
,
->
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test2', bar: 123}]
Meteor.call 'updateTest', {_id: @document1Id}, {$unset: {foo: ''}}, @expect (error, count) =>
@assertFalse error, error
@assertEqual count, 1
,
@runOnServer ->
for sessionId, session of Meteor.server.sessions
if disableMergebox
@assertEqual session.getCollectionView('testCollection').documents, {}
else
@assertNotEqual session.getCollectionView('testCollection').documents, {}
,
->
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, bar: 123}]
Meteor.call 'updateTest', {_id: @document1Id}, {$set: {foo: 'test3'}}, @expect (error, count) =>
@assertFalse error, error
@assertEqual count, 1
,
@runOnServer ->
for sessionId, session of Meteor.server.sessions
if disableMergebox
@assertEqual session.getCollectionView('testCollection').documents, {}
else
@assertNotEqual session.getCollectionView('testCollection').documents, {}
,
->
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test3', bar: 123}]
Meteor.call 'removeTest', {_id: @document1Id}, @expect (error, count) =>
@assertFalse error, error
@assertEqual count, 1
,
@runOnServer ->
for sessionId, session of Meteor.server.sessions
@assertEqual session.getCollectionView('testCollection').documents, {}
,
->
@assertEqual TestCollection.find().fetch(), []
]
testClientMergeboxDisabled: @publishTest true
testClientMergeboxNotDisabled: @publishTest false
testClientMultipleSubscriptions: [
->
@subscription = @assertSubscribeSuccessful 'testPublish', true, 1, @expect()
@assertSubscribeSuccessful 'testPublish', true, 2, @expect()
,
->
@assertEqual TestCollection.find().fetch(), []
Meteor.call 'insertTest', {foo: 'test', bar: 123}, @expect (error, documentId) =>
@assertFalse error, error
@assertTrue documentId
@document1Id = documentId
,
->
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test', bar: 123}]
@subscription.stop()
# To wait a bit for unsubscribe to happen.
Meteor.setTimeout @expect(), 100 # ms
,
->
# Last change wins, document has been removed.
@assertEqual TestCollection.find().fetch(), []
Meteor.call 'updateTest', {_id: @document1Id}, {$set: {foo: 'test2'}}, @expect (error, count) =>
@assertFalse error, error
@assertEqual count, 1
,
->
# Only the changed field is published.
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test2'}]
]
testClientMultipleSubscriptionsFullObserve: [
->
@subscription = @assertSubscribeSuccessful 'testPublishFullObserve', 1, @expect()
@assertSubscribeSuccessful 'testPublishFullObserve', 2, @expect()
,
->
@assertEqual TestCollection.find().fetch(), []
Meteor.call 'insertTest', {foo: 'test', bar: 123}, @expect (error, documentId) =>
@assertFalse error, error
@assertTrue documentId
@document1Id = documentId
,
->
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test', bar: 123}]
@subscription.stop()
# To wait a bit for unsubscribe to happen.
Meteor.setTimeout @expect(), 100 # ms
,
->
# Last change wins, document has been removed.
@assertEqual TestCollection.find().fetch(), []
Meteor.call 'updateTest', {_id: @document1Id}, {$set: {foo: 'test2'}}, @expect (error, count) =>
@assertFalse error, error
@assertEqual count, 1
,
->
# With full observe all fields are published.
@assertEqual TestCollection.find().fetch(), [{_id: @document1Id, foo: 'test2', bar: 123}]
]
ClassyTestCase.addTest new BasicTestCase()