forked from angular/angular
/
text_spec.ts
146 lines (127 loc) · 4.23 KB
/
text_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
/**
* @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
*/
import {Component} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {of} from 'rxjs';
describe('text instructions', () => {
it('should handle all flavors of interpolated text', () => {
@Component({
template: `
<div>a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h{{eight}}i{{nine}}j</div>
<div>a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h{{eight}}i</div>
<div>a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h</div>
<div>a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g</div>
<div>a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f</div>
<div>a{{one}}b{{two}}c{{three}}d{{four}}e</div>
<div>a{{one}}b{{two}}c{{three}}d</div>
<div>a{{one}}b{{two}}c</div>
<div>a{{one}}b</div>
<div>{{one}}</div>
`
})
class App {
one = 1;
two = 2;
three = 3;
four = 4;
five = 5;
six = 6;
seven = 7;
eight = 8;
nine = 9;
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
const allTextContent =
Array.from((fixture.nativeElement as HTMLElement).querySelectorAll('div'))
.map((div: HTMLDivElement) => div.textContent);
expect(allTextContent).toEqual([
'a1b2c3d4e5f6g7h8i9j',
'a1b2c3d4e5f6g7h8i',
'a1b2c3d4e5f6g7h',
'a1b2c3d4e5f6g',
'a1b2c3d4e5f',
'a1b2c3d4e',
'a1b2c3d',
'a1b2c',
'a1b',
'1',
]);
});
it('should handle piped values in interpolated text', () => {
@Component({
template: `
<p>{{who | async}} sells {{(item | async)?.what}} down by the {{(item | async)?.where}}.</p>
`
})
class App {
who = of('Sally');
item = of({
what: 'seashells',
where: 'seashore',
});
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
const p = fixture.nativeElement.querySelector('p') as HTMLDivElement;
expect(p.textContent).toBe('Sally sells seashells down by the seashore.');
});
it('should not sanitize urls in interpolated text', () => {
@Component({
template: '<p>{{thisisfine}}</p>',
})
class App {
thisisfine = 'javascript:alert("image_of_dog_with_coffee_in_burning_building.gif")';
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
const p = fixture.nativeElement.querySelector('p');
expect(p.textContent)
.toBe('javascript:alert("image_of_dog_with_coffee_in_burning_building.gif")');
});
it('should not allow writing HTML in interpolated text', () => {
@Component({
template: '<div>{{test}}</div>',
})
class App {
test = '<h1>LOL, big text</h1>';
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(div.innerHTML).toBe('<h1>LOL, big text</h1>');
});
it('should stringify functions used in bindings', () => {
@Component({
template: '<div>{{test}}</div>',
})
class App {
test = function foo() {};
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(div.innerHTML).toBe('function foo() { }');
});
it('should handle binding syntax used inside quoted text', () => {
@Component({
template: `{{'Interpolations look like {{this}}'}}`,
})
class App {
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('Interpolations look like {{this}}');
});
});