-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex_spec.ts
149 lines (130 loc) · 4.88 KB
/
index_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
/**
* @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 { DevServerBuilderOutput } from '@angular-devkit/build-angular';
import { workspaces } from '@angular-devkit/core';
import fetch from 'node-fetch'; // eslint-disable-line import/no-extraneous-dependencies
import { createArchitect, host } from '../testing/test-utils';
describe('Dev Server Builder index', () => {
const targetSpec = { project: 'app', target: 'serve' };
beforeEach(async () => host.initialize().toPromise());
afterEach(async () => host.restore().toPromise());
it(`adds 'type="module"' when differential loading is needed`, async () => {
host.writeMultipleFiles({
'.browserslistrc': `
last 1 chrome version
IE 10
`,
});
const architect = (await createArchitect(host.root())).architect;
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
const output = (await run.result) as DevServerBuilderOutput;
expect(output.success).toBe(true);
const response = await fetch(output.baseUrl);
expect(await response.text()).toContain(
'<script src="runtime.js" type="module"></script>' +
'<script src="polyfills.js" type="module"></script>' +
'<script src="vendor.js" type="module"></script>' +
'<script src="main.js" type="module"></script>',
);
await run.stop();
});
it(`does not add 'type="module"' to custom scripts when differential loading is needed`, async () => {
host.writeMultipleFiles({
'.browserslistrc': `
last 1 chrome version
IE 10
`,
'test.js': 'console.log("test");',
});
const { workspace } = await workspaces.readWorkspace(
host.root(),
workspaces.createWorkspaceHost(host),
);
const app = workspace.projects.get('app');
if (!app) {
fail('Test application "app" not found.');
return;
}
const target = app.targets.get('build');
if (!target) {
fail('Test application "app" target "build" not found.');
return;
}
if (!target.options) {
target.options = {};
}
target.options.scripts = ['test.js'];
await workspaces.writeWorkspace(workspace, workspaces.createWorkspaceHost(host));
const architect = (await createArchitect(host.root())).architect;
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
const output = (await run.result) as DevServerBuilderOutput;
expect(output.success).toBe(true);
const response = await fetch(output.baseUrl);
expect(await response.text()).toContain(
'<script src="runtime.js" type="module"></script>' +
'<script src="polyfills.js" type="module"></script>' +
'<script src="scripts.js" defer></script>' +
'<script src="vendor.js" type="module"></script>' +
'<script src="main.js" type="module"></script>',
);
await run.stop();
});
it(`doesn't 'type="module"' when differential loading is not needed`, async () => {
host.writeMultipleFiles({
'.browserslistrc': `
last 1 chrome version
`,
});
const architect = (await createArchitect(host.root())).architect;
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
const output = (await run.result) as DevServerBuilderOutput;
expect(output.success).toBe(true);
const response = await fetch(output.baseUrl);
expect(await response.text()).toContain(
'<script src="runtime.js" defer></script>' +
'<script src="polyfills.js" defer></script>' +
'<script src="vendor.js" defer></script>' +
'<script src="main.js" defer></script>',
);
await run.stop();
});
it('sets HTML lang attribute with the active locale', async () => {
const locale = 'fr';
const { workspace } = await workspaces.readWorkspace(
host.root(),
workspaces.createWorkspaceHost(host),
);
const app = workspace.projects.get('app');
if (!app) {
fail('Test application "app" not found.');
return;
}
app.extensions['i18n'] = {
locales: {
[locale]: [],
},
};
const target = app.targets.get('build');
if (!target) {
fail('Test application "app" target "build" not found.');
return;
}
if (!target.options) {
target.options = {};
}
target.options.localize = [locale];
await workspaces.writeWorkspace(workspace, workspaces.createWorkspaceHost(host));
const architect = (await createArchitect(host.root())).architect;
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
const output = (await run.result) as DevServerBuilderOutput;
expect(output.success).toBe(true);
const response = await fetch(output.baseUrl);
expect(await response.text()).toContain(`lang="${locale}"`);
await run.stop();
});
});