-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathhmr_spec.ts
198 lines (171 loc) · 6.34 KB
/
hmr_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
/**
* @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 { Architect, BuilderRun } from '@angular-devkit/architect';
/* eslint-disable import/no-extraneous-dependencies */
import { Browser } from 'puppeteer/lib/cjs/puppeteer/common/Browser';
import { Page } from 'puppeteer/lib/cjs/puppeteer/common/Page';
import puppeteer from 'puppeteer/lib/cjs/puppeteer/node';
/* eslint-enable import/no-extraneous-dependencies */
import { debounceTime, switchMap, take } from 'rxjs/operators';
import { createArchitect, host } from '../testing/test-utils';
/* eslint-disable @typescript-eslint/no-explicit-any */
declare const document: any;
declare const getComputedStyle: any;
/* eslint-enable @typescript-eslint/no-explicit-any */
describe('Dev Server Builder HMR', () => {
const target = { project: 'app', target: 'serve' };
const overrides = { hmr: true, watch: true, port: 0 };
let architect: Architect;
let browser: Browser;
let page: Page;
let logs: string[] = [];
let runs: BuilderRun[];
beforeAll(async () => {
browser = await puppeteer.launch({
// MacOSX users need to set the local binary manually because Chrome has lib files with
// spaces in them which Bazel does not support in runfiles
// See: https://github.com/angular/angular-cli/pull/17624
// eslint-disable-next-line max-len
// executablePath: '/Users/<USERNAME>/git/angular-cli/node_modules/puppeteer/.local-chromium/mac-800071/chrome-mac/Chromium.app/Contents/MacOS/Chromium',
args: ['--no-sandbox', '--disable-gpu'],
});
});
afterAll(async () => {
await browser.close();
});
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
logs = [];
runs = [];
page = await browser.newPage();
page.on('console', (msg) => logs.push(msg.text()));
host.writeMultipleFiles({
'src/app/app.component.html': `
<p>{{title}}</p>
<input class="visible" type="text">
<input type="hidden">
<select>
<option>one</option>
<option>two</option>
</select>
`,
});
});
afterEach(async () => {
await host.restore().toPromise();
await page.close();
await Promise.all(runs.map((r) => r.stop()));
});
it('works for CSS changes', async () => {
const run = await architect.scheduleTarget(target, overrides);
runs.push(run);
let buildCount = 0;
await run.output
.pipe(
debounceTime(1000),
switchMap(async (buildEvent) => {
expect(buildEvent.success).toBe(true);
const url = buildEvent.baseUrl as string;
switch (buildCount) {
case 0:
await page.goto(url);
expect(logs).toContain('[HMR] Waiting for update signal from WDS...');
host.writeMultipleFiles({
'src/styles.css': 'p { color: rgb(255, 255, 0) }',
});
break;
case 1:
expect(logs).toContain('[HMR] Updated modules:');
expect(logs).toContain(`[HMR] css reload %s ${url}styles.css`);
expect(logs).toContain('[HMR] App is up to date.');
const pTagColor = await page.evaluate(() => {
const el = document.querySelector('p');
return getComputedStyle(el).color;
});
expect(pTagColor).toBe('rgb(255, 255, 0)');
break;
}
logs = [];
buildCount++;
}),
take(2),
)
.toPromise();
});
it('works for TS changes', async () => {
const run = await architect.scheduleTarget(target, overrides);
runs.push(run);
let buildCount = 0;
await run.output
.pipe(
debounceTime(1000),
switchMap(async (buildEvent) => {
expect(buildEvent.success).toBe(true);
const url = buildEvent.baseUrl as string;
switch (buildCount) {
case 0:
await page.goto(url);
expect(logs).toContain('[HMR] Waiting for update signal from WDS...');
host.replaceInFile('src/app/app.component.ts', `'app'`, `'app-hmr'`);
break;
case 1:
expect(logs).toContain('[HMR] Updated modules:');
expect(logs).toContain('[HMR] App is up to date.');
const innerText = await page.evaluate(() => document.querySelector('p').innerText);
expect(innerText).toBe('app-hmr');
break;
}
logs = [];
buildCount++;
}),
take(2),
)
.toPromise();
});
it('restores input and select values', async () => {
const run = await architect.scheduleTarget(target, overrides);
runs.push(run);
let buildCount = 0;
await run.output
.pipe(
debounceTime(1000),
switchMap(async (buildEvent) => {
expect(buildEvent.success).toBe(true);
const url = buildEvent.baseUrl as string;
switch (buildCount) {
case 0:
await page.goto(url);
expect(logs).toContain('[HMR] Waiting for update signal from WDS...');
await page.evaluate(() => {
document.querySelector('input.visible').value = 'input value';
document.querySelector('select').value = 'two';
});
host.replaceInFile('src/app/app.component.ts', `'app'`, `'app-hmr'`);
break;
case 1:
expect(logs).toContain('[HMR] Updated modules:');
expect(logs).toContain('[HMR] App is up to date.');
expect(logs).toContain('[NG HMR] Restoring input/textarea values.');
expect(logs).toContain('[NG HMR] Restoring selected options.');
const inputValue = await page.evaluate(
() => document.querySelector('input.visible').value,
);
expect(inputValue).toBe('input value');
const selectValue = await page.evaluate(() => document.querySelector('select').value);
expect(selectValue).toBe('two');
break;
}
logs = [];
buildCount++;
}),
take(2),
)
.toPromise();
});
});