From 361188f4870feacc899f47a41f0aac4425364e65 Mon Sep 17 00:00:00 2001 From: Netanel Basal Date: Mon, 13 May 2019 15:54:25 +0300 Subject: [PATCH] feat(service): allow passing entrycomponents --- projects/spectator/jest/src/service.ts | 6 ++++-- projects/spectator/package.json | 2 +- projects/spectator/src/lib/service.ts | 26 ++++++++++++++++---------- src/app/auth.service.spec.ts | 2 ++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/projects/spectator/jest/src/service.ts b/projects/spectator/jest/src/service.ts index 7a7db1bb..b531220a 100644 --- a/projects/spectator/jest/src/service.ts +++ b/projects/spectator/jest/src/service.ts @@ -7,7 +7,7 @@ */ 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'; @@ -15,11 +15,13 @@ export interface SpectatorService extends BaseSpectatorService { get(token: any): T & SpyObject; } -export function createService(options: Params | Type): SpectatorService { +export function createService(options: ServiceParams | Type): SpectatorService { 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; diff --git a/projects/spectator/package.json b/projects/spectator/package.json index 626ebf2c..c0700466 100644 --- a/projects/spectator/package.json +++ b/projects/spectator/package.json @@ -2,7 +2,7 @@ "name": "@netbasal/spectator", "description": "Angular tests made easy", "author": "Netanel Basal ", - "version": "3.7.2", + "version": "3.8.0", "license": "MIT", "repository": { "type": "git", diff --git a/projects/spectator/src/lib/service.ts b/projects/spectator/src/lib/service.ts index 2aefa4c7..3d5ec0dc 100644 --- a/projects/spectator/src/lib/service.ts +++ b/projects/spectator/src/lib/service.ts @@ -6,10 +6,11 @@ * found in the LICENSE file at https://github.com/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 { service: S; @@ -17,25 +18,30 @@ export interface SpectatorService { get(token: any): T & SpyObject; } -export type Params = { +export type ServiceParams = TestModuleMetadata & { service?: Type; mocks?: Type[]; - providers?: Provider[]; - imports?: any[]; + entryComponents?: any[]; }; -export function createService(options: Params | Type): SpectatorService { +export function createService(options: ServiceParams | Type): SpectatorService { const service = isType(options) ? options : options.service; - const module: Params = { - providers: [service], - imports: [] + const module: ServiceParams = { + 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(() => { diff --git a/src/app/auth.service.spec.ts b/src/app/auth.service.spec.ts index 4ac050fd..21a4a3d2 100644 --- a/src/app/auth.service.spec.ts +++ b/src/app/auth.service.spec.ts @@ -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] });