-
Notifications
You must be signed in to change notification settings - Fork 8
/
layout-test.js
224 lines (181 loc) · 7.06 KB
/
layout-test.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
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
223
224
String.prototype.compact = function () {
return this.trim().replace(/\s/g, '').replace(/\n/g, '');
};
var renderComponent = function (cmp, parent, props) {
var inst = UI.render(cmp.extend(props || {}));
UI.DomRange.insert(inst.dom, parent);
return inst;
};
var renderLayout = function (parent, props) {
return renderComponent(Layout, parent, props);
};
var withRenderedComponent = function (cmp, props, cb) {
if (arguments.length < 3) {
cb = props;
props = {};
}
var screen = document.createElement('div');
document.body.appendChild(screen);
var inst = renderComponent(cmp, screen, props);
try {
cb(inst, screen);
} finally {
document.body.removeChild(screen);
}
};
var withRenderedLayout = function (props, cb) {
if (arguments.length < 2) {
cb = props;
props = {};
}
var screen = document.createElement('div');
document.body.appendChild(screen);
var inst = renderLayout(screen, props);
try {
cb(inst, screen);
} finally {
document.body.removeChild(screen);
}
};
Tinytest.add('layout - rendering dynamic templates', function (test) {
withRenderedLayout({template: 'LayoutOne'}, function (layout, screen) {
test.equal(screen.innerHTML.trim(), 'one', 'initial layout template not rendered');
layout.template('LayoutTwo');
Deps.flush();
test.equal(screen.innerHTML.trim(), 'two', 'calling template method should change layout template');
});
});
Tinytest.add('layout - setting undefined template', function (test) {
withRenderedLayout({template: 'LayoutOne'}, function (layout, screen) {
test.equal(screen.innerHTML.trim(), 'one', 'initial layout template not rendered');
layout.template(undefined);
Deps.flush();
test.equal(screen.innerHTML.trim(), '', 'Should use default layout');
});
});
Tinytest.add('layout - dynamic data', function (test) {
withRenderedLayout({template: 'LayoutWithData'}, function (layout, screen) {
var renderCount = 1;
layout.rendered = function () {
renderCount++;
};
test.equal(renderCount, 1, 'layout should have only rendered once');
test.equal(screen.innerHTML.trim(), 'layout', 'initial layout not rendered');
layout.setData({title: 'test'});
Deps.flush();
test.equal(screen.innerHTML.trim(), 'layout test', 'layout data context not changed');
test.equal(renderCount, 1, 'layout should have only rendered once');
});
});
Tinytest.add('layout - yield into main region with default layout', function (test) {
withRenderedLayout(function (layout, screen) {
layout.setRegion('One');
Deps.flush();
test.equal(screen.innerHTML.trim(), 'one', 'could not render into main region with default layout');
});
});
Tinytest.add('layout - default main region using Layout template', function (test) {
withRenderedComponent(Template.DefaultMainRegion, function (cmp, screen) {
test.equal(screen.innerHTML.trim(), 'ok', 'default main region should be __content');
});
});
Tinytest.add('layout - dynamic yield regions', function (test) {
withRenderedLayout({template: 'LayoutWithTwoYields'}, function (layout, screen) {
var renderedCount = 1;
layout.rendered = function () { renderedCount++; };
var oneRenderCount = 0;
Template.One.rendered = function () { oneRenderCount++; };
layout.setRegion('One');
Deps.flush();
test.equal(screen.innerHTML.compact(), 'one', 'main region should be "one"');
test.equal(oneRenderCount, 1, 'template should have been rendered into layout');
// should be equivalent to above
layout.setRegion('main', 'One');
Deps.flush();
test.equal(screen.innerHTML.compact(), 'one', 'main region should be "one"');
test.equal(oneRenderCount, 1, 'template already rendered so should not be rendered again');
layout.setRegion('footer', 'Two');
Deps.flush();
test.equal(screen.innerHTML.compact(), 'onetwo', 'both yield regions should have rendered');
});
});
Tinytest.add('layout - contentFor helper', function (test) {
withRenderedLayout({template: 'LayoutWithTwoYields'}, function (layout, screen) {
layout.setRegion('ContentForTests');
Deps.flush();
test.equal(screen.innerHTML.compact(), 'mainfooter', 'contentFor helper should render into yield');
});
});
Tinytest.add('layout - global layout data context', function (test) {
withRenderedLayout({template: 'LayoutWithData'}, function (layout, screen) {
var layoutRenderCount = 1;
layout.rendered = function () { layoutRenderCount++; };
test.equal(screen.innerHTML.compact(), 'layout');
layout.setData({title:'1'});
Deps.flush();
test.equal(screen.innerHTML.compact(), 'layout1', 'data context should be set on layout');
test.equal(layoutRenderCount, 1, 'layout should not re-render');
layout.setData({title:'2'});
Deps.flush();
test.equal(screen.innerHTML.compact(), 'layout2', 'data context should be set on layout');
test.equal(layoutRenderCount, 1, 'layout should not re-render');
});
});
Tinytest.add('layout - data with yield regions', function (test) {
withRenderedLayout({template: 'LayoutWithDataAndYields'}, function (layout, screen) {
var layoutRenderCount = 1;
layout.rendered = function () { layoutRenderCount++; };
var childRenderCount = 0;
var footerRenderCount = 0;
Template.ChildWithData.rendered = function () { childRenderCount++; };
Template.FooterWithData.rendered = function () { footerRenderCount++; };
layout.setRegion('main', 'ChildWithData');
layout.setRegion('footer', 'FooterWithData');
Deps.flush();
test.equal(childRenderCount, 1);
test.equal(footerRenderCount, 1);
test.equal(layoutRenderCount, 1);
test.equal(screen.innerHTML.compact(), 'layoutchildfooter');
layout.setData({title:'1'});
Deps.flush();
test.equal(childRenderCount, 1);
test.equal(footerRenderCount, 1);
test.equal(layoutRenderCount, 1);
test.equal(screen.innerHTML.compact(), 'layout1child1footer1');
layout.setData({title:'2'});
Deps.flush();
test.equal(childRenderCount, 1);
test.equal(footerRenderCount, 1);
test.equal(layoutRenderCount, 1);
test.equal(screen.innerHTML.compact(), 'layout2child2footer2');
});
});
Tinytest.add('layout - layout template not found in lookup', function (test) {
var div = document.createElement('div');
document.body.appendChild(div);
try {
var layout;
test.throws(function () {
layout = renderComponent(Layout, div, {
template: 'SomeBogusTemplateThatDoesNotExist'
});
}, /BlazeLayout/); // this checks the error is a BlazeLayout error
} finally {
document.body.removeChild(div);
}
});
Tinytest.add('layout - region templates not found in lookup', function (test) {
var div = document.createElement('div');
document.body.appendChild(div);
try {
var layout = renderComponent(Layout, div);
test.throws(function () {
layout.setRegion('SomeBogusTemplate');
// _throwFirstError means it will actually throw
// instead of just logging to the console
Deps.flush({_throwFirstError: true});
}, /BlazeLayout/);
} finally {
document.body.removeChild(div);
}
});