Skip to content

Commit 4ece43d

Browse files
authored
test(angular-rspack): add unit tests for angular loader (#17)
* test(angular-rspack): add unit tests for angular-transform-loader * test(angular-rspack): add unit tests for angular-transform-loader * fix: format * fix: wip * fix: test change * fix: test change 2
1 parent 6c091d8 commit 4ece43d

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

packages/angular-rspack/src/lib/models/augmented-compilation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const NG_RSPACK_SYMBOL_NAME = 'NG_RSPACK_BUILD';
55

66
export type NG_RSPACK_COMPILATION_STATE = {
77
javascriptTransformer: JavaScriptTransformer;
8-
typescriptFileCache: Map<string, string>;
8+
typescriptFileCache: Map<string, string | Buffer>;
99
};
1010
export type NgRspackCompilation = Compilation & {
1111
[NG_RSPACK_SYMBOL_NAME]: () => NG_RSPACK_COMPILATION_STATE;

packages/angular-rspack/src/lib/plugins/loaders/angular-transform.loader.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ import {
66
TemplateUrlsResolver,
77
} from '@nx/angular-rspack-compiler';
88

9-
const styleUrlsResolver = new StyleUrlsResolver();
10-
const templateUrlsResolver = new TemplateUrlsResolver();
9+
const _styleUrlsResolver = new StyleUrlsResolver();
10+
const _templateUrlsResolver = new TemplateUrlsResolver();
1111

12-
export default function loader(this: LoaderContext<unknown>, content: string) {
12+
export default function loader(
13+
this: LoaderContext<unknown>,
14+
content: string,
15+
opt?: {
16+
styleUrlsResolver: StyleUrlsResolver;
17+
templateUrlsResolver: TemplateUrlsResolver;
18+
}
19+
) {
20+
const {
21+
styleUrlsResolver = _styleUrlsResolver,
22+
templateUrlsResolver = _templateUrlsResolver,
23+
} = opt ?? {};
1324
const callback = this.async();
1425
if (
1526
(this._compilation as NgRspackCompilation)[NG_RSPACK_SYMBOL_NAME] ===
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { beforeEach, describe, expect, vi } from 'vitest';
2+
import { default as angularTransformLoader } from './angular-transform.loader';
3+
import { NG_RSPACK_SYMBOL_NAME, type NgRspackCompilation } from '../../models';
4+
import type { LoaderContext } from '@rspack/core';
5+
6+
class StyleUrlsResolverMock {
7+
constructor(private cb: (content: string) => string) {}
8+
9+
resolve(content: string, _: string): string[] {
10+
return [this.cb(content)];
11+
}
12+
}
13+
14+
class TemplateUrlsResolverMock {
15+
constructor(private cb: (content: string) => string) {}
16+
17+
resolve(content: string, _: string): string[] {
18+
return [this.cb(content)];
19+
}
20+
}
21+
22+
describe('angular-transform.loader', () => {
23+
const callback = vi.fn();
24+
const addDependency = vi.fn();
25+
const typescriptFileCache = new Map<string, string | Buffer>();
26+
const _compilation = {
27+
[NG_RSPACK_SYMBOL_NAME]: () => ({
28+
typescriptFileCache,
29+
}),
30+
} as unknown as NgRspackCompilation;
31+
const thisValue = {
32+
async: vi.fn(() => callback),
33+
_compilation: {},
34+
} as unknown as LoaderContext<unknown>;
35+
36+
beforeEach(() => {
37+
vi.clearAllMocks();
38+
});
39+
40+
it('should return content when NG_RSPACK_SYMBOL_NAME is undefined', () => {
41+
angularTransformLoader.call(thisValue, 'content');
42+
expect(callback).toHaveBeenCalledWith(null, 'content');
43+
});
44+
45+
it('should add dependencies for resolved template and style URLs', () => {
46+
angularTransformLoader.call(
47+
{
48+
...thisValue,
49+
_compilation,
50+
resourcePath: '/home/projects/analog/src/app/app.component.ts',
51+
addDependency,
52+
},
53+
'@Component()',
54+
{
55+
templateUrlsResolver: new TemplateUrlsResolverMock(
56+
() =>
57+
'./app.component.html|/home/projects/analog/src/app/app.component.html'
58+
),
59+
styleUrlsResolver: new StyleUrlsResolverMock(
60+
() =>
61+
'./app.component.css|/home/projects/analog/src/app/app.component.css'
62+
),
63+
} as any
64+
);
65+
66+
expect(addDependency).toHaveBeenCalledTimes(2);
67+
expect(addDependency).toHaveBeenNthCalledWith(
68+
1,
69+
'/home/projects/analog/src/app/app.component.html'
70+
);
71+
expect(addDependency).toHaveBeenNthCalledWith(
72+
2,
73+
'/home/projects/analog/src/app/app.component.css'
74+
);
75+
});
76+
77+
it('should return content when typescriptFileCache does not contain normalizedRequest', () => {
78+
angularTransformLoader.call(
79+
{
80+
...thisValue,
81+
_compilation,
82+
resourcePath: '/path/to/file.ts',
83+
},
84+
'content'
85+
);
86+
87+
expect(callback).toHaveBeenCalledWith(null, 'content');
88+
});
89+
90+
it('should return content when typescriptFileCache does contain string', () => {
91+
typescriptFileCache.set(
92+
'/home/projects/analog/src/app/app.component.ts',
93+
'cached content'
94+
);
95+
96+
angularTransformLoader.call(
97+
{
98+
...thisValue,
99+
_compilation,
100+
resourcePath: '/home/projects/analog/src/app/app.component.ts',
101+
},
102+
'@Component()'
103+
);
104+
105+
expect(callback).toHaveBeenCalledWith(null, 'cached content');
106+
});
107+
108+
it('should return content when typescriptFileCache does contain Buffer', () => {
109+
typescriptFileCache.set(
110+
'/home/projects/analog/src/app/app.component.ts',
111+
Buffer.from('cached content')
112+
);
113+
114+
angularTransformLoader.call(
115+
{
116+
...thisValue,
117+
_compilation,
118+
resourcePath: '/home/projects/analog/src/app/app.component.ts',
119+
},
120+
'@Component()'
121+
);
122+
123+
expect(callback).toHaveBeenCalledWith(null, Buffer.from('cached content'));
124+
});
125+
});

0 commit comments

Comments
 (0)