-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathvisit-test.js
More file actions
357 lines (304 loc) · 8.75 KB
/
visit-test.js
File metadata and controls
357 lines (304 loc) · 8.75 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
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
const SimpleDOM = require('simple-dom');
const setupAppTest = require('./helpers/setup-app');
function assertHTMLMatches(assert, actualHTML, expectedHTML) {
assert.ok(actualHTML.match(expectedHTML), actualHTML + ' matches ' + expectedHTML);
}
function handleError(assert) {
return function (error) {
assert.ok(false, error.stack);
};
}
// This is based on what fastboot-server does
let HTMLSerializer = new SimpleDOM.HTMLSerializer(SimpleDOM.voidMap);
function fastbootVisit(App, url) {
let doc = new SimpleDOM.Document();
let rootElement = doc.body;
let options = { isBrowser: false, document: doc, rootElement: rootElement };
return App.visit(url, options).then(function (instance) {
try {
return {
url: instance.getURL(),
title: doc.title,
body: HTMLSerializer.serialize(rootElement),
};
} finally {
instance.destroy();
}
});
}
function assertFastbootResult(assert, expected) {
return function (actual) {
assert.equal(actual.url, expected.url);
assertHTMLMatches(assert, actual.body, expected.body);
};
}
QUnit.module('Ember.Application - visit() Integration Tests', function (hooks) {
setupAppTest(hooks);
QUnit.test('FastBoot: basic', function (assert) {
this.routes(function () {
this.route('a');
this.route('b');
});
this.template('application', '<h1>Hello world</h1>\n{{outlet}}');
this.template('a', '<h2>Welcome to {{x-foo page="A"}}</h2>');
this.template('b', '<h2>{{x-foo page="B"}}</h2>');
let initCalled = false;
let didInsertElementCalled = false;
this.component(
'x-foo',
{
tagName: 'span',
init: function () {
this._super();
initCalled = true;
},
didInsertElement: function () {
didInsertElementCalled = true;
},
},
'Page {{this.page}}'
);
let App = this.createApplication();
return Promise.all([
fastbootVisit(App, '/a').then(
assertFastbootResult(assert, {
url: '/a',
body: '<h1>Hello world</h1>\n<h2>Welcome to <span id=".+" class="ember-view">Page A</span></h2>',
}),
handleError(assert)
),
fastbootVisit(App, '/b').then(
assertFastbootResult(assert, {
url: '/b',
body: '<h1>Hello world</h1>\n<h2><span id=".+" class="ember-view">Page B</span></h2>',
}),
handleError
),
]).then(function () {
assert.ok(initCalled, 'Component#init should be called');
assert.notOk(didInsertElementCalled, 'Component#didInsertElement should not be called');
});
});
QUnit.test('FastBoot: redirect', function (assert) {
this.routes(function () {
this.route('a');
this.route('b');
this.route('c');
});
this.template('a', '<h1>Hello from A</h1>');
this.template('b', '<h1>Hello from B</h1>');
this.template('c', '<h1>Hello from C</h1>');
this.route('a', {
beforeModel: function () {
this.router.replaceWith('b');
},
});
this.route('b', {
afterModel: function () {
this.router.transitionTo('c');
},
});
let App = this.createApplication();
return Promise.all([
fastbootVisit(App, '/a').then(
assertFastbootResult(assert, {
url: '/c',
body: '<h1>Hello from C</h1>',
}),
handleError(assert)
),
fastbootVisit(App, '/b').then(
assertFastbootResult(assert, {
url: '/c',
body: '<h1>Hello from C</h1>',
}),
handleError(assert)
),
]);
});
QUnit.test('FastBoot: attributes are sanitized', function (assert) {
this.template('application', '<a href={{this.test}}></a>');
this.controller('application', {
test: 'javascript:alert("hello")',
});
let App = this.createApplication();
return Promise.all([
fastbootVisit(App, '/').then(
assertFastbootResult(assert, {
url: '/',
body: '<a href="unsafe:javascript:alert\\("hello"\\)"></a>',
}),
handleError(assert)
),
]);
});
QUnit.test('FastBoot: route error', function (assert) {
this.routes(function () {
this.route('a');
this.route('b');
});
this.template('a', '<h1>Hello from A</h1>');
this.template('b', '<h1>Hello from B</h1>');
this.route('a', {
beforeModel: function () {
throw new Error('Error from A');
},
});
this.route('b', {
afterModel: function () {
throw new Error('Error from B');
},
});
let App = this.createApplication();
return Promise.all([
fastbootVisit(App, '/a').then(
function (instance) {
assert.ok(false, 'It should not render');
instance.destroy();
},
function (error) {
assert.equal(error.message, 'Error from A');
}
),
fastbootVisit(App, '/b').then(
function (instance) {
assert.ok(false, 'It should not render');
instance.destroy();
},
function (error) {
assert.equal(error.message, 'Error from B');
}
),
]);
});
QUnit.test('FastBoot: route error template', function (assert) {
this.routes(function () {
this.route('a');
});
this.template('error', '<p>Error template rendered!</p>');
this.template('a', '<h1>Hello from A</h1>');
this.route('a', {
model: function () {
throw new Error('Error from A');
},
});
let App = this.createApplication();
return Promise.all([
fastbootVisit(App, '/a').then(
assertFastbootResult(assert, {
url: '/a',
body: '<p>Error template rendered!</p>',
}),
handleError(assert)
),
]);
});
QUnit.test('Resource-discovery setup', function (assert) {
class Network {
constructor() {
this.requests = [];
}
fetch(url) {
this.requests.push(url);
return Promise.resolve();
}
}
this.routes(function () {
this.route('a');
this.route('b');
this.route('c');
this.route('d');
this.route('e');
});
let network;
this.route('a', {
model: function () {
return network.fetch('/a');
},
afterModel: function () {
this.router.replaceWith('b');
},
});
this.route('b', {
model: function () {
return network.fetch('/b');
},
afterModel: function () {
this.router.replaceWith('c');
},
});
this.route('c', {
model: function () {
return network.fetch('/c');
},
});
this.route('d', {
model: function () {
return network.fetch('/d');
},
afterModel: function () {
this.router.replaceWith('e');
},
});
this.route('e', {
model: function () {
return network.fetch('/e');
},
});
this.template('a', '{{x-foo}}');
this.template('b', '{{x-foo}}');
this.template('c', '{{x-foo}}');
this.template('d', '{{x-foo}}');
this.template('e', '{{x-foo}}');
let xFooInstances = 0;
this.component('x-foo', {
init: function () {
this._super();
xFooInstances++;
},
});
let App = this.createApplication();
function assertResources(url, resources) {
network = new Network();
return App.visit(url, { isBrowser: false, shouldRender: false }).then(function (instance) {
try {
let viewRegistry = instance.lookup('-view-registry:main');
assert.strictEqual(Object.keys(viewRegistry).length, 0, 'did not create any views');
assert.deepEqual(network.requests, resources);
} finally {
instance.destroy();
}
}, handleError(assert));
}
return assertResources('/a', ['/a', '/b', '/c'])
.then(() => {
return assertResources('/b', ['/b', '/c']);
})
.then(() => {
return assertResources('/c', ['/c']);
})
.then(() => {
return assertResources('/d', ['/d', '/e']);
})
.then(() => {
return assertResources('/e', ['/e']);
})
.then(() => {
assert.strictEqual(xFooInstances, 0, 'it should not create any x-foo components');
});
});
QUnit.test('FastBoot: tagless components can render', function (assert) {
this.template('application', "<div class='my-context'>{{my-component}}</div>");
this.component('my-component', { tagName: '' }, '<h1>hello world</h1>');
let App = this.createApplication();
return Promise.all([
fastbootVisit(App, '/').then(
assertFastbootResult(assert, {
url: '/',
body: /<div class="my-context"><h1>hello world<\/h1><\/div>/,
}),
handleError(assert)
),
]);
});
});