Skip to content

Commit

Permalink
feat(module): allow to pass NgModule config in extras
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Alex Malkevich committed Jan 17, 2019
1 parent 03f3336 commit 74e7990
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ describe('MyComponent', () => {

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

### Providing custom config for NgModule

Sometimes you will need to provide extra config for your component/directive under test
so it can work in isolated unit test. You can do this like so:

```ts
import { getTestingForComponent, getTestingForDirective } from '@orchestrator/ngx-testing';

// For component
getTestingForComponent(YourComponent, { ngModule: { imports: [...], providers: [...] } });

// For directive
getTestingForDirective(YourDirective, { ngModule: { imports: [...], providers: [...] } });
```

_NOTE:_ `ngModule` prop in second argument is default config for `@NgModule`.

## Build

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.
Expand Down
25 changes: 18 additions & 7 deletions projects/ngx-testing/src/lib/ngx-testing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TestingDirectiveModuleExtras,
TestTypeKind,
} from './types';
import { mergeArrays } from './util';

@NgModule({
imports: [CommonModule],
Expand All @@ -22,7 +23,7 @@ export class NgxTestingModule<T = any> {
compType: Type<T>,
extras: TestingComponentModuleExtras = {},
): ModuleWithProviders<NgxTestingModule<T>> {
const testModule = getTestingModuleFor(compType, compType);
const testModule = getTestingModuleFor(compType, compType, extras.ngModule);
return {
ngModule: testModule,
providers: [
Expand All @@ -38,7 +39,11 @@ export class NgxTestingModule<T = any> {
dirType: Type<T>,
extras: TestingDirectiveModuleExtras = {},
): ModuleWithProviders<NgxTestingModule<T>> {
const testModule = getTestingModuleFor(dirType, extras.hostComponent);
const testModule = getTestingModuleFor(
dirType,
extras.hostComponent,
extras.ngModule,
);
return {
ngModule: testModule,
providers: [
Expand All @@ -51,12 +56,18 @@ export class NgxTestingModule<T = any> {
}
}

function getTestingModuleFor<T>(type: Type<T>, entryType?: Type<any>) {
function getTestingModuleFor<T>(
type: Type<T>,
entryType?: Type<any>,
extra: NgModule = {},
) {
@NgModule({
imports: [NgxTestingModule],
exports: [NgxTestingModule, type],
declarations: [type],
entryComponents: entryType ? [entryType] : [],
imports: mergeArrays(extra.imports, [NgxTestingModule]),
exports: mergeArrays(extra.exports, [NgxTestingModule, type]),
declarations: mergeArrays(extra.declarations, [type]),
entryComponents: entryType
? mergeArrays(extra.entryComponents, [entryType])
: extra.entryComponents,
})
class TestingModule<D> {}
return TestingModule as Type<TestingModule<T>>;
Expand Down
3 changes: 2 additions & 1 deletion projects/ngx-testing/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ComponentFactory,
DebugElement,
EventEmitter,
NgModuleFactory,
NgModule,
Type,
} from '@angular/core';

Expand All @@ -11,6 +11,7 @@ import { OutputMock } from './output-mock';
export interface TestingModuleExtras {
template?: string;
projectContent?: string;
ngModule?: NgModule;
}

// tslint:disable-next-line:no-empty-interface
Expand Down
14 changes: 14 additions & 0 deletions projects/ngx-testing/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ export function getDirectiveIO<T>(dirType: Type<T>): DirectiveIO {
export function get<T>(type: Type<T> | InjectionToken<T>): T {
return TestBed.get(type);
}

/**
* @internal
*/
export function mergeArrays<T>(...arrays: T[][]): T[] {
return arrays.reduce((acc, arr) => [...acc, ...toArray(arr)], []);
}

/**
* @internal
*/
export function toArray<T>(arr?: T[]): T[] {
return arr ? arr : [];
}

0 comments on commit 74e7990

Please sign in to comment.