-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencil.test.ts
180 lines (170 loc) · 4.74 KB
/
stencil.test.ts
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
const assert = require('assert')
const Stencil = require('../stencil')
describe('stencil test suite', () => {
it('should render nodes with the same name correctly', () => {
let actual = Stencil.render(
'{{#test}}{{#test}}{{test}}{{/test}}{{/test}}',
{
test: {
test: {
test: 'Hello World',
},
},
},
)
let expected = `Hello World`
assert.equal(actual, expected)
actual = Stencil.render(
'{{#test}}{{#test}}{{test.test}}{{/test}}{{/test}}',
{
test: {
test: {
test: {
test: 'Hello World',
},
},
},
},
)
assert.equal(actual, expected)
actual = Stencil.render(
'{{#test}}Test {{#test}}{{test.test}}{{/test}}{{/test}}',
{
test: {
test: {
test: {
test: 'Hello World',
},
},
},
},
)
expected = `Test Hello World`
assert.equal(actual, expected)
actual = Stencil.render(
'{{#test}}Test {{#test}}{{test.test}}{{/test}}{{/test}}{{#rest}}{{best}}{{/rest}}',
{
test: {
test: {
test: {
test: 'Hello World',
},
},
},
rest: {
best: ' - Best',
},
},
)
expected = `Test Hello World - Best`
assert.equal(actual, expected)
})
it('should render a simple template', () => {
const actual = Stencil.render(
'My name is {{firstName}} {{lastName}}!',
{ firstName: 'John', lastName: 'Doe' },
)
const expected = `My name is John Doe!`
assert.equal(actual, expected)
})
it('should render a view function', () => {
const view = {
firstName: 'John',
lastName: 'Doe',
fullName: function() {
return `${this.firstName} ${this.lastName}`
},
}
const actual = Stencil.render('My name is {{fullName}}!', view)
const expected = `My name is John Doe!`
assert.equal(actual, expected)
})
it('should render a view function with param', () => {
const view = {
firstName: 'John',
lastName: 'Doe',
fullName: function(firstName: string) {
return `${firstName} ${this.lastName}`
},
}
const actual = Stencil.render('My name is {{fullName Bob}}!', view)
const expected = `My name is Bob Doe!`
assert.equal(actual, expected)
})
it('should render a sub template within a variable', () => {
const view = {
logoText: `Logo`,
}
const templates = {
header: '<header>{{> logo}}</header>',
logo: '<span>{{logoText}}</span>',
nav: '<nav>{{> links}}</nav>',
links: '<ul><li>Link</li></ul>',
}
const template = `{{> header}}{{> nav}}`
const actual = Stencil.render(template, view, templates)
const expected = `<header><span>Logo</span></header><nav><ul><li>Link</li></ul></nav>`
assert.equal(actual, expected)
})
it('should render the filters correctly', () => {
let actual = Stencil.render(`{{name|ucwords}}`, {
name: `john doe`,
})
let expected = `John Doe`
assert.equal(actual, expected)
actual = Stencil.render(`{{name|upper}}`, {
name: `john doe`,
})
expected = `JOHN DOE`
assert.equal(actual, expected)
actual = Stencil.render(`{{name|lower}}`, {
name: `John Doe`,
})
expected = `john doe`
assert.equal(actual, expected)
actual = Stencil.render(`{{link|linkify}}`, {
link: `http://www.google.com`,
})
expected = `<a href="http://www.google.com" title="http://www.google.com">http://www.google.com</a>`
assert.equal(actual, expected)
actual = Stencil.render(`{{text|excerpt}}`, {
text: `This is a story about a developer who coded for over 20 years.
And despite the career, he worked under his peers, whom only had just a few years.
No matter the work, no matter the time, this developer just couldn't get another dime.
Was it his past, was it his pride, or was it the fact that he never lied?`,
})
expected = `This is a story about a developer who coded for over 20 years.
And despite the career, he worked under his peers, whom only had just a few years.
No matter the work, no matter the time, this developer just couldn't get another dime.
Was it his past,...`
assert.equal(actual, expected)
actual = Stencil.render(`{{text|stripTags}}`, {
text: `<a href=""></a><div><span><b>Tags are stripped</b></span></div>`,
})
expected = `Tags are stripped`
assert.equal(actual, expected)
})
it('should convert new lines into break tags', () => {
let actual = Stencil.render(`Test\nTest`, {}, undefined, {
newLineToBr: true,
})
let expected = `Test<br />\nTest`
assert.equal(actual, expected)
})
it('should allow me to use a custom filter', () => {
let stateAbbrev = (res: string): string => {
if (res === 'United States of America') {
return 'USA'
}
return ''
}
let actual = Stencil.render(
`{{state|stateAbbrev}}`,
{ state: 'United States of America' },
undefined,
{ filters: { stateAbbrev } },
)
let expected = 'USA'
assert.equal(actual, expected)
})
})