-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.coffee
More file actions
165 lines (128 loc) · 4.52 KB
/
tests.coffee
File metadata and controls
165 lines (128 loc) · 4.52 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
165
if Meteor.isServer
TestDataCollection = new Mongo.Collection null
Meteor.methods
insertTest: (obj) ->
TestDataCollection.insert obj
updateTest: (selector, query) ->
TestDataCollection.update selector, query
removeTest: (selector) ->
TestDataCollection.remove selector
Meteor.publish 'testDataPublish', ->
@autorun (computation) =>
@setData 'countAll', TestDataCollection.find().count()
return
@autorun (computation) =>
TestDataCollection.find({}, {sort: {i: 1}, limit: @data('limit')}).observeChanges
addedBefore: (id, fields, before) =>
@added 'testDataCollection', id, fields
changed: (id, fields) =>
@changed 'testDataCollection', id, fields
removed: (id) =>
@removed 'testDataCollection', id
@ready()
return
Meteor.publish 'testPublish', ->
id = Random.id()
previousData = {}
@autorun (computation) =>
newData = @data()
data = _.clone newData
for field of previousData when field not of data
data[field] = undefined
if computation.firstRun
@added 'testCollection', id, data
else
@changed 'testCollection', id, data
previousData = newData
return
@ready()
else
TestDataCollection = new Mongo.Collection 'testDataCollection'
TestCollection = new Mongo.Collection 'testCollection'
class BasicTestCase extends ClassyTestCase
@testName: 'subscription-data - basic'
setUpServer: ->
TestDataCollection.remove {}
testClientBasic: [
->
@subscription1 = @assertSubscribeSuccessful 'testPublish', @expect()
,
->
@assertEqual TestCollection.find({}, {fields: _id: 0}).fetch(), [{}]
@assertEqual @subscription1.data(), {}
@subscription1.setData {foo: 'test', bar: 123}
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestCollection.find({}, {fields: _id: 0}).fetch(), [{foo: 'test', bar: 123}]
@assertEqual @subscription1.data(), {foo: 'test', bar: 123}
@subscription1.setData 'foo', 'test2'
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestCollection.find({}, {fields: _id: 0}).fetch(), [{foo: 'test2', bar: 123}]
@assertEqual @subscription1.data(), {foo: 'test2', bar: 123}
@subscription1.setData 'foo', undefined
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestCollection.find({}, {fields: _id: 0}).fetch(), [{bar: 123}]
@assertEqual @subscription1.data(), {bar: 123}
@subscription1.setData 'foo', 'test3'
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestCollection.find({}, {fields: _id: 0}).fetch(), [{foo: 'test3', bar: 123}]
@assertEqual @subscription1.data(), {foo: 'test3', bar: 123}
@subscription1.setData {}
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestCollection.find({}, {fields: _id: 0}).fetch(), [{}]
@assertEqual @subscription1.data(), {}
]
testClientTwoWay: [
->
@subscription2 = @assertSubscribeSuccessful 'testDataPublish', @expect()
,
->
@assertEqual TestDataCollection.find({}).fetch(), []
@assertEqual @subscription2.data(), {countAll: 0}
for i in [0...10]
Meteor.call 'insertTest', {i: i}, @expect (error, documentId) =>
@assertFalse error, error
@assertTrue documentId
,
->
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestDataCollection.find({}).count(), 10
@assertEqual @subscription2.data(), {countAll: 10}
@subscription2.setData 'limit', 5
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestDataCollection.find({}).count(), 5
@assertEqual @subscription2.data(), {countAll: 10, limit: 5}
for i in [0...10]
Meteor.call 'insertTest', {i: i}, @expect (error, documentId) =>
@assertFalse error, error
@assertTrue documentId
,
->
# To wait a bit for change to propagate.
Meteor.setTimeout @expect(), 200 # ms
,
->
@assertEqual TestDataCollection.find({}).count(), 5
@assertEqual @subscription2.data(), {countAll: 20, limit: 5}
]
ClassyTestCase.addTest new BasicTestCase()