-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathaugment-index-html_spec.ts
166 lines (149 loc) · 4.67 KB
/
augment-index-html_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
/**
* @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 { tags } from '@angular-devkit/core';
import { AugmentIndexHtmlOptions, augmentIndexHtml } from './augment-index-html';
describe('augment-index-html', () => {
const indexGeneratorOptions: AugmentIndexHtmlOptions = {
html: '<html><head></head><body></body></html>',
baseHref: '/',
sri: false,
files: [],
loadOutputFile: async (_fileName: string) => '',
entrypoints: [
['scripts', false],
['polyfills', true],
['main', true],
['styles', false],
],
};
const oneLineHtml = (html: TemplateStringsArray) =>
tags.stripIndents`${html}`.replace(/(>\s+)/g, '>');
it('can generate index.html', async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
files: [
{ file: 'styles.css', extension: '.css', name: 'styles' },
{ file: 'runtime.js', extension: '.js', name: 'main' },
{ file: 'main.js', extension: '.js', name: 'main' },
{ file: 'runtime.js', extension: '.js', name: 'polyfills' },
{ file: 'polyfills.js', extension: '.js', name: 'polyfills' },
],
});
expect(content).toEqual(oneLineHtml`
<html>
<head><base href="/">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<script src="runtime.js" type="module"></script>
<script src="polyfills.js" type="module"></script>
<script src="main.js" type="module"></script>
</body>
</html>
`);
});
it('should replace base href value', async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
html: '<html><head><base href="/"></head><body></body></html>',
baseHref: '/Apps/',
});
expect(content).toEqual(oneLineHtml`
<html>
<head><base href="/Apps/">
</head>
<body>
</body>
</html>
`);
});
it('should add lang and dir LTR attribute for French (fr)', async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
lang: 'fr',
});
expect(content).toEqual(oneLineHtml`
<html lang="fr" dir="ltr">
<head>
<base href="/">
</head>
<body>
</body>
</html>
`);
});
it('should add lang and dir RTL attribute for Pashto (ps)', async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
lang: 'ps',
});
expect(content).toEqual(oneLineHtml`
<html lang="ps" dir="rtl">
<head>
<base href="/">
</head>
<body>
</body>
</html>
`);
});
it(`should fallback to use language ID to set the dir attribute (en-US)`, async () => {
const { content, warnings } = await augmentIndexHtml({
...indexGeneratorOptions,
lang: 'en-US',
});
expect(warnings).toHaveSize(0);
expect(content).toEqual(oneLineHtml`
<html lang="en-US" dir="ltr">
<head>
<base href="/">
</head>
<body>
</body>
</html>
`);
});
it(`should work when lang (locale) is not provided by '@angular/common'`, async () => {
const { content, warnings } = await augmentIndexHtml({
...indexGeneratorOptions,
lang: 'xx-XX',
});
expect(warnings).toEqual([
`Locale data for 'xx-XX' cannot be found. 'dir' attribute will not be set for this locale.`,
]);
expect(content).toEqual(oneLineHtml`
<html lang="xx-XX">
<head>
<base href="/">
</head>
<body>
</body>
</html>
`);
});
it(`should add script and link tags even when body and head element doesn't exist`, async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
html: `<app-root></app-root>`,
files: [
{ file: 'styles.css', extension: '.css', name: 'styles' },
{ file: 'runtime.js', extension: '.js', name: 'main' },
{ file: 'main.js', extension: '.js', name: 'main' },
{ file: 'runtime.js', extension: '.js', name: 'polyfills' },
{ file: 'polyfills.js', extension: '.js', name: 'polyfills' },
],
});
expect(content).toEqual(oneLineHtml`
<link rel="stylesheet" href="styles.css">
<script src="runtime.js" type="module"></script>
<script src="polyfills.js" type="module"></script>
<script src="main.js" type="module"></script>
<app-root></app-root>
`);
});
});