forked from angular/angular
/
template_mapping_spec.ts
672 lines (605 loc) · 26.5 KB
/
template_mapping_spec.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/// <reference types="node" />
import {inspect} from 'util';
import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing';
import {tsSourceMapBug29300Fixed} from '../../src/ngtsc/util/src/ts_source_map_bug_29300';
import {loadStandardTestFiles} from '../helpers/src/mock_file_loading';
import {NgtscTestEnvironment} from './env';
import {getMappedSegments, SegmentMapping} from './sourcemap_utils';
const testFiles = loadStandardTestFiles();
runInEachFileSystem((os) => {
describe('template source-mapping', () => {
let env!: NgtscTestEnvironment;
beforeEach(() => {
env = NgtscTestEnvironment.setup(testFiles);
env.tsconfig({sourceMap: true, target: 'es2015', enableI18nLegacyMessageIdFormat: false});
});
describe('Inline templates', () => {
describe('(element creation)', () => {
it('should map simple element with content', () => {
const mappings = compileAndMap('<h1>Heading 1</h1>');
expect(mappings).toContain(
{source: '<h1>', generated: 'i0.ɵɵelementStart(0, "h1")', sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: 'Heading 1',
generated: 'i0.ɵɵtext(1, "Heading 1")',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</h1>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map void element', () => {
const mappings = compileAndMap('<hr>');
expect(mappings).toContain(
{source: '<hr>', generated: 'i0.ɵɵelement(0, "hr")', sourceUrl: '../test.ts'});
});
});
describe('(interpolations)', () => {
it('should map a mix of interpolated and static content', () => {
const mappings = compileAndMap('<h3>Hello {{ name }}</h3>');
expect(mappings).toContain(
{source: '<h3>', generated: 'i0.ɵɵelementStart(0, "h3")', sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: 'Hello {{ name }}',
generated: 'i0.ɵɵtextInterpolate1("Hello ", ctx.name, "")',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</h3>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map a complex interpolated expression', () => {
const mappings = compileAndMap('<h2>{{ greeting + " " + name }}</h2>');
expect(mappings).toContain(
{source: '<h2>', generated: 'i0.ɵɵelementStart(0, "h2")', sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: '{{ greeting + " " + name }}',
generated: 'i0.ɵɵtextInterpolate(ctx.greeting + " " + ctx.name)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</h2>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map interpolated properties', () => {
const mappings = compileAndMap('<div id="{{name}}"></div>');
expect(mappings).toContain({
source: '<div id="{{name}}"></div>',
generated: 'i0.ɵɵelement(0, "div", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain({
source: 'id="{{name}}"',
generated: 'i0.ɵɵpropertyInterpolate("id", ctx.name)',
sourceUrl: '../test.ts'
});
});
it('should map interpolation with pipe', () => {
const mappings = compileAndMap('<div>{{200.3 | percent : 2 }}</div>');
expect(mappings).toContain(
{source: '<div>', generated: 'i0.ɵɵelementStart(0, "div")', sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: '{{200.3 | percent : 2 }}',
generated: 'i0.ɵɵtextInterpolate(i0.ɵɵpipeBind2(2, 1, 200.3, 2))',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</div>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
});
describe('(property bindings)', () => {
it('should map a simple input binding expression', () => {
const mappings = compileAndMap('<div [attr]="name"></div>');
expect(mappings).toContain({
source: '<div [attr]="name"></div>',
generated: 'i0.ɵɵelement(0, "div", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain({
source: '[attr]="name"',
generated: 'i0.ɵɵproperty("attr", ctx.name)',
sourceUrl: '../test.ts'
});
});
it('should map a complex input binding expression', () => {
const mappings = compileAndMap('<div [attr]="greeting + name"></div>');
expect(mappings).toContain({
source: '<div [attr]="greeting + name"></div>',
generated: 'i0.ɵɵelement(0, "div", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain({
source: '[attr]="greeting + name"',
generated: 'i0.ɵɵproperty("attr", ctx.greeting + ctx.name)',
sourceUrl: '../test.ts'
});
});
it('should map a longhand input binding expression', () => {
const mappings = compileAndMap('<div bind-attr="name"></div>');
expect(mappings).toContain({
source: '<div bind-attr="name"></div>',
generated: 'i0.ɵɵelement(0, "div", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain({
source: 'bind-attr="name"',
generated: 'i0.ɵɵproperty("attr", ctx.name)',
sourceUrl: '../test.ts'
});
});
it('should map a simple output binding expression', () => {
const mappings = compileAndMap('<button (click)="doSomething()">Do it</button>');
expect(mappings).toContain({
source: '<button (click)="doSomething()">',
generated: 'i0.ɵɵelementStart(0, "button", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: 'Do it', generated: 'i0.ɵɵtext(1, "Do it")', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: 'doSomething()', generated: 'ctx.doSomething()', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: '</button>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map a complex output binding expression', () => {
const mappings = compileAndMap(
`<button (click)="items.push('item' + items.length)">Add Item</button>`);
expect(mappings).toContain({
source: `<button (click)="items.push('item' + items.length)">`,
generated: 'i0.ɵɵelementStart(0, "button", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: 'Add Item', generated: 'i0.ɵɵtext(1, "Add Item")', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: 'items.push(', generated: 'ctx.items.push(', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: `'item'`, generated: `"item"`, sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: ' + items.length)',
generated: ' + ctx.items.length)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</button>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map a longhand output binding expression', () => {
const mappings = compileAndMap('<button on-click="doSomething()">Do it</button>');
expect(mappings).toContain({
source: '<button on-click="doSomething()">',
generated: 'i0.ɵɵelementStart(0, "button", 0)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: 'Do it', generated: 'i0.ɵɵtext(1, "Do it")', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: 'doSomething()', generated: 'ctx.doSomething()', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: '</button>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map a two-way binding expression', () => {
const mappings = compileAndMap('Name: <input [(ngModel)]="name">');
expect(mappings).toContain({
source: '<input [(ngModel)]="name">',
generated: 'i0.ɵɵelementStart(1, "input", 0)',
sourceUrl: '../test.ts'
});
// TODO: improve mappings here
expect(mappings).toContain({
source: '[(ngModel)]="name"',
generated:
'i0.ɵɵlistener("ngModelChange", function TestCmp_Template_input_ngModelChange_1_listener($event) { return ctx.name = $event; })',
sourceUrl: '../test.ts'
});
expect(mappings).toContain({
source: '<input [(ngModel)]="name">',
generated: 'i0.ɵɵelementEnd()',
sourceUrl: '../test.ts'
});
});
it('should map a longhand two-way binding expression', () => {
const mappings = compileAndMap('Name: <input bindon-ngModel="name">');
expect(mappings).toContain({
source: '<input bindon-ngModel="name">',
generated: 'i0.ɵɵelementStart(1, "input", 0)',
sourceUrl: '../test.ts'
});
// TODO: improve mappings here
expect(mappings).toContain({
source: 'bindon-ngModel="name"',
generated:
'i0.ɵɵlistener("ngModelChange", function TestCmp_Template_input_ngModelChange_1_listener($event) { return ctx.name = $event; })',
sourceUrl: '../test.ts'
});
expect(mappings).toContain({
source: '<input bindon-ngModel="name">',
generated: 'i0.ɵɵelementEnd()',
sourceUrl: '../test.ts'
});
});
it('should map a class input binding', () => {
const mappings = compileAndMap('<div [class.initial]="isInitial">Message</div>');
expect(mappings).toContain({
source: '<div [class.initial]="isInitial">',
generated: 'i0.ɵɵelementStart(0, "div")',
sourceUrl: '../test.ts'
});
// TODO: Add better mappings for binding
expect(mappings).toContain(
{source: 'Message', generated: 'i0.ɵɵtext(1, "Message")', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: '</div>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
});
describe('(structural directives)', () => {
it('should map *ngIf scenario', () => {
const mappings = compileAndMap('<div *ngIf="showMessage()">{{ name }}</div>');
expect(mappings).toContain({
source: '<div *ngIf="showMessage()">',
generated: 'i0.ɵɵelementStart(0, "div")',
sourceUrl: '../test.ts'
});
// TODO - map the bindings better
expect(mappings).toContain(
{source: '</div>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
// TODO: the `ctx_r...` appears to be dependent upon previous tests!!!
// expect(mappings).toContain({
// source: '{{ name }}',
// generated: 'i0.ɵɵtextInterpolate(ctx_r0.name)',
// sourceUrl: '../test.ts'
// });
});
it('should map ng-template [ngIf] scenario', () => {
const mappings = compileAndMap(
`<ng-template [ngIf]="showMessage()">\n` +
` <div>{{ name }}</div>\n` +
` <hr>\n` +
`</ng-template>`);
expect(mappings).toContain(
{source: '<div>', generated: 'i0.ɵɵelementStart(0, "div")', sourceUrl: '../test.ts'});
// TODO - map the bindings better
expect(mappings).toContain(
{source: '</div>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
// TODO: the `ctx_r...` appears to be dependent upon previous tests!!!
// expect(mappings).toContain({
// source: '{{ name }}',
// generated: 'i0.ɵɵtextInterpolate(ctx_r0.name)',
// sourceUrl: '../test.ts'
// });
});
it('should map *ngFor scenario', () => {
const mappings = compileAndMap(
'<div *ngFor="let item of items; index as i; trackBy: trackByFn">{{ item }}</div>');
expect(mappings).toContain({
source: '<div *ngFor="let item of items; index as i; trackBy: trackByFn">',
generated: 'i0.ɵɵelementStart(0, "div")',
sourceUrl: '../test.ts'
});
// TODO - map the bindings better
expect(mappings).toContain(
{source: '</div>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
it('should map ng-template [ngFor] scenario', () => {
const mappings = compileAndMap(
`<ng-template ngFor [ngForOf]="items" let-item>{{ item }}</ng-template>`);
// TODO - map the bindings better
});
});
describe('(content projection)', () => {
it('should map default and selected projection', () => {
const mappings = compileAndMap(
`<h3><ng-content select="title"></ng-content></h3>\n` +
`<div><ng-content></ng-content></div>`);
expect(mappings).toContain(
{source: '<h3>', generated: 'i0.ɵɵelementStart(0, "h3")', sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: '<ng-content select="title"></ng-content>',
generated: 'i0.ɵɵprojection(1)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</h3>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{source: '<div>', generated: 'i0.ɵɵelementStart(2, "div")', sourceUrl: '../test.ts'});
expect(mappings).toContain({
source: '<ng-content></ng-content>',
generated: 'i0.ɵɵprojection(3, 1)',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{source: '</div>', generated: 'i0.ɵɵelementEnd()', sourceUrl: '../test.ts'});
});
});
describe('$localize', () => {
it('should create simple i18n message source-mapping', () => {
const mappings = compileAndMap(`<div i18n>Hello, World!</div>`);
expect(mappings).toContain({
source: '<div i18n>',
generated: 'i0.ɵɵelementStart(0, "div")',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: 'Hello, World!',
generated: '`Hello, World!`',
sourceUrl: '../test.ts',
});
});
it('should create placeholder source-mappings', () => {
const mappings = compileAndMap(`<div i18n>Hello, {{name}}!</div>`);
expect(mappings).toContain({
source: '<div i18n>',
generated: 'i0.ɵɵelementStart(0, "div")',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '</div>',
generated: 'i0.ɵɵelementEnd()',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: 'Hello, ',
generated: '`Hello, ${',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '{{name}}',
generated: '"\\uFFFD0\\uFFFD"',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '!',
generated: '}:INTERPOLATION:!`',
sourceUrl: '../test.ts',
});
});
it('should correctly handle collapsed whitespace in placeholder source-mappings', () => {
const mappings = compileAndMap(
`<div i18n title=" pre-title {{title_value}} post-title" i18n-title> pre-body {{body_value}} post-body</div>`);
expect(mappings).toContain({
source: '<div i18n title=" pre-title {{title_value}} post-title" i18n-title> ',
generated: 'i0.ɵɵelementStart(0, "div", 0)',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '</div>',
generated: 'i0.ɵɵelementEnd()',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: ' pre-body ',
generated: '` pre-body ${',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '{{body_value}}',
generated: '"\\uFFFD0\\uFFFD"',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: ' post-body',
generated: '}:INTERPOLATION: post-body`',
sourceUrl: '../test.ts',
});
});
it('should create tag (container) placeholder source-mappings', () => {
const mappings = compileAndMap(`<div i18n>Hello, <b>World</b>!</div>`);
expect(mappings).toContain({
source: '<div i18n>',
generated: 'i0.ɵɵelementStart(0, "div")',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '</div>',
generated: 'i0.ɵɵelementEnd()',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: 'Hello, ',
generated: '`Hello, ${',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '<b>',
generated: '"\\uFFFD#2\\uFFFD"',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: 'World',
generated: '}:START_BOLD_TEXT:World${',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '</b>',
generated: '"\\uFFFD/#2\\uFFFD"',
sourceUrl: '../test.ts',
});
expect(mappings).toContain({
source: '!',
generated: '}:CLOSE_BOLD_TEXT:!`',
sourceUrl: '../test.ts',
});
});
});
it('should create (simple string) inline template source-mapping', () => {
const mappings = compileAndMap('<div>this is a test</div><div>{{ 1 + 2 }}</div>');
// Creation mode
expect(mappings).toContain(
{generated: 'i0.ɵɵelementStart(0, "div")', source: '<div>', sourceUrl: '../test.ts'});
expect(mappings).toContain({
generated: 'i0.ɵɵtext(1, "this is a test")',
source: 'this is a test',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementEnd()', source: '</div>', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementStart(2, "div")', source: '<div>', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{generated: 'i0.ɵɵtext(3)', source: '{{ 1 + 2 }}', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementEnd()', source: '</div>', sourceUrl: '../test.ts'});
// Update mode
expect(mappings).toContain({
generated: 'i0.ɵɵtextInterpolate(1 + 2)',
source: '{{ 1 + 2 }}',
sourceUrl: '../test.ts'
});
});
it('should create (simple backtick string) inline template source-mapping', () => {
const mappings = compileAndMap('<div>this is a test</div><div>{{ 1 + 2 }}</div>');
// Creation mode
expect(mappings).toContain(
{generated: 'i0.ɵɵelementStart(0, "div")', source: '<div>', sourceUrl: '../test.ts'});
expect(mappings).toContain({
generated: 'i0.ɵɵtext(1, "this is a test")',
source: 'this is a test',
sourceUrl: '../test.ts'
});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementEnd()', source: '</div>', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementStart(2, "div")', source: '<div>', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{generated: 'i0.ɵɵtext(3)', source: '{{ 1 + 2 }}', sourceUrl: '../test.ts'});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementEnd()', source: '</div>', sourceUrl: '../test.ts'});
// TODO(benlesh): We need to circle back and prevent the extra parens from being generated.
// Update mode
expect(mappings).toContain({
generated: 'i0.ɵɵtextInterpolate(1 + 2)',
source: '{{ 1 + 2 }}',
sourceUrl: '../test.ts'
});
});
it('should create correct inline template source-mapping when the source contains escape sequences',
() => {
// Note that the escaped double quotes, which need un-escaping to be parsed correctly.
const mappings = compileAndMap('<div class=\\"some-class\\">this is a test</div>');
expect(mappings).toContain({
generated: 'i0.ɵɵelementStart(0, "div", 0)',
source: '<div class=\\"some-class\\">',
sourceUrl: '../test.ts'
});
const attrsMapping =
mappings.find(mapping => /consts: \[\[1, "some-class"\]\]/.test(mapping.generated));
expect(attrsMapping).toBeDefined();
});
});
if (tsSourceMapBug29300Fixed()) {
describe('External templates (where TS supports source-mapping)', () => {
it('should create external template source-mapping', () => {
const mappings =
compileAndMap('<div>this is a test</div><div>{{ 1 + 2 }}</div>', './dir/test.html');
// Creation mode
expect(mappings).toContain({
generated: 'i0.ɵɵelementStart(0, "div")',
source: '<div>',
sourceUrl: '../dir/test.html'
});
expect(mappings).toContain({
generated: 'i0.ɵɵtext(1, "this is a test")',
source: 'this is a test',
sourceUrl: '../dir/test.html'
});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementEnd()', source: '</div>', sourceUrl: '../dir/test.html'});
expect(mappings).toContain({
generated: 'i0.ɵɵelementStart(2, "div")',
source: '<div>',
sourceUrl: '../dir/test.html'
});
expect(mappings).toContain(
{generated: 'i0.ɵɵtext(3)', source: '{{ 1 + 2 }}', sourceUrl: '../dir/test.html'});
expect(mappings).toContain(
{generated: 'i0.ɵɵelementEnd()', source: '</div>', sourceUrl: '../dir/test.html'});
// Update mode
expect(mappings).toContain({
generated: 'i0.ɵɵtextInterpolate(1 + 2)',
source: '{{ 1 + 2 }}',
sourceUrl: '../dir/test.html'
});
});
it('should create correct mappings when templateUrl is in a different rootDir', () => {
const mappings = compileAndMap(
'<div>this is a test</div><div>{{ 1 + 2 }}</div>', 'extraRootDir/test.html');
// Creation mode
expect(mappings).toContain({
generated: 'i0.ɵɵelementStart(0, "div")',
source: '<div>',
sourceUrl: '../extraRootDir/test.html'
});
expect(mappings).toContain({
generated: 'i0.ɵɵtext(1, "this is a test")',
source: 'this is a test',
sourceUrl: '../extraRootDir/test.html'
});
expect(mappings).toContain({
generated: 'i0.ɵɵelementEnd()',
source: '</div>',
sourceUrl: '../extraRootDir/test.html'
});
expect(mappings).toContain({
generated: 'i0.ɵɵelementStart(2, "div")',
source: '<div>',
sourceUrl: '../extraRootDir/test.html'
});
expect(mappings).toContain({
generated: 'i0.ɵɵtext(3)',
source: '{{ 1 + 2 }}',
sourceUrl: '../extraRootDir/test.html'
});
expect(mappings).toContain({
generated: 'i0.ɵɵelementEnd()',
source: '</div>',
sourceUrl: '../extraRootDir/test.html'
});
// Update mode
expect(mappings).toContain({
generated: 'i0.ɵɵtextInterpolate(1 + 2)',
source: '{{ 1 + 2 }}',
sourceUrl: '../extraRootDir/test.html'
});
});
});
}
function compileAndMap(template: string, templateUrl: string|null = null) {
const templateConfig = templateUrl ? `templateUrl: '${templateUrl}'` :
('template: `' + template.replace(/`/g, '\\`') + '`');
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test-cmp',
${templateConfig}
})
export class TestCmp {}
`);
if (templateUrl) {
env.write(templateUrl, template);
}
env.driveMain();
return getMappedSegments(env, 'test.js');
}
/**
* Helper function for debugging failed mappings.
* This lays out the segment mappings in the console to make it easier to compare.
*/
function dumpMappings(mappings: SegmentMapping[]) {
mappings.forEach(map => {
// tslint:disable-next-line:no-console
console.log(
padValue(map.sourceUrl, 20, 0) + ' : ' + padValue(inspect(map.source), 100, 23) +
' : ' + inspect(map.generated));
});
function padValue(value: string, max: number, start: number) {
const padding = value.length > max ? ('\n' +
' '.repeat(max + start)) :
' '.repeat(max - value.length);
return value + padding;
}
}
});
});