This repository was archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrenderer-test.js
300 lines (298 loc) · 14.5 KB
/
renderer-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
// Disable Prettier
// @ts-nocheck
const { html: h, renderToStream, renderToString } = require('../index.js');
const { createAsyncIterable } = require('./utils.js');
const { expect } = require('chai');
const getStream = require('get-stream');
describe('Server template render', () => {
describe('text', () => {
it('should render a plain text template', async () => {
const result = () => h`text`;
const expected = 'text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with text value', async () => {
const result = () => h`${'text'}`;
const expected = 'text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with number value', async () => {
const result = () => h`${1}`;
const expected = '1';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with boolean value', async () => {
const result = () => h`${true}`;
const expected = 'true';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with null value', async () => {
const result = () => h`${null}`;
const expected = 'null';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with undefined value', async () => {
const result = () => h`${undefined}`;
const expected = '';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with array value', async () => {
const result = () => h`${[1, 2, 3]}`;
const expected = '123';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with deeply nested array value', async () => {
const result = () => h`${[1, 2, [3, [4, 5]]]}`;
const expected = '12345';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with sync iterator value', async () => {
const array = ['hello ', 'there ', 'world', [", how's ", 'it ', 'going']];
const result = () => h`Well ${array[Symbol.iterator]()}?`;
const expected = 'Well hello there world, how's it going?';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with nested template value', async () => {
const result = () => h`some ${h`text`}`;
const expected = 'some text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with array of nested template values', async () => {
const result = () => h`some ${[1, 2, 3].map((i) => h`${i}`)} text`;
const expected = 'some 123 text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with Promise value', async () => {
const result = () => h`${Promise.resolve('some')} text`;
const expected = 'some text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with Promise template value', async () => {
const result = () => h`${Promise.resolve(h`some`)} text`;
const expected = 'some text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should not render a template with Promise errors', async () => {
const result = () => h`${Promise.reject(Error('errored!'))}`;
try {
const html = await renderToString(result());
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
try {
const html = await getStream(renderToStream(result()));
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
});
it('should not render a template with Promises that throw errors', async () => {
const result = () =>
h`${new Promise(() => {
throw Error('errored!');
})}`;
try {
const html = await renderToString(result());
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
try {
const html = await getStream(renderToStream(result()));
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
});
it('should render a template with AsyncIterator value', async () => {
const result = () => h`${createAsyncIterable(['some', ' async'])} text`;
const expected = 'some async text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with AsyncIterator template value', async () => {
const result = () => h`${createAsyncIterable([h`some`, h` async`])} text`;
const expected = 'some async text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with deeply nested sync/async templates', async () => {
const data = { title: 'title', body: 'this is body text' };
const nestedVeryDeep = async () => ['and ', "don't ", 'forget ', ['this']];
const nestedDeep = async () => h`<div>this too ${nestedVeryDeep()}</div>`;
const nested = async (body) => h`<div>${body} ${nestedDeep()}</div>`;
const result = () => h`<main><h1>${data.title}</h1>${nested(data.body)}</main>`;
const expected =
'<main><h1>title</h1><div>this is body text <div>this too and don't forget this</div></div></main>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should allow multiple renders of existing template results', async () => {
const result = h`text`;
const expected = 'text';
expect(await getStream(renderToStream(result))).to.equal(expected);
expect(await getStream(renderToStream(result))).to.equal(expected);
});
it('should render plain string result', async () => {
const result = () => 'text';
const expected = 'text';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
});
describe('attributes', () => {
it('should render a template with quoted text attribute', async () => {
const value = 'text';
const result = () => h`<div a="${value}"></div>`;
const expected = '<div a="text"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with quoted array attribute', async () => {
const value = [1, 2, 3];
const result = () => h`<div a="${value}"></div>`;
const expected = '<div a="123"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with unquoted attribute', async () => {
const value = 'text';
const result = () => h`<div a=${value}></div>`;
const expected = '<div a="text"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with quoted attribute and extra whitespace', async () => {
const value = 'text';
const result = () => h`<div a = " ${value} "></div>`;
const expected = '<div a=" text "></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with quoted attribute and extra strings', async () => {
const value = 'text';
const result = () => h`<div a="some ${value}"></div>`;
const expected = '<div a="some text"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with quoted attribute and multiple strings/values', async () => {
const value = 'text';
const result = () => h`<div a="this is ${'some'} ${value}">${'node'}</div>`;
const expected = '<div a="this is some text">node</div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with boolean attribute', async () => {
const value = true;
const result = () => h`<div ?a="${value}"></div>`;
const expected = '<div a></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with event attribute', async () => {
const result = () => h`<div @a="${'some event'}"></div>`;
const expected = '<div ></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with property attribute', async () => {
const result = () => h`<div .a="${'some prop'}"></div>`;
const expected = '<div ></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should not render a template with nested template attribute', async () => {
const result = () => h`<div a="some ${h`text`}"></div>`;
const expected = '<div a="some [object Object]"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should not render a template with nested template array attribute', async () => {
const result = () => h`<div a="some ${[h`1`, h`2`, h`3`]}"></div>`;
const expected = '<div a="some [object Object][object Object][object Object]"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with Promise text attribute', async () => {
const result = () => h`<div a="some ${Promise.resolve('text')} here"></div>`;
const expected = '<div a="some text here"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with Promise array attribute', async () => {
const result = () => h`<div a="some ${Promise.resolve([1, 2, 3])} here"></div>`;
const expected = '<div a="some 123 here"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with Promise template attribute', async () => {
const result = () => h`<div a="some ${Promise.resolve('text')} here"></div>`;
const expected = '<div a="some text here"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should render a template with multiple Promise templates attribute', async () => {
const result = () =>
h`<div a="some ${Promise.resolve('text')} here ${Promise.resolve('too')}"></div>`;
const expected = '<div a="some text here too"></div>';
expect(await renderToString(result())).to.equal(expected);
expect(await getStream(renderToStream(result()))).to.equal(expected);
});
it('should not render a template attribute with Promise errors', async () => {
const result = () => h`<div a="some ${Promise.reject(Error('errored!'))}"></div>`;
try {
const html = await renderToString(result());
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
try {
const html = await getStream(renderToStream(result()));
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
});
it('should not render a template attribute with Promise that throws errors', async () => {
const result = () =>
h`<div a="some ${new Promise(() => {
throw Error('errored!');
})}"></div>`;
try {
const html = await renderToString(result());
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
try {
const html = await getStream(renderToStream(result()));
expect(html).to.not.exist;
} catch (err) {
expect(err).to.have.property('message', 'errored!');
}
});
it('should render a template with stringified property object when options.serializePropertyAttributes', async () => {
const value = { some: 'text' };
const result = () => h`<div .a="${value}"></div>`;
const expected = '<div .a="{"some":"text"}"></div>';
const options = { serializePropertyAttributes: true };
expect(await renderToString(result(), options)).to.equal(expected);
expect(await getStream(renderToStream(result(), options))).to.equal(expected);
});
});
});