forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase-href-webpack-plugin.spec.ts
66 lines (57 loc) · 1.9 KB
/
base-href-webpack-plugin.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
import {oneLineTrim} from 'common-tags';
import {BaseHrefWebpackPlugin} from './base-href-webpack-plugin';
function mockCompiler(indexHtml: string, callback: Function) {
return {
plugin: function (event: any, compilerCallback: Function) {
const compilation = {
plugin: function (hook: any, compilationCallback: Function) {
const htmlPluginData = {
html: indexHtml
};
compilationCallback(htmlPluginData, callback);
}
};
compilerCallback(compilation);
}
};
}
describe('base href webpack plugin', () => {
const html = oneLineTrim`
<html>
<head></head>
<body></body>
</html>
`;
it('should do nothing when baseHref is null', () => {
const plugin = new BaseHrefWebpackPlugin({ baseHref: null });
const compiler = mockCompiler(html, (x: any, htmlPluginData: any) => {
expect(htmlPluginData.html).toEqual('<body><head></head></body>');
});
plugin.apply(compiler);
});
it('should insert base tag when not exist', function () {
const plugin = new BaseHrefWebpackPlugin({ baseHref: '/' });
const compiler = mockCompiler(html, (x: any, htmlPluginData: any) => {
expect(htmlPluginData.html).toEqual(oneLineTrim`
<html>
<head><base href="/"></head>
<body></body>
</html>
`);
});
plugin.apply(compiler);
});
it('should replace href attribute when base tag already exists', function () {
const plugin = new BaseHrefWebpackPlugin({ baseHref: '/myUrl/' });
const compiler = mockCompiler(oneLineTrim`
<head><base href="/" target="_blank"></head>
<body></body>
`, (x: any, htmlPluginData: any) => {
expect(htmlPluginData.html).toEqual(oneLineTrim`
<head><base href="/myUrl/" target="_blank"></head>
<body></body>
`);
});
plugin.apply(compiler);
});
});