Skip to content

Commit

Permalink
feat(service): allow passing entrycomponents
Browse files Browse the repository at this point in the history
  • Loading branch information
Netanel Basal committed May 13, 2019
1 parent 9edad7d commit 361188f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
6 changes: 4 additions & 2 deletions projects/spectator/jest/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
*/

import { Type } from '@angular/core';
import { Params, SpectatorService as BaseSpectatorService, createService as baseCreateService, isType } from '@netbasal/spectator';
import { SpectatorService as BaseSpectatorService, createService as baseCreateService, isType, ServiceParams } from '@netbasal/spectator';

import { mockProvider, SpyObject } from './mock';

export interface SpectatorService<S> extends BaseSpectatorService<S> {
get<T>(token: any): T & SpyObject<T>;
}

export function createService<S>(options: Params<S> | Type<S>): SpectatorService<S> {
export function createService<S>(options: ServiceParams<S> | Type<S>): SpectatorService<S> {
if (!isType(options)) {
options.providers = options.providers || [];
(options.mocks || []).forEach(type => options.providers.push(mockProvider(type)));
options.mocks = [];
options.declarations = options.declarations || [];
options.entryComponents = options.entryComponents || [];
}

return baseCreateService(options) as SpectatorService<S>;
Expand Down
2 changes: 1 addition & 1 deletion projects/spectator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@netbasal/spectator",
"description": "Angular tests made easy",
"author": "Netanel Basal <[email protected]>",
"version": "3.7.2",
"version": "3.8.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
26 changes: 16 additions & 10 deletions projects/spectator/src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,42 @@
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { TestBed } from '@angular/core/testing';
import { TestBed, TestModuleMetadata } from '@angular/core/testing';
import { Provider, Type } from '@angular/core';
import { mockProvider, SpyObject } from './mock';
import { isType } from './is-type';
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';

export interface SpectatorService<S> {
service: S;

get<T>(token: any): T & SpyObject<T>;
}

export type Params<S> = {
export type ServiceParams<S> = TestModuleMetadata & {
service?: Type<S>;
mocks?: Type<any>[];
providers?: Provider[];
imports?: any[];
entryComponents?: any[];
};

export function createService<S>(options: Params<S> | Type<S>): SpectatorService<S> {
export function createService<S>(options: ServiceParams<S> | Type<S>): SpectatorService<S> {
const service = isType(options) ? options : options.service;

const module: Params<S> = {
providers: [service],
imports: []
const module: ServiceParams<S> = {
providers: [service]
};

if (!isType(options)) {
(options.mocks || []).forEach(type => module.providers.push(mockProvider(type)));
(options.providers || []).forEach(type => module.providers.push(type));
module.imports = module.imports.concat(options.imports || []);
module.providers = [...module.providers, ...(options.providers || [])];
module.declarations = options.declarations || [];
module.imports = options.imports || [];

TestBed.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: options.entryComponents || []
}
});
}

beforeEach(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/app/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { createService } from '@netbasal/spectator';
import { AuthService } from './auth.service';
import { DateService } from './date.service';
import { DynamicComponent } from './dynamic/dynamic.component';

describe('AuthService', () => {
const spectator = createService({
service: AuthService,
entryComponents: [DynamicComponent],
mocks: [DateService]
});

Expand Down

0 comments on commit 361188f

Please sign in to comment.