This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
loader-assets.spec.js
79 lines (63 loc) · 1.88 KB
/
loader-assets.spec.js
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
/*jshint jasmine: true, node: true */
'use strict';
const mock = require('mock-require');
describe('SKY UX assets Webpack loader', () => {
let loader;
beforeEach(() => {
mock('fs', {
readFileSync: function () {
return 'zxcv';
}
});
mock('hash-file', {
sync: function () {
return 'abcdefg';
}
});
mock('loader-utils', {
getOptions: function () {
return {
baseUrl: 'https://localhost:1234/base/'
};
}
});
mock.reRequire('../lib/assets-processor');
loader = mock.reRequire('../loader/sky-assets/index');
});
afterEach(() => {
mock.stop('hash-file');
mock.stop('fs');
});
it('should replace links to assets and emit the referenced files', () => {
const html = `
<img src="~/assets/image.jpg">
<img src="~/assets/image2.jpg">
<img src="~/assets/image.3.jpg">
`;
const config = {
_compiler: {
plugin: function () { }
},
options: {
SKY_PAGES: {
entries: []
}
},
emitFile: function () { }
};
const emitFileSpy = spyOn(config, 'emitFile');
let modifiedHtml = loader.apply(config, [html]);
// Verify the referenced file is emitted to Webpack's output.
expect(emitFileSpy.calls.count()).toBe(3);
expect(emitFileSpy.calls.argsFor(0)).toEqual(['assets/image.abcdefg.jpg', 'zxcv']);
expect(emitFileSpy.calls.argsFor(1)).toEqual(['assets/image2.abcdefg.jpg', 'zxcv']);
expect(emitFileSpy.calls.argsFor(2)).toEqual(['assets/image.3.abcdefg.jpg', 'zxcv']);
// Verify that the references were updated to include the base URL and hash.
expect(modifiedHtml).toBe(`
<img src="https://localhost:1234/base/assets/image.abcdefg.jpg">
<img src="https://localhost:1234/base/assets/image2.abcdefg.jpg">
<img src="https://localhost:1234/base/assets/image.3.abcdefg.jpg">
`
);
});
});