forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejs_test.js
More file actions
119 lines (100 loc) · 3.86 KB
/
ejs_test.js
File metadata and controls
119 lines (100 loc) · 3.86 KB
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
steal('funcunit/qunit','jquerypp/view/ejs', function(){
module("jquerypp/view/ejs, rendering",{
setup : function(){
this.animals = ['sloth', 'bear', 'monkey']
if(!this.animals.each){
this.animals.each = function(func){
for(var i =0; i < this.length; i++){
func(this[i])
}
}
}
this.squareBrackets = "<ul><% this.animals.each(function(animal){%>" +
"<li><%= animal %></li>" +
"<%});%></ul>"
this.squareBracketsNoThis = "<ul><% animals.each(function(animal){ %>" +
"<li><%= animal %></li>" +
"<%});%></ul>"
this.angleBracketsNoThis = "<ul><% animals.each(function(animal){%>" +
"<li><%= animal %></li>" +
"<%});%></ul>";
}
})
test("render with left bracket", function(){
var compiled = new $.EJS({text: this.squareBrackets}).render({animals: this.animals})
equals(compiled, "<ul><li>sloth</li><li>bear</li><li>monkey</li></ul>", "renders with bracket")
})
test("render with with", function(){
var compiled = new $.EJS({text: this.squareBracketsNoThis}).render({animals: this.animals}) ;
equals(compiled, "<ul><li>sloth</li><li>bear</li><li>monkey</li></ul>", "renders bracket with no this")
})
test("default carrot", function(){
var compiled = new $.EJS({text: this.angleBracketsNoThis}).render({animals: this.animals}) ;
equals(compiled, "<ul><li>sloth</li><li>bear</li><li>monkey</li></ul>")
})
test("render with double angle", function(){
var text = "<%% replace_me %>"+
"<ul><% animals.each(function(animal){%>" +
"<li><%= animal %></li>" +
"<%});%></ul>";
var compiled = new $.EJS({text: text}).render({animals: this.animals}) ;
equals(compiled, "<% replace_me %><ul><li>sloth</li><li>bear</li><li>monkey</li></ul>", "works")
});
test("comments", function(){
var text = "<%# replace_me %>"+
"<ul><% animals.each(function(animal){%>" +
"<li><%= animal %></li>" +
"<%});%></ul>";
var compiled = new $.EJS({text: text}).render({animals: this.animals}) ;
equals(compiled,"<ul><li>sloth</li><li>bear</li><li>monkey</li></ul>" )
});
test("multi line", function(){
var text = "a \n b \n c",
result = new $.EJS({text: text}).render({}) ;
equals(result, text)
})
test("escapedContent", function(){
var text = "<span><%= tags %></span><label>&</label><strong><%= number %></strong><input value='<%= quotes %>'/>";
var compiled = new $.EJS({text: text}).render({tags: "foo < bar < car > zar > poo",
quotes : "I use 'quote' fingers \"a lot\"",
number : 123}) ;
var div = $('<div/>').html(compiled)
equals(div.find('span').text(), "foo < bar < car > zar > poo" );
equals(div.find('strong').text(), 123 );
equals(div.find('input').val(), "I use 'quote' fingers \"a lot\"" );
equals(div.find('label').html(), "&" );
})
test("returning blocks", function(){
var somethingHelper = function(cb){
return cb([1,2,3,4])
}
var res = $.View("//jquerypp/view/ejs/test_template.ejs",{something: somethingHelper,
items: ['a','b']});
// make sure expected values are in res
//ok(/\s4\s/.test(res), "first block called" );
//equals(res.match(/ItemsLength4/g).length, 4, "innerBlock and each")
});
test("easy hookup", function(){
var div = $('<div/>').html("//jquerypp/view/ejs/easyhookup.ejs",{text: "yes"})
ok( div.find('div').hasClass('yes'), "has yes" )
});
test("helpers", function() {
$.EJS.Helpers.prototype.simpleHelper = function()
{
return 'Simple';
}
$.EJS.Helpers.prototype.elementHelper = function()
{
return function(el) {
el.innerHTML = 'Simple';
}
}
var text = "<div><%= simpleHelper() %></div>";
var compiled = new $.EJS({text: text}).render() ;
equals(compiled, "<div>Simple</div>");
text = "<div id=\"hookup\" <%= elementHelper() %>></div>";
compiled = new $.EJS({text: text}).render() ;
$('#qunit-test-area').append($(compiled));
equals($('#hookup').html(), "Simple");
});
})