Skip to content

Commit 2ae96e2

Browse files
committed
test(angular-rspack): add normalize-options unit tests
1 parent ecb6508 commit 2ae96e2

File tree

5 files changed

+156
-27
lines changed

5 files changed

+156
-27
lines changed

packages/angular-rspack/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"express": "4.21.1"
5656
},
5757
"devDependencies": {
58+
"@ng-rspack/testing-utils": "workspace:*",
5859
"@ng-rspack/testing-setup": "workspace:*",
5960
"@ng-rspack/testing-vitest-setup": "workspace:*"
6061
},

packages/angular-rspack/src/lib/models/normalize-options.ts

+38-14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type HasServerOptions = Pick<
2424
AngularRspackPluginOptions,
2525
'server' | 'ssrEntry' | 'root'
2626
>;
27+
2728
export function getHasServer({
2829
server,
2930
ssrEntry,
@@ -37,6 +38,22 @@ export function getHasServer({
3738
);
3839
}
3940

41+
export const DEFAULT_NORMALIZED_OPTIONS: Omit<
42+
AngularRspackPluginOptions,
43+
'root' | 'tsConfig' | 'fileReplacements' | 'ssrEntry' | 'server' | 'hasServer'
44+
> = {
45+
index: './src/index.html',
46+
browser: './src/main.ts',
47+
polyfills: [],
48+
assets: ['./public'],
49+
styles: ['./src/styles.css'],
50+
scripts: [],
51+
aot: true,
52+
inlineStyleLanguage: 'css',
53+
skipTypeChecking: false,
54+
useTsProjectReferences: false,
55+
} as const;
56+
4057
export function normalizeOptions(
4158
options: Partial<AngularRspackPluginOptions> = {}
4259
): AngularRspackPluginOptions {
@@ -45,24 +62,31 @@ export function normalizeOptions(
4562
fileReplacements = [],
4663
server,
4764
ssrEntry,
65+
...restOptions
4866
} = options;
4967

5068
return {
69+
...DEFAULT_NORMALIZED_OPTIONS,
70+
...restOptions,
5171
root,
52-
index: options.index ?? './src/index.html',
53-
browser: options.browser ?? './src/main.ts',
54-
...(server ? { server } : {}),
55-
...(ssrEntry ? { ssrEntry } : {}),
56-
polyfills: options.polyfills ?? [],
57-
assets: options.assets ?? ['./public'],
58-
styles: options.styles ?? ['./src/styles.css'],
59-
scripts: options.scripts ?? [],
60-
fileReplacements: resolveFileReplacements(fileReplacements, root),
61-
aot: options.aot ?? true,
62-
inlineStyleLanguage: options.inlineStyleLanguage ?? 'css',
6372
tsConfig: options.tsConfig ?? join(root, 'tsconfig.app.json'),
64-
hasServer: getHasServer({ server, ssrEntry, root }),
65-
skipTypeChecking: options.skipTypeChecking ?? false,
66-
useTsProjectReferences: options.useTsProjectReferences ?? false,
73+
fileReplacements: resolveFileReplacements(fileReplacements, root),
74+
...normalizeOptionsServerOptions({ server, ssrEntry, root }),
75+
};
76+
}
77+
78+
export function normalizeOptionsServerOptions({
79+
server,
80+
ssrEntry,
81+
root,
82+
}: Pick<AngularRspackPluginOptions, 'root' | 'server' | 'ssrEntry'>) {
83+
if (!getHasServer({ server, ssrEntry, root })) {
84+
return { hasServer: false };
85+
}
86+
87+
return {
88+
hasServer: true,
89+
server,
90+
ssrEntry,
6791
};
6892
}

packages/angular-rspack/src/lib/models/normalize-options.unit.test.ts

+113-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { FileReplacement } from '@ng-rspack/compiler';
22
import {
33
getHasServer,
44
HasServerOptions,
5+
normalizeOptions,
6+
normalizeOptionsServerOptions,
57
resolveFileReplacements,
68
} from './normalize-options';
7-
import { describe } from 'vitest';
9+
import { describe, expect } from 'vitest';
10+
import { vol } from 'memfs';
11+
import { MEMFS_VOLUME } from '@ng-rspack/testing-utils';
812

913
describe('resolveFileReplacements', () => {
1014
it('should resolve file replacements', () => {
@@ -19,6 +23,10 @@ describe('resolveFileReplacements', () => {
1923
});
2024

2125
describe('getHasServer', () => {
26+
afterEach(() => {
27+
vol.reset();
28+
});
29+
2230
it('should return false if server is not provided', () => {
2331
expect(getHasServer({ root: '' })).toBe(false);
2432
});
@@ -30,16 +38,116 @@ describe('getHasServer', () => {
3038
it('should return false if server file does not exist', () => {
3139
expect(
3240
getHasServer({
33-
server: 'server',
34-
ssrEntry: 'ssrEntry',
41+
server: 'main.ts',
42+
ssrEntry: 'server.main.ts',
3543
root: '/not-existing-folder',
3644
})
3745
).toBe(false);
3846
});
3947

40-
it.todo('should return true if server and ssrEntry files exist', () => {
48+
it('should return true if server and ssrEntry files exist', () => {
49+
vol.fromJSON(
50+
{
51+
'main.ts': '',
52+
'server.main.ts': '',
53+
},
54+
MEMFS_VOLUME
55+
);
56+
4157
expect(
42-
getHasServer({ server: 'server', ssrEntry: 'ssrEntry', root: __dirname })
58+
getHasServer({
59+
server: 'main.ts',
60+
ssrEntry: 'server.main.ts',
61+
root: MEMFS_VOLUME,
62+
})
4363
).toBe(true);
4464
});
4565
});
66+
67+
describe('normalizeOptionsServerOptions', () => {
68+
it('should return hasServer false options if getHasServer is false', () => {
69+
expect(
70+
normalizeOptionsServerOptions({
71+
root: MEMFS_VOLUME,
72+
})
73+
).toStrictEqual({ hasServer: false });
74+
});
75+
76+
it('should return hasServer true as well as the server and ssrEntry options if getHasServer is true', () => {
77+
vol.fromJSON(
78+
{
79+
'main.ts': '',
80+
'server.main.ts': '',
81+
},
82+
MEMFS_VOLUME
83+
);
84+
85+
expect(
86+
normalizeOptionsServerOptions({
87+
server: 'main.ts',
88+
ssrEntry: 'server.main.ts',
89+
root: MEMFS_VOLUME,
90+
})
91+
).toStrictEqual({
92+
hasServer: true,
93+
server: 'main.ts',
94+
ssrEntry: 'server.main.ts',
95+
});
96+
});
97+
});
98+
99+
describe('normalizeOptions', () => {
100+
it('should normalize options default options', () => {
101+
expect(normalizeOptions()).toStrictEqual({
102+
root: process.cwd(),
103+
aot: true,
104+
hasServer: false,
105+
inlineStyleLanguage: 'css',
106+
skipTypeChecking: false,
107+
polyfills: [],
108+
index: './src/index.html',
109+
browser: './src/main.ts',
110+
assets: ['./public'],
111+
styles: ['./src/styles.css'],
112+
scripts: [],
113+
fileReplacements: [],
114+
tsConfig: expect.stringMatching(/tsconfig.app.json$/),
115+
useTsProjectReferences: false,
116+
});
117+
});
118+
119+
it('should normalize options server options', () => {
120+
vol.fromJSON(
121+
{
122+
'main.ts': '',
123+
'server.main.ts': '',
124+
},
125+
MEMFS_VOLUME
126+
);
127+
128+
expect(
129+
normalizeOptions({
130+
root: MEMFS_VOLUME,
131+
server: 'main.ts',
132+
ssrEntry: 'server.main.ts',
133+
})
134+
).toStrictEqual({
135+
root: MEMFS_VOLUME,
136+
server: 'main.ts',
137+
ssrEntry: 'server.main.ts',
138+
hasServer: true,
139+
aot: true,
140+
inlineStyleLanguage: 'css',
141+
skipTypeChecking: false,
142+
polyfills: [],
143+
index: './src/index.html',
144+
browser: './src/main.ts',
145+
assets: ['./public'],
146+
styles: ['./src/styles.css'],
147+
scripts: [],
148+
fileReplacements: [],
149+
tsConfig: expect.stringMatching(/tsconfig.app.json$/),
150+
useTsProjectReferences: false,
151+
});
152+
});
153+
});

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testing/vitest-setup/src/lib/fs-memfs.setup-file.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
MockInstance,
3-
afterEach,
4-
beforeEach,
5-
vi,
6-
beforeAll,
7-
afterAll,
8-
} from 'vitest';
1+
import { afterAll, afterEach, beforeAll, beforeEach, MockInstance, vi } from 'vitest';
92
import { MEMFS_VOLUME } from '@ng-rspack/testing-utils';
103

114
/**

0 commit comments

Comments
 (0)