forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_test.js
358 lines (295 loc) · 10.4 KB
/
view_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
(function() {
module("can/view");
test("multiple template types work", function(){
var expected = '<h3>helloworld</h3>';
can.each(["micro","ejs","jaml", "mustache"], function(ext){
var actual = can.view.render("//can/view/test/template." + ext, {
"message" :"helloworld"
}, {
helper: function(){
return "foo"
}
});
equal(can.trim(actual), expected, "Text rendered");
})
});
test("helpers work", function(){
var expected = '<h3>helloworld</h3><div>foo</div>';
can.each(["ejs", "mustache"], function(ext){
var actual = can.view.render("//can/view/test/helpers." + ext, {
"message" :"helloworld"
}, {
helper: function(){
return "foo"
}
});
equal(can.trim(actual), expected, "Text rendered");
})
});
test("buildFragment works right", function(){
can.append( can.$("#qunit-test-area"), can.view("//can/view/test//plugin.ejs",{}) )
ok(/something/.test( can.$("#something span")[0].firstChild.nodeValue ),"something has something");
can.remove( can.$("#something") );
});
test("async templates, and caching work", function(){
stop();
var i = 0;
can.view.render("//can/view/test//temp.ejs",{"message" :"helloworld"}, function(text){
ok(/helloworld\s*/.test(text), "we got a rendered template");
i++;
equal(i, 2, "Ajax is not synchronous");
start();
});
i++;
equal(i, 1, "Ajax is not synchronous")
})
test("caching works", function(){
// this basically does a large ajax request and makes sure
// that the second time is always faster
stop();
var startT = new Date(),
first;
can.view.render("//can/view/test//large.ejs",{"message" :"helloworld"}, function(text){
first = new Date();
ok(text, "we got a rendered template");
can.view.render("//can/view/test//large.ejs",{"message" :"helloworld"}, function(text){
var lap2 = (new Date()) - first,
lap1 = first-startT;
// ok( lap1 > lap2, "faster this time "+(lap1 - lap2) )
start();
})
})
})
test("hookup", function(){
can.view("//can/view/test//hookup.ejs",{});
equal(window.hookedUp, 'dummy', 'Hookup ran and got element');
});
test("inline templates other than 'tmpl' like ejs", function(){
var script = document.createElement('script');
script.setAttribute('type', 'test/ejs')
script.setAttribute('id', 'test_ejs')
script.text = '<span id="new_name"><%= name %></span>';
document.getElementById("qunit-test-area").appendChild(script);
var div = document.createElement('div');
div.appendChild(can.view('test_ejs', {name: 'Henry'}))
equal( div.getElementsByTagName("span")[0].firstChild.nodeValue , 'Henry');
});
//canjs issue #31
test("render inline templates with a #", function(){
var script = document.createElement('script');
script.setAttribute('type', 'test/ejs')
script.setAttribute('id', 'test_ejs')
script.text = '<span id="new_name"><%= name %></span>';
document.getElementById("qunit-test-area").appendChild(script);
var div = document.createElement('div');
div.appendChild(can.view('#test_ejs', {name: 'Henry'}));
//make sure we aren't returning the current document as the template
equal(div.getElementsByTagName("script").length, 0, "Current document was not used as template")
if(div.getElementsByTagName("span").length === 1) {
equal( div.getElementsByTagName("span")[0].firstChild.nodeValue , 'Henry');
}
});
test("object of deferreds", function(){
var foo = new can.Deferred(),
bar = new can.Deferred();
stop();
can.view.render("//can/view/test//deferreds.ejs",{
foo : typeof foo.promise == 'function' ? foo.promise() : foo,
bar : bar
}).then(function(result){
equal(result, "FOO and BAR");
start();
});
setTimeout(function(){
foo.resolve("FOO");
},100);
bar.resolve("BAR");
});
test("deferred", function(){
var foo = new can.Deferred();
stop();
can.view.render("//can/view/test//deferred.ejs",foo).then(function(result){
equal(result, "FOO");
start();
});
setTimeout(function(){
foo.resolve({
foo: "FOO"
});
},100);
});
test("hyphen in type", function(){
var script = document.createElement('script');
script.setAttribute('type', 'text/x-ejs')
script.setAttribute('id', 'hyphenEjs')
script.text = '\nHyphen\n';
document.getElementById("qunit-test-area").appendChild(script);
var div = document.createElement('div');
div.appendChild(can.view('hyphenEjs', {}))
ok( /Hyphen/.test(div.innerHTML) , 'has hyphen');
})
test("create template with string", function(){
can.view.ejs("fool", "everybody plays the <%= who %> <%= howOften %>");
var div = document.createElement('div');
div.appendChild(can.view('fool', {who: "fool", howOften: "sometimes"}));
ok( /fool sometimes/.test(div.innerHTML) , 'has fool sometimes'+div.innerHTML);
})
test("return renderer", function() {
var directResult = can.view.ejs('renderer_test', "This is a <%= test %>");
var renderer = can.view('renderer_test');
ok(can.isFunction(directResult), 'Renderer returned directly');
ok(can.isFunction(renderer), 'Renderer is a function');
equal(renderer({ test : 'working test' }), 'This is a working test', 'Rendered');
renderer = can.view("//can/view/test//template.ejs");
ok(can.isFunction(renderer), 'Renderer is a function');
equal(renderer({ message : 'Rendered!' }), '<h3>Rendered!</h3>', 'Synchronous template loaded and rendered');
// TODO doesn't get caught in Zepto for whatever reason
// raises(function() {
// can.view('jkflsd.ejs');
// }, 'Nonexistent template throws error');
});
test("nameless renderers (#162, #195)", 8, function() {
// EJS
var nameless = can.view.ejs('<h2><%= message %></h2>'),
data = {
message : 'HI!'
},
result = nameless(data),
node = result.childNodes[0];
ok('ownerDocument' in result, 'Result is a document fragment');
equal(node.tagName.toLowerCase(), 'h2', 'Got h2 rendered');
equal(node.innerHTML, data.message, 'Got EJS result rendered');
equal(nameless.render(data), '<h2>HI!</h2>', '.render EJS works and returns HTML');
// Mustache
nameless = can.view.mustache('<h3>{{message}}</h3>');
data = {
message : 'MUSTACHE!'
};
result = nameless(data);
node = result.childNodes[0]
ok('ownerDocument' in result, 'Result is a document fragment');
equal(node.tagName.toLowerCase(), 'h3', 'Got h3 rendered');
equal(node.innerHTML, data.message, 'Got Mustache result rendered');
equal(nameless.render(data), '<h3>MUSTACHE!</h3>', '.render Mustache works and returns HTML');
});
test("deferred resolves with data (#183, #209)", function(){
var foo = new can.Deferred();
var bar = new can.Deferred();
var original = {
foo : foo,
bar : bar
};
stop();
ok(can.isDeferred(original.foo), 'Original foo property is a Deferred');
can.view("//can/view/test//deferred.ejs", original).then(function(result, data){
ok(data, 'Data exists');
equal(data.foo, 'FOO', 'Foo is resolved');
equal(data.bar, 'BAR', 'Bar is resolved');
ok(can.isDeferred(original.foo), 'Original property did not get modified');
start();
});
setTimeout(function(){
foo.resolve('FOO');
},100);
setTimeout(function() {
bar.resolve('BAR');
}, 50);
});
test("Empty model displays __!!__ as input values (#196)", function() {
can.view.ejs("test196", "User id: <%= user.attr('id') || '-' %>" +
" User name: <%= user.attr('name') || '-' %>");
var frag = can.view('test196', {
user: new can.Observe()
});
var div = document.createElement('div');
div.appendChild(frag);
equal(div.innerHTML, 'User id: - User name: -', 'Got expected HTML content');
can.view('test196', {
user : new can.Observe()
}, function(frag) {
div = document.createElement('div');
div.appendChild(frag);
equal(div.innerHTML, 'User id: - User name: -', 'Got expected HTML content in callback as well');
});
});
test("Select live bound options don't contain __!!__", function() {
var domainList = new can.Observe.List([{
id: 1,
name: 'example.com'
}, {
id: 2,
name: 'google.com'
}, {
id: 3,
name: 'yahoo.com'
}, {
id: 4,
name: 'microsoft.com'
}]),
frag = can.view("//can/view/test/select.ejs", {
domainList: domainList
}),
div = document.createElement('div');
div.appendChild(frag);
can.append( can.$("#qunit-test-area"), div)
equal(div.outerHTML.match(/__!!__/g), null, 'No __!!__ contained in HTML content')
//equal(can.$('#test-dropdown')[0].outerHTML, can.$('#test-dropdown2')[0].outerHTML, 'Live bound select and non-live bound select the same');
});
test('Live binding on number inputs', function(){
var template = can.view.ejs('<input id="candy" type="number" value="<%== state.attr("number") %>" />');
var observe = new can.Observe({ number : 2 });
var frag = template({ state: observe });
can.append(can.$("#qunit-test-area"), frag);
var input = document.getElementById('candy');
equal(input.getAttribute('value'), 2, 'render workered');
observe.attr('number', 5);
equal(input.getAttribute('value'), 5, 'update workered');
})
test("Resetting a live-bound <textarea> changes its value to __!!__ (#223)", function() {
var template = can.view.ejs("<form><textarea><%= this.attr('test') %></textarea></form>"),
frag = template(new can.Observe({
test : 'testing'
})),
form, textarea;
can.append(can.$("#qunit-test-area"), frag);
form = document.getElementById('qunit-test-area').getElementsByTagName('form')[0];
textarea = form.children[0];
equal(textarea.value, 'testing', 'Textarea value set');
textarea.value = 'blabla';
equal(textarea.value, 'blabla', 'Textarea value updated');
form.reset();
equal(form.children[0].value, 'testing', 'Textarea value set back to original live-bound value');
});
test("Deferred fails (#276)", function(){
var foo = new can.Deferred();
stop();
can.view.render("//can/view/test//deferred.ejs",foo)
.fail(function(error) {
equal(error.message, 'Deferred error');
start();
});
setTimeout(function(){
foo.reject({
message: 'Deferred error'
})
},100);
});
test("Object of deferreds fails (#276)", function() {
var foo = new can.Deferred(),
bar = new can.Deferred();
stop();
can.view.render("//can/view/test//deferreds.ejs",{
foo : typeof foo.promise == 'function' ? foo.promise() : foo,
bar : bar
}).fail(function(error){
equal(error.message, 'foo error');
start();
});
setTimeout(function(){
foo.reject({
message: 'foo error'
});
},100);
bar.resolve('Bar done');
});
})();