-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlayout_test.js
331 lines (268 loc) · 9.93 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
String.prototype.compact = function () {
return this.trim().replace(/\s/g, '').replace(/\n/g, '');
};
var ReactiveVar = function (value) {
this._value = value;
this._dep = new Deps.Dependency;
};
ReactiveVar.prototype.get = function () {
this._dep.depend();
return this._value;
};
ReactiveVar.prototype.set = function (value) {
if (value !== this._value) {
this._value = value;
this._dep.changed();
}
};
ReactiveVar.prototype.clear = function () {
this._value = null;
this._dep = new Deps.Dependency;
};
// a reactive template variable we can use
var reactiveTemplate = new ReactiveVar;
// a reactive data variable we can use
var reactiveData = new ReactiveVar;
var withDiv = function (callback) {
var el = document.createElement('div');
document.body.appendChild(el);
try {
callback(el);
} finally {
document.body.removeChild(el);
}
};
var withRenderedTemplate = function (template, callback) {
withDiv(function (el) {
template = _.isString(template) ? Template[template] : template;
Blaze.render(template, el);
Deps.flush();
callback(el);
});
};
Tinytest.add('Layout - Template layout with block content', function (test) {
withRenderedTemplate('BlockLayout', function (el) {
test.equal(el.innerHTML.compact(), 'layout-main-footer');
});
});
Template.BlockLayoutWithData.helpers({
getData: function () {
return 'data';
}
});
Tinytest.add('Layout - Template layout with block content and data', function (test) {
withRenderedTemplate('BlockLayoutWithData', function (el) {
test.equal(el.innerHTML.compact(), 'layout-data-main-data-footer-data');
});
});
Template.BlockLayoutWithOuterData.data = null;
Template.BlockLayoutWithOuterData.helpers({
getData: function () {
return Template.BlockLayoutWithOuterData.data;
}
});
Tinytest.add('Layout - data - yield inherits data from outside by default', function (test) {
Template.BlockLayoutWithOuterData.data = undefined;
withRenderedTemplate('BlockLayoutWithOuterData', function (el) {
test.equal(el.innerHTML.compact(), 'layout-outerData-inner-outerData');
});
});
Tinytest.add('Layout - data - yield inherits data layout if defined', function (test) {
Template.BlockLayoutWithOuterData.data = 'layoutData';
withRenderedTemplate('BlockLayoutWithOuterData', function (el) {
test.equal(el.innerHTML.compact(), 'layout-layoutData-inner-layoutData');
});
});
Template.BlockLayoutWithOuterDataAndWith.data = null;
Template.BlockLayoutWithOuterDataAndWith.helpers({
getData: function () {
return Template.BlockLayoutWithOuterDataAndWith.data;
}
});
Tinytest.add('Layout - data - with in context trumps all else', function (test) {
Template.BlockLayoutWithOuterDataAndWith.data = undefined;
withRenderedTemplate('BlockLayoutWithOuterDataAndWith', function (el) {
test.equal(el.innerHTML.compact(), 'layout-outerData-inner-innerData');
});
});
Tinytest.add('Layout - data - with in context trumps layout data', function (test) {
Template.BlockLayoutWithOuterDataAndWith.data = 'layoutData';
withRenderedTemplate('BlockLayoutWithOuterDataAndWith', function (el) {
test.equal(el.innerHTML.compact(), 'layout-layoutData-inner-innerData');
});
});
Tinytest.add('Layout - data - render always clears data', function (test) {
var layout = new Iron.Layout;
withRenderedTemplate(layout.create(), function (el) {
layout.template('LayoutOne');
layout.render('One', {data: 'firstData'});
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-One-firstData-');
// rendering with no data, should now not have data
layout.render('One')
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-One--');
});
});
// see https://github.com/EventedMind/iron-router/issues/693
Tinytest.add('Layout - data - in-place changes to data are not missed', function(test) {
var layout = new Iron.Layout;
var Posts = new Meteor.Collection(null);
Posts.insert({foo: 'bar'});
withRenderedTemplate(layout.create(), function (el) {
layout.template('LayoutOne');
layout.data(function() {
return Posts.findOne();
});
layout.render('OneFoo');
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-One-bar-');
var post = layout.data(); // grab the internal reference, if exists
post.foo = 'baz';
Posts.update(post._id, {$set: {foo: post.foo}});
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-One-baz-');
});
});
Tinytest.add('Layout - JavaScript layout', function (test) {
var layout = new Iron.Layout;
withRenderedTemplate(layout.create(), function (el) {
// starts off empty
test.equal(el.innerHTML.compact(), '');
// then we'll choose a layout
layout.template('LayoutWithData');
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout--');
// render the One template into the main region
layout.render('One');
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout--One--');
// now render Two into the footer region
layout.render('Two', {to: 'footer'});
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout--One--Two--');
// now set a global layout data context!
layout.data('DATA');
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-DATA-One-DATA-Two-DATA-');
// and finally let's override some specific region data contexts!
layout.render('One', {data: 'ONE'});
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-DATA-One-ONE-Two-DATA-');
layout.render('Two', {to: 'footer', data: 'TWO'});
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-DATA-One-ONE-Two-TWO-');
});
});
Tinytest.add('Layout - JavaScript rendering transactions', function (test) {
var layout = new Iron.Layout({template: 'RenderingTransactionsLayout'});
withRenderedTemplate(layout.create(), function (el) {
// start the transaction and provide an oncomplete callback
var calls = [];
layout.beginRendering(function onComplete(regions) {
calls.push({
regions: regions
});
});
// render the LayoutOnePage template into the main region
// this should also render "footer" through a contentFor block
layout.render('LayoutOnePage');
layout.render('Aside', {to: 'aside'});
Deps.flush();
test.equal(calls.length, 1);
test.equal(calls[0].regions, ['main', 'aside', 'footer']);
});
});
Tinytest.add('Layout - rendering transactions multiple calls to beginRendering', function (test) {
var layout = new Iron.Layout({template: 'RenderingTransactionsLayout'});
withRenderedTemplate(layout.create(), function (el) {
// start the transaction and provide an oncomplete callback
var calls = [];
var onComplete = function onComplete (regions) {
calls.push({
regions: regions
});
};
layout.beginRendering(onComplete);
// render the LayoutOnePage template into the main region
// this should also render "footer" through a contentFor block
layout.render('LayoutOnePage');
layout.render('Aside', {to: 'aside'});
// now before we flush call beginRendering again. the previous transaction
// should immediately complete
layout.beginRendering(onComplete);
// this time our onComplete should only register the main region being
// rendered.
layout.render('LayoutOnePage');
// now actually flush
Deps.flush();
test.equal(calls.length, 2, "onComplete called for both rendering transactions");
// first time, the rendered regions only include the programmatic ones
// because the contentFor doesn't have a change to run.
var regions = calls[0].regions;
test.equal(regions.length, 2);
test.isTrue(_.contains(regions, "main"));
test.isTrue(_.contains(regions, "aside"));
var regions = calls[1].regions;
test.equal(regions.length, 2);
test.isTrue(_.contains(regions, "main"));
test.isTrue(_.contains(regions, "footer"));
});
});
Tinytest.add('Layout - has, clear and clearAll', function (test) {
var layout = new Iron.Layout({template: 'LayoutOne'});
withRenderedTemplate(layout.create(), function (el) {
// just make sure we're starting off correctly
test.equal(el.innerHTML.compact(), 'layout-');
// we have a main region {{> yield}}
test.isTrue(layout.has());
// we also should have a footer region {{> yield "footer"}}
test.isTrue(layout.has('footer'));
// we should not have some bogus region
test.isFalse(layout.has('bogus'));
// render the One template into the main region
layout.render('One');
Deps.flush();
// now render Two into the footer region
layout.render('Two', {to: 'footer'});
Deps.flush();
// make sure everything rendered correctly
test.equal(el.innerHTML.compact(), 'layout-One--Two--');
// now clear footer region
layout.clear("footer");
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-One--');
// clear main region
layout.clear();
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-');
// now build it back up again and clearAll
layout.render('One');
layout.render('Two', {to: 'footer'});
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-One--Two--');
layout.clearAll();
Deps.flush();
test.equal(el.innerHTML.compact(), 'layout-');
});
});
Tinytest.add('Layout - default layout', function (test) {
// no default template
var layout = new Iron.Layout;
withRenderedTemplate(layout.create(), function (el) {
layout.render('Plain');
Deps.flush();
test.equal(el.innerHTML.compact(), 'plain', 'no default layout');
});
});
Tinytest.add('Layout - hasRegion', function (test) {
var layout = new Iron.Layout;
withRenderedTemplate(layout.create(), function (el) {
layout.template('LayoutWithHasRegion');
Deps.flush();
test.equal(el.innerHTML.compact(), 'no', 'hasRegion mis-reported true');
layout.render('One', {to: 'test'});
Deps.flush();
test.equal(el.innerHTML.compact(), 'yes', 'hasRegion mis-reported false');
});
});