Repository navigation
Expand file tree
/
Copy pathangular-host_spec.ts
More file actions
97 lines (83 loc) · 3.12 KB
/
Copy pathangular-host_spec.ts
File metadata and controls
97 lines (83 loc) · 3.12 KB
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
/**
* @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.dev/license
*/
import ts from 'typescript';
import { type AngularHostOptions, createAngularCompilerHost } from './angular-host';
describe('createAngularCompilerHost', () => {
function createHost() {
const transformStylesheet = jasmine
.createSpy<AngularHostOptions['transformStylesheet']>('transformStylesheet')
.and.resolveTo('.compiled {}');
const processWebWorker = jasmine
.createSpy<AngularHostOptions['processWebWorker']>('processWebWorker')
.and.returnValue('worker.js');
const host = createAngularCompilerHost(
ts,
{},
{
transformStylesheet,
processWebWorker,
},
undefined,
);
return { host, transformStylesheet };
}
describe('readResource', () => {
it('should not call readFile for .css and .scss stylesheet files', async () => {
const { host } = createHost();
const readFileSpy = spyOn(host, 'readFile').and.returnValue('.btn { color: red; }');
expect(await host.readResource?.('/app/src/app.component.css')).toBe('');
expect(await host.readResource?.('/app/src/app.component.scss')).toBe('');
expect(readFileSpy).not.toHaveBeenCalled();
});
it('should call readFile for .html and .svg template files', async () => {
const { host } = createHost();
const readFileSpy = spyOn(host, 'readFile').and.callFake(
(fileName) => `<content of ${fileName}>`,
);
expect(await host.readResource?.('/app/src/app.component.html')).toBe(
'<content of /app/src/app.component.html>',
);
expect(await host.readResource?.('/app/src/icon.component.svg')).toBe(
'<content of /app/src/icon.component.svg>',
);
expect(readFileSpy).toHaveBeenCalledTimes(2);
});
});
describe('transformResource', () => {
it('should invoke transformStylesheet when context.resourceFile is set even if data is empty', async () => {
const { host, transformStylesheet } = createHost();
const result = await host.transformResource?.('', {
type: 'style',
containingFile: '/app/src/app.component.ts',
resourceFile: '/app/src/app.component.scss',
order: 0,
className: 'AppComponent',
});
expect(result).toEqual({ content: '.compiled {}' });
expect(transformStylesheet).toHaveBeenCalledOnceWith(
'',
'/app/src/app.component.ts',
'/app/src/app.component.scss',
0,
'AppComponent',
);
});
it('should short-circuit empty inline styles without calling transformStylesheet', async () => {
const { host, transformStylesheet } = createHost();
const result = await host.transformResource?.(' ', {
type: 'style',
containingFile: '/app/src/app.component.ts',
resourceFile: null,
order: 0,
className: 'AppComponent',
});
expect(result).toEqual({ content: '' });
expect(transformStylesheet).not.toHaveBeenCalled();
});
});
});