-
Notifications
You must be signed in to change notification settings - Fork 8
/
tests.coffee
222 lines (158 loc) · 7.31 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
212
213
214
215
216
217
218
219
220
221
222
class DummyComponent extends BaseComponent
constructor: (field) ->
super()
@field = field
fieldValue: ->
@field
@renderComponent: ->
new @().renderComponent()
renderComponent: ->
"Hello world."
BaseComponent.register 'DummyComponent', DummyComponent
class SelfRegisterComponent extends DummyComponent
# Alternative way of registering components.
@register 'SelfRegisterComponent'
class UnregisteredComponent extends DummyComponent
# Name has to be set manually.
UnregisteredComponent.componentName 'UnregisteredComponent'
class SelfNameUnregisteredComponent extends DummyComponent
# Alternative way of setting the name manually.
@componentName 'SelfNameUnregisteredComponent'
class MyNamespace
class MyNamespace.MyComponent extends DummyComponent
# Alternative way of registering components.
@register 'MyNamespace.MyComponent'
class MyNamespaceComponent extends DummyComponent
# Probably you could simply have MyNamespace class as a component, but
# we want to test if these can be disjoint. Registry should not modify
# components and use them as a namespace.
@register 'MyNamespace'
class BasicTestCase extends ClassyTestCase
@testName: 'base-component - basic'
testBase: =>
result = BaseComponent.getComponent('DummyComponent').renderComponent()
@assertEqual result, "Hello world."
result = new (BaseComponent.getComponent('DummyComponent'))().renderComponent()
@assertEqual result, "Hello world."
testGetComponent: =>
@assertEqual BaseComponent.getComponent('DummyComponent'), DummyComponent
@assertEqual BaseComponent.getComponent('unknown'), null
testComponentName: =>
@assertEqual DummyComponent.componentName(), 'DummyComponent'
@assertEqual BaseComponent.componentName(), null
testSelfRegister: =>
@assertTrue BaseComponent.getComponent 'SelfRegisterComponent'
testUnregisteredComponent: =>
result = UnregisteredComponent.renderComponent()
@assertEqual result, "Hello world."
result = new UnregisteredComponent().renderComponent()
@assertEqual result, "Hello world."
result = SelfNameUnregisteredComponent.renderComponent()
@assertEqual result, "Hello world."
result = new SelfNameUnregisteredComponent().renderComponent()
@assertEqual result, "Hello world."
testNamespace: =>
# To test if a component with the same name as a namespace is reachable.
result = BaseComponent.getComponent('MyNamespace').renderComponent()
@assertEqual result, "Hello world."
result = new (BaseComponent.getComponent('MyNamespace'))().renderComponent()
@assertEqual result, "Hello world."
# And a namespaced component.
result = BaseComponent.getComponent('MyNamespace.MyComponent').renderComponent()
@assertEqual result, "Hello world."
result = new (BaseComponent.getComponent('MyNamespace.MyComponent'))().renderComponent()
@assertEqual result, "Hello world."
# And a namespaced component with explicit namespace.
result = BaseComponent.getComponent(BaseComponent.components.MyNamespace, 'MyComponent').renderComponent()
@assertEqual result, "Hello world."
result = new (BaseComponent.getComponent(BaseComponent.components.MyNamespace, 'MyComponent'))().renderComponent()
@assertEqual result, "Hello world."
testErrors: =>
@assertThrows =>
BaseComponent.register()
,
/Component name is required for registration/
@assertThrows =>
BaseComponent.register 'DummyComponent', null
,
/Component 'DummyComponent' already registered/
@assertThrows =>
BaseComponent.register 'OtherDummyComponent', DummyComponent
,
/Component 'OtherDummyComponent' already registered under the name 'DummyComponent'/
@assertThrows =>
BaseComponent.getComponent {}
,
/Component name '\[object Object\]' is not a string/
testChildren: =>
component = new DummyComponent 'foobar'
parentComponent = new UnregisteredComponent()
results = []
handle = Tracker.autorun =>
results.push parentComponent.childComponents()
resultsWith = []
handleWith = Tracker.autorun =>
resultsWith.push parentComponent.childComponentsWith(field: 'foobar')
component.parentComponent parentComponent
parentComponent.addChildComponent component
@assertEqual component.parentComponent(), parentComponent
@assertEqual parentComponent.childComponents(), [component]
@assertEqual parentComponent.childComponents(component), [component]
@assertEqual parentComponent.childComponents(parentComponent), []
@assertEqual parentComponent.childComponents(DummyComponent), [component]
@assertEqual parentComponent.childComponents(UnregisteredComponent), []
@assertEqual parentComponent.childComponents('DummyComponent'), [component]
@assertEqual parentComponent.childComponents('UnregisteredComponent'), []
@assertEqual parentComponent.childComponentsWith('field'), [component]
@assertEqual parentComponent.childComponentsWith('fieldValue'), [component]
@assertEqual parentComponent.childComponentsWith('nonexisting'), []
@assertEqual parentComponent.childComponentsWith(field: 'foobar'), [component]
@assertEqual parentComponent.childComponentsWith(field: 'faabar'), []
@assertEqual parentComponent.childComponentsWith(fieldValue: 'foobar'), [component]
@assertEqual parentComponent.childComponentsWith(fieldValue: 'faabar'), []
self = @
@assertEqual parentComponent.childComponentsWith(
(child) ->
self.assertEqual @, parentComponent
self.assertEqual child, component
true
), [component]
@assertEqual parentComponent.childComponentsWith((child) -> false), []
Tracker.flush()
component.parentComponent null
parentComponent.removeChildComponent component
@assertEqual component.parentComponent(), null
@assertEqual parentComponent.childComponents(), []
@assertEqual parentComponent.childComponents(component), []
@assertEqual parentComponent.childComponents(DummyComponent), []
@assertEqual parentComponent.childComponents(UnregisteredComponent), []
@assertEqual parentComponent.childComponents('DummyComponent'), []
@assertEqual parentComponent.childComponents('UnregisteredComponent'), []
@assertEqual parentComponent.childComponentsWith('field'), []
@assertEqual parentComponent.childComponentsWith('fieldValue'), []
@assertEqual parentComponent.childComponentsWith('nonexisting'), []
@assertEqual parentComponent.childComponentsWith(field: 'foobar'), []
@assertEqual parentComponent.childComponentsWith(field: 'faabar'), []
@assertEqual parentComponent.childComponentsWith(fieldValue: 'foobar'), []
@assertEqual parentComponent.childComponentsWith(fieldValue: 'faabar'), []
Tracker.flush()
componentZoobar = new DummyComponent 'zoobar'
componentZoobar.parentComponent parentComponent
parentComponent.addChildComponent componentZoobar
Tracker.flush()
handle.stop()
handleWith.stop()
@assertEqual results, [
[]
[component]
[]
[componentZoobar]
]
@assertEqual resultsWith, [
[]
[component]
[]
# One less entry than in "results" because "foobar" does not match "zoobar", and the
# result is the same, an empty array, so reactive computation is not invalidated.
]
ClassyTestCase.addTest new BasicTestCase()