-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests_client.coffee
190 lines (138 loc) · 4.56 KB
/
tests_client.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
runs = []
output = []
Template.computedFieldTestTemplate.onCreated ->
internal = new ReactiveVar 42
@field = new ComputedField =>
runs.push true
internal.get()
,
true
@autorun (computation) =>
f = new ComputedField =>
Template.currentData()?.foo?() % 10
output.push f()
Template.computedFieldTestTemplate.helpers
bar: ->
field = new ComputedField =>
foo = Template.currentData()?.foo?()
if _.isNumber foo
foo % 10
else
''
field()
Template.computedFieldTestTemplate.events
'click .computedFieldTestTemplate': (event) ->
Template.instance().field()
class TemplateTestCase extends ClassyTestCase
@testName: 'computed-field - template'
testTemplate: [
->
@internal = new ReactiveVar 42
@foo = new ComputedField =>
@internal.get()
@rendered = Blaze.renderWithData Template.computedFieldTestTemplate, {foo: @foo}, $('body').get(0)
Tracker.afterFlush @expect()
,
->
@assertEqual $('.computedFieldTestTemplate').text(), '42|2'
@internal.set 43
# Field flush happens automatically when using getter.
@assertEqual @foo(), 43
# There was no global flush yet, so old value is rendered.
@assertEqual $('.computedFieldTestTemplate').text(), '42|2'
Tracker.afterFlush @expect()
,
->
# But after global flush we want that the new value is rendered, even if we flushed
# the autorun before the global flush happened (by calling a getter).
@assertEqual $('.computedFieldTestTemplate').text(), '43|3'
Blaze.remove @rendered
# Even after all dependencies are removed, autorun is still active.
@assertTrue @foo._isRunning()
# But after the value changes again and the computation reruns, a new check is made after the global flush.
@internal.set 44
@assertEqual @foo(), 44
Tracker.afterFlush @expect()
,
->
# And now the computed field should not be running anymore.
@assertFalse @foo._isRunning()
@internal.set 45
@assertFalse Tracker.active
@assertEqual @foo(), 45
Tracker.afterFlush @expect()
,
->
# Value was updated, but because getter was not called in the reactive context, autorun was stopped again.
@assertFalse @foo._isRunning()
# But now if we render the template again and register a dependency again.
@rendered = Blaze.renderWithData Template.computedFieldTestTemplate, {foo: @foo}, $('body').get(0)
Tracker.afterFlush @expect()
,
->
@assertEqual $('.computedFieldTestTemplate').text(), '45|5'
# Autorun is running again.
@assertTrue @foo._isRunning()
Blaze.remove @rendered
# Still running. There was no value change and no global flush yet.
@assertTrue @foo._isRunning()
# We can also stop autorun manually.
@foo.stop()
@assertFalse @foo._isRunning()
]
testTemplateAutorun: [
->
runs = []
output = []
@rendered = Blaze.render Template.computedFieldTestTemplate, $('body').get(0)
Tracker.afterFlush @expect()
,
->
@assertEqual $('.computedFieldTestTemplate').text(), '|'
$('.computedFieldTestTemplate').click()
Tracker.afterFlush @expect()
,
->
$('.computedFieldTestTemplate').click()
Tracker.afterFlush @expect()
,
->
@assertTrue @rendered.templateInstance().field._isRunning()
Blaze.remove @rendered
@assertFalse @rendered.templateInstance().field._isRunning()
@assertEqual runs.length, 1
@assertEqual output.length, 1
]
testTemplateNestedAutorun: [
->
output = []
@internal = new ReactiveVar 42
@foo = new ComputedField =>
@internal.get()
@rendered = Blaze.renderWithData Template.computedFieldTestTemplate, {foo: @foo}, $('body').get(0)
Tracker.afterFlush @expect()
,
->
@assertEqual $('.computedFieldTestTemplate').text(), '42|2'
@internal.set 43
@assertEqual $('.computedFieldTestTemplate').text(), '42|2'
Tracker.afterFlush @expect()
,
->
@assertEqual $('.computedFieldTestTemplate').text(), '43|3'
@internal.set 53
@assertEqual $('.computedFieldTestTemplate').text(), '43|3'
Tracker.afterFlush @expect()
,
->
@assertEqual $('.computedFieldTestTemplate').text(), '53|3'
Blaze.remove @rendered
@internal.set 54
Tracker.afterFlush @expect()
,
->
# We can also stop autorun manually.
@foo.stop()
@assertEqual output, [2, 3]
]
ClassyTestCase.addTest new TemplateTestCase()