-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexists.js
216 lines (215 loc) · 6.41 KB
/
exists.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
let suite = {
name: 'Exists',
tests: [
{
description: 'Exists alone (no other HTML), with text inside.',
template: '{?name}name exists{/name}',
context: {name: 'Dorothy'},
expectedHTML: 'name exists'
},
{
description: 'Exists alone with reference inside',
template: '{?name}{name} exists{/name}',
context: {name: 'Dorothy'},
expectedHTML: 'Dorothy exists'
},
{
description: 'Exists with HTML inside',
template: '{?name}<div>Hello, <span>world</span></div>{/name}',
context: {name: 'Dorothy'},
expectedHTML: '<div>Hello, <span>world</span></div>'
},
{
description: 'Exists inside HTML',
template: '<div>Hello, {?name}<span>world</span>{/name}</div>',
context: {name: 'Dorothy'},
expectedHTML: '<div>Hello, <span>world</span></div>'
},
{
description: 'Exists in an attribute',
template: '<div class="{?isSelected}selected{/isSelected}"></div>',
context: {isSelected: true},
expectedHTML: '<div class="selected"></div>'
},
{
description: 'Exists in an attribute with an else',
template: '<div class="{?isSelected}selected{:else}not-selected{/isSelected}"></div>',
context: {isSelected: false},
expectedHTML: '<div class="not-selected"></div>'
},
{
description: 'Exists where reference is 0',
template: 'Hello, {?zero}world{/zero}',
context: {zero: 0},
expectedHTML: 'Hello, world'
},
{
description: 'Exists where reference is empty string',
template: 'Hello, {?name}world{/name}',
context: {name: ''},
expectedHTML: 'Hello, '
},
{
description: 'Exists where reference is empty array',
template: 'Hello, {?name}world{/name}',
context: {name: []},
expectedHTML: 'Hello, '
},
{
description: 'Exists where reference is false',
template: 'Hello, {?name}world{/name}',
context: {name: false},
expectedHTML: 'Hello, '
},
{
description: 'Exists with else where reference is truthy',
template: 'Hello, {?name}world{:else}abyss{/name}',
context: {name: true},
expectedHTML: 'Hello, world'
},
{
description: 'Exists containing multiple HTML elements, followd by refernece',
template: 'Hello, {?name}<span>world</span>.{/name} My name is {name}.',
context: {name: 'Dorothy'},
expectedHTML: 'Hello, <span>world</span>. My name is Dorothy.'
},
{
description: 'Exists with else where reference is falsy',
template: 'Hello, {?name}world{:else}abyss{/name}',
context: {name: false},
expectedHTML: 'Hello, abyss'
},
{
description: 'Exists where reference has dots',
template: 'Hello, {?member.lastName}fullName{/member.lastName}',
context: {member: { lastName: 'Smith'} },
expectedHTML: 'Hello, fullName'
},
{
description: 'Exists where reference is a promise',
template: 'Hello, {?now}later{/now}',
context: {
now: new Promise((resolve) => {
resolve('and later');
})
},
expectedHTML: 'Hello, later'
},
{
description: 'Exists where reference is a promise that resolves to a falsy value',
template: 'Hello, {?now}later{/now}',
context: {
now: new Promise((resolve) => {
resolve('');
})
},
expectedHTML: 'Hello, '
},
{
description: 'Exists with an else where reference is a promise that resolves to a falsy value',
template: 'Hello, {?now}later{:else}never{/now}',
context: {
now: new Promise((resolve) => {
resolve('');
})
},
expectedHTML: 'Hello, never'
},
{
description: 'Exists with a pending where reference is a promise',
template: 'Hello, {?now}later{:pending}coming soon{/now}',
context: {
now: function() {
return new Promise((resolve) => {
setTimeout(function() {
resolve(true);
}, 100);
});
}
},
expectedHTML: 'Hello, <div class="tornado-pending">coming soon</div>',
expectedHTMLResolved: 'Hello, later'
},
{
description: 'Exists with an else where reference is a promise that rejects',
template: 'Hello, {?now}later{:else}never{/now}',
context: {
now: new Promise((resolve, reject) => {
reject();
})
},
expectedHTML: 'Hello, never'
},
{
description: 'Exists with sibling exists',
template: '<div>{?brother}brother{/brother}</div><div>{?sister} and sister{/sister}</div>',
context: {
brother: true,
sister: true
},
expectedHTML: '<div>brother</div><div> and sister</div>'
},
{
description: 'Exists with sibling exists in attribute',
template: '<div class="{?brother}brother{/brother}{?sister} sister{/sister}"></div>',
context: {
brother: true,
sister: true
},
expectedHTML: '<div class="brother sister"></div>'
},
{
description: 'Exists where reference is a function',
template: '{?fun}Way fun!{/fun}',
context: {
fun: function() {
return true;
}
},
expectedHTML: 'Way fun!'
},
{
description: 'Exists where dotted reference contains a function',
template: '{?fun.fun}Way fun!{/fun.fun}',
context: {
fun: function() {
return {
fun: true
};
}
},
expectedHTML: 'Way fun!'
},
{
description: 'Exists where reference is a function that returns a Promise',
template: '{?fun}Way fun!{/fun}',
context: {
fun: function() {
return new Promise(function(resolve) {
resolve(true);
});
}
},
expectedHTML: 'Way fun!'
},
{
description: 'Exists where dotted reference contains a function that returns a Promise',
template: '{?fun.fun}Way fun!{/fun.fun}',
context: {
fun: function() {
return new Promise(function(resolve) {
resolve({fun: true});
});
}
},
expectedHTML: 'Way fun!'
},
{
description: 'Exists using helper syntax ( {@exists key="key"}...{/exists} )',
template: '{@exists key="name"}name exists{/exists}',
context: {name: 'Dorothy'},
expectedHTML: 'name exists'
}
]
};
export default suite;