Skip to content

Commit

Permalink
feat(lib): v4 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal authored and dirkluijk committed Aug 26, 2019
1 parent 3684241 commit aaeb7f9
Show file tree
Hide file tree
Showing 37 changed files with 231 additions and 592 deletions.
21 changes: 0 additions & 21 deletions projects/spectator/jest/src/config.ts

This file was deleted.

32 changes: 5 additions & 27 deletions projects/spectator/jest/src/host.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { Type } from '@angular/core';
import { SpectatorWithHost as BaseSpectatorWithHost, createHostComponentFactory as baseCreateHostComponentFactory, HostComponent, SpectatorOptions, isType, Token } from '@netbasal/spectator';

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

export class SpectatorWithHost<C, H = HostComponent> extends BaseSpectatorWithHost<C, H> {
/**
* @inheritDoc
*/
get<T>(type: Token<T>, fromComponentInjector = false): T & SpyObject<T> {
return super.get(type, fromComponentInjector) as T & SpyObject<T>;
}
}
import { createHostComponentFactory as baseCreateHostComponentFactory, HostComponent, SpectatorOptions, SpectatorWithHost as BaseSpectatorWithHost } from '@netbasal/spectator';
import { CreateComponentOptions } from '../../src/lib/types';

export function createHostComponentFactory<C, H = HostComponent>(options: SpectatorOptions<C, H> | Type<C>): (template: string, detectChanges?: boolean, complexInputs?: Partial<C>) => SpectatorWithHost<C, H> {
if (!isType(options)) {
options.providers = options.providers || [];
(options.mocks || []).forEach(type => options.providers.push(mockProvider(type)));
options.mocks = [];
}
export class SpectatorWithHost<C, H = HostComponent> extends BaseSpectatorWithHost<C, H> {}

return baseCreateHostComponentFactory(options) as (template: string, detectChanges?: boolean, complexInputs?: Partial<C>) => SpectatorWithHost<C, H>;
export function createHostComponentFactory<Component, Host = HostComponent>(options: SpectatorOptions<Component, Host> | Type<Component>): (template: string, options?: CreateComponentOptions<Component>) => SpectatorWithHost<Component, Host> {
return baseCreateHostComponentFactory(options);
}
16 changes: 1 addition & 15 deletions projects/spectator/jest/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { Type } from '@angular/core';
import { SpectatorHTTP as BaseSpectatorHTTP, createHTTPFactory as baseCreateHTTPFactory } from '@netbasal/spectator';

import { SpyObject } from './mock';

export class SpectatorHTTP<T> extends BaseSpectatorHTTP<T> {
get: <S>(service: Type<S>) => S & SpyObject<S>;
}
import { createHTTPFactory as baseCreateHTTPFactory, SpectatorHTTP } from '@netbasal/spectator';

export function createHTTPFactory<T>(dataService: Type<T>, providers = []): () => SpectatorHTTP<T> {
return baseCreateHTTPFactory(dataService, providers) as () => SpectatorHTTP<T>;
Expand Down
21 changes: 2 additions & 19 deletions projects/spectator/jest/src/internals.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/
import { Spectator as BaseSpectator } from '@netbasal/spectator';

import { Spectator as BaseSpectator, Token } from '@netbasal/spectator';

import { SpyObject } from './mock';

export class Spectator<C> extends BaseSpectator<C> {
/**
* @inheritDoc
*/
get<T>(type: Token<T> | Token<any>, fromComponentInjector = false): T & SpyObject<T> {
return super.get(type, fromComponentInjector) as T & SpyObject<T>;
}
}
export class Spectator<C> extends BaseSpectator<C> {}
10 changes: 2 additions & 8 deletions projects/spectator/jest/src/matchers-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ declare namespace jest {
toHaveId(id: string | number): boolean;

toHaveClass(className: string): boolean;
/**
* @deprecated
*/
toHaveAttr({ attr, val }: { attr: string; val: string }): boolean;

toHaveAttribute(attr: string, val?: string): boolean;
/**
* @deprecated
*/
toHaveProp({ prop, val }: { prop: string; val: string }): boolean;

toHaveProperty(prop: string, val: string | boolean): boolean;

toHaveProperty(prop: string, val: string | boolean): boolean;
Expand Down
1 change: 0 additions & 1 deletion projects/spectator/jest/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// <reference types="jest" />
/// <reference path="./matchers-types.d.ts" />
export * from './config';
export * from './dom-selectors';
export * from './host';
export * from './http';
Expand Down
25 changes: 4 additions & 21 deletions projects/spectator/jest/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

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

import { mockProvider, SpyObject } from './mock';
import { createService as baseCreateService, ServiceParams, SpectatorService as BaseSpectatorService, Token } from '@netbasal/spectator';
import { SpyObject } from './mock';

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

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>;
export function createService<Service>(options: ServiceParams<Service> | Type<Service>): SpectatorService<Service> {
return baseCreateService(options);
}
28 changes: 4 additions & 24 deletions projects/spectator/jest/src/spectator.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { createTestComponentFactory as baseCreateTestComponentFactory, SpectatorOptions, isType } from '@netbasal/spectator';
import { mockProvider } from './mock';
import { createTestComponentFactory as baseCreateTestComponentFactory, SpectatorOptions } from '@netbasal/spectator';
import { Type } from '@angular/core';
import { Spectator } from './internals';
import { CreateComponentOptions } from '../../src/lib/types';

/**
* Create factory-function for tested component
* @param component - testedType
* @param shallow - use NO_ERRORS_SCHEMA
* @param moduleMetadata
*/
export function createTestComponentFactory<T>(options: SpectatorOptions<T> | Type<T>): (componentParameters?: Partial<T>, detectChanges?: boolean) => Spectator<T> {
if (!isType(options)) {
options.providers = options.providers || [];
(options.mocks || []).forEach(type => options.providers.push(mockProvider(type)));
options.mocks = [];
}

return baseCreateTestComponentFactory(options) as (componentParameters?: Partial<T>, detectChanges?: boolean) => Spectator<T>;
export function createTestComponentFactory<Component>(typeOrOptions: SpectatorOptions<Component> | Type<Component>): (options?: CreateComponentOptions<Component>) => Spectator<Component> {
return baseCreateTestComponentFactory(typeOrOptions);
}
25 changes: 9 additions & 16 deletions projects/spectator/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { TestModuleMetadata } from '@angular/core/testing';
import { Component, NO_ERRORS_SCHEMA, Type } from '@angular/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { mockProvider } from './mock';
import { isType } from './is-type';
import { getGlobalsInjections } from './globals-injections';
import { isType } from './types';

@Component({
template: ''
Expand Down Expand Up @@ -39,17 +31,17 @@ const defaultOptions: SpectatorOptions<any, HostComponent> = {
declareComponent: true
};

export function initialModule<T, C = HostComponent>(
options: SpectatorOptions<T, C> | Type<T>,
export function initialModule<Component, Host = HostComponent>(
options: SpectatorOptions<Component, Host> | Type<Component>,
withHost = false
): {
moduleMetadata: TestModuleMetadata & SpectatorOptions<T, C>;
component: Type<T>;
host: Type<C>;
moduleMetadata: TestModuleMetadata & SpectatorOptions<Component, Host>;
component: Type<Component>;
host: Type<Host>;
} {
const { declarations: globalDec, providers: globalProviders, imports: globalImports } = getGlobalsInjections();
const merged = Object.assign({}, defaultOptions, options);
let moduleMetadata: TestModuleMetadata & Partial<SpectatorOptions<T, C>>;
let moduleMetadata: TestModuleMetadata & Partial<SpectatorOptions<Component, Host>>;
let component;
let host;

Expand All @@ -61,6 +53,7 @@ export function initialModule<T, C = HostComponent>(
imports: [...globalImports, NoopAnimationsModule],
schemas: [],
providers: [...globalProviders],
componentProviders: [],
entryComponents: []
};
} else {
Expand All @@ -72,7 +65,7 @@ export function initialModule<T, C = HostComponent>(
imports: [...globalImports, merged.disableAnimations ? NoopAnimationsModule : [], ...(merged.imports || [])],
schemas: [merged.shallow ? NO_ERRORS_SCHEMA : merged.schemas || []],
providers: [...globalProviders, ...(merged.providers || [])],
componentProviders: merged.componentProviders ? [merged.componentProviders] : undefined,
componentProviders: merged.componentProviders ? [merged.componentProviders] : [],
entryComponents: [merged.entryComponents]
};

Expand Down
9 changes: 0 additions & 9 deletions projects/spectator/src/lib/dummy-ngmodule.ts

This file was deleted.

65 changes: 33 additions & 32 deletions projects/spectator/src/lib/host.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { DebugElement, Type } from '@angular/core';
import { DebugElement, Provider, Type } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
import { By } from '@angular/platform-browser';

import { _getChild, _getChildren, _setInput, Spectator } from './internals';
import * as customMatchers from './matchers';
import { DOMSelector } from './dom-selectors';
import { HostComponent, initialModule, SpectatorOptions } from './config';
import { Token } from './token';
import { isType } from './is-type';
import { CreateComponentOptions, isType } from './types';

export class SpectatorWithHost<C, H = HostComponent> extends Spectator<C> {
hostComponent: H;
Expand All @@ -29,6 +20,7 @@ export class SpectatorWithHost<C, H = HostComponent> extends Spectator<C> {
get debugElement() {
return this._debugElement || this.hostDebugElement;
}

set debugElement(value) {
this._debugElement = value;
}
Expand Down Expand Up @@ -72,25 +64,32 @@ export class SpectatorWithHost<C, H = HostComponent> extends Spectator<C> {
}
}

export function createHostComponentFactory<C, H = HostComponent>(typeOrOptions: SpectatorOptions<C, H> | Type<C>): (template: string, detectChanges?: boolean, complexInputs?: Partial<C>) => SpectatorWithHost<C, H> {
const { component, moduleMetadata, host } = initialModule<C, H>(typeOrOptions, true);
export function createHostComponentFactory<Component, Host = HostComponent>(typeOrOptions: SpectatorOptions<Component, Host> | Type<Component>): (template: string, options?: CreateComponentOptions<Component>) => SpectatorWithHost<Component, Host> {
const { component, moduleMetadata, host } = initialModule<Component, Host>(typeOrOptions, true);

const dc = isType(typeOrOptions) || typeOrOptions.detectChanges === undefined ? true : typeOrOptions.detectChanges;
const factoryCD = isType(typeOrOptions) || typeOrOptions.detectChanges === undefined ? true : typeOrOptions.detectChanges;

beforeEach(() => {
jasmine.addMatchers(customMatchers as any);
TestBed.configureTestingModule(moduleMetadata);
});

return (template: string, detectChanges = true, initialInputs?: Partial<C>) => {
return (template: string, options?: CreateComponentOptions<Component>) => {
const defaults: CreateComponentOptions<Component> = { props: {}, detectChanges: true, providers: [] };
const { detectChanges, props, providers } = { ...defaults, ...options };

if (providers.length) {
providers.forEach((provider: Provider) => {
TestBed.overrideProvider((provider as any).provide, provider as any);
});
}

TestBed.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: moduleMetadata.entryComponents
}
});

TestBed.overrideComponent(host, { set: { template: template } });

if (moduleMetadata.componentProviders) {
TestBed.overrideComponent(component, {
set: {
Expand All @@ -99,30 +98,32 @@ export function createHostComponentFactory<C, H = HostComponent>(typeOrOptions:
});
}

const spectatorWithHost = new SpectatorWithHost<C, H>();
spectatorWithHost.hostFixture = TestBed.createComponent(host);
TestBed.overrideComponent(host, { set: { template: template } });

const withHost = new SpectatorWithHost<Component, Host>();
withHost.hostFixture = TestBed.createComponent(host);
// The host component instance
spectatorWithHost.hostComponent = spectatorWithHost.hostFixture.componentInstance;
spectatorWithHost.hostDebugElement = spectatorWithHost.hostFixture.debugElement;
spectatorWithHost.hostElement = spectatorWithHost.hostFixture.nativeElement;
withHost.hostComponent = withHost.hostFixture.componentInstance;
withHost.hostDebugElement = withHost.hostFixture.debugElement;
withHost.hostElement = withHost.hostFixture.nativeElement;
// The tested component debug element
spectatorWithHost.debugElement = spectatorWithHost.hostFixture.debugElement.query(By.directive(component));
withHost.debugElement = withHost.hostFixture.debugElement.query(By.directive(component));
// The tested component instance, rendered inside the host
if (spectatorWithHost.debugElement) {
spectatorWithHost.component = spectatorWithHost.debugElement.componentInstance;
spectatorWithHost.element = spectatorWithHost.debugElement.nativeElement;
if (withHost.debugElement) {
withHost.component = withHost.debugElement.componentInstance;
withHost.element = withHost.debugElement.nativeElement;
}

if (initialInputs) {
Object.keys(initialInputs).forEach(key => {
spectatorWithHost.component[key] = initialInputs[key];
if (props) {
Object.keys(props).forEach(key => {
withHost.component[key] = props[key];
});
}

if (dc && detectChanges) {
spectatorWithHost.hostFixture.detectChanges();
if (factoryCD && detectChanges) {
withHost.hostFixture.detectChanges();
}

return spectatorWithHost;
return withHost;
};
}
8 changes: 0 additions & 8 deletions projects/spectator/src/lib/http.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* @license
* Copyright Netanel Basal. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https:/NetanelBasal/spectator/blob/master/LICENSE
*/

import { async, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController, TestRequest } from '@angular/common/http/testing';
import { HttpClient } from '@angular/common/http';
Expand Down
Loading

0 comments on commit aaeb7f9

Please sign in to comment.