Skip to content

Commit 74e7990

Browse files
author
Alex Malkevich
committed
feat(module): allow to pass NgModule config in extras
Now you can pass config for NgModule in extas as `extras.ngModule`. It will be used in the module where component/directive under test is declared so you can provide neccessary config for it to work properly.
1 parent 03f3336 commit 74e7990

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ describe('MyComponent', () => {
9999

100100
For more examples visit [example-component.spec.ts](projects/ngx-testing/src/lib/example-component.spec.ts).
101101

102+
### Providing custom config for NgModule
103+
104+
Sometimes you will need to provide extra config for your component/directive under test
105+
so it can work in isolated unit test. You can do this like so:
106+
107+
```ts
108+
import { getTestingForComponent, getTestingForDirective } from '@orchestrator/ngx-testing';
109+
110+
// For component
111+
getTestingForComponent(YourComponent, { ngModule: { imports: [...], providers: [...] } });
112+
113+
// For directive
114+
getTestingForDirective(YourDirective, { ngModule: { imports: [...], providers: [...] } });
115+
```
116+
117+
_NOTE:_ `ngModule` prop in second argument is default config for `@NgModule`.
118+
102119
## Build
103120

104121
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.

projects/ngx-testing/src/lib/ngx-testing.module.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TestingDirectiveModuleExtras,
1313
TestTypeKind,
1414
} from './types';
15+
import { mergeArrays } from './util';
1516

1617
@NgModule({
1718
imports: [CommonModule],
@@ -22,7 +23,7 @@ export class NgxTestingModule<T = any> {
2223
compType: Type<T>,
2324
extras: TestingComponentModuleExtras = {},
2425
): ModuleWithProviders<NgxTestingModule<T>> {
25-
const testModule = getTestingModuleFor(compType, compType);
26+
const testModule = getTestingModuleFor(compType, compType, extras.ngModule);
2627
return {
2728
ngModule: testModule,
2829
providers: [
@@ -38,7 +39,11 @@ export class NgxTestingModule<T = any> {
3839
dirType: Type<T>,
3940
extras: TestingDirectiveModuleExtras = {},
4041
): ModuleWithProviders<NgxTestingModule<T>> {
41-
const testModule = getTestingModuleFor(dirType, extras.hostComponent);
42+
const testModule = getTestingModuleFor(
43+
dirType,
44+
extras.hostComponent,
45+
extras.ngModule,
46+
);
4247
return {
4348
ngModule: testModule,
4449
providers: [
@@ -51,12 +56,18 @@ export class NgxTestingModule<T = any> {
5156
}
5257
}
5358

54-
function getTestingModuleFor<T>(type: Type<T>, entryType?: Type<any>) {
59+
function getTestingModuleFor<T>(
60+
type: Type<T>,
61+
entryType?: Type<any>,
62+
extra: NgModule = {},
63+
) {
5564
@NgModule({
56-
imports: [NgxTestingModule],
57-
exports: [NgxTestingModule, type],
58-
declarations: [type],
59-
entryComponents: entryType ? [entryType] : [],
65+
imports: mergeArrays(extra.imports, [NgxTestingModule]),
66+
exports: mergeArrays(extra.exports, [NgxTestingModule, type]),
67+
declarations: mergeArrays(extra.declarations, [type]),
68+
entryComponents: entryType
69+
? mergeArrays(extra.entryComponents, [entryType])
70+
: extra.entryComponents,
6071
})
6172
class TestingModule<D> {}
6273
return TestingModule as Type<TestingModule<T>>;

projects/ngx-testing/src/lib/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ComponentFactory,
33
DebugElement,
44
EventEmitter,
5-
NgModuleFactory,
5+
NgModule,
66
Type,
77
} from '@angular/core';
88

@@ -11,6 +11,7 @@ import { OutputMock } from './output-mock';
1111
export interface TestingModuleExtras {
1212
template?: string;
1313
projectContent?: string;
14+
ngModule?: NgModule;
1415
}
1516

1617
// tslint:disable-next-line:no-empty-interface

projects/ngx-testing/src/lib/util.ts

+14
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ export function getDirectiveIO<T>(dirType: Type<T>): DirectiveIO {
2626
export function get<T>(type: Type<T> | InjectionToken<T>): T {
2727
return TestBed.get(type);
2828
}
29+
30+
/**
31+
* @internal
32+
*/
33+
export function mergeArrays<T>(...arrays: T[][]): T[] {
34+
return arrays.reduce((acc, arr) => [...acc, ...toArray(arr)], []);
35+
}
36+
37+
/**
38+
* @internal
39+
*/
40+
export function toArray<T>(arr?: T[]): T[] {
41+
return arr ? arr : [];
42+
}

0 commit comments

Comments
 (0)