Skip to content

Commit

Permalink
fix: removing deprecations
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removed NG_GUARDS, use NG_MOCKS_GUARDS
BREAKING CHANGE: removed NG_INTERCEPTORS, use NG_MOCKS_INTERCEPTORS
BREAKING CHANGE: removed custom meta in MockComponent
BREAKING CHANGE: removed MockHelper, use ngMocks
  • Loading branch information
satanTime committed Nov 14, 2020
1 parent eef7b94 commit 2625352
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 113 deletions.
14 changes: 0 additions & 14 deletions lib/common/core.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,3 @@ export const NG_MOCKS_OVERRIDES = new InjectionToken<Map<Type<any> | AbstractTyp
export const NG_MOCKS_GUARDS = new InjectionToken<void>('NG_MOCKS_GUARDS');
export const NG_MOCKS_INTERCEPTORS = new InjectionToken<void>('NG_MOCKS_INTERCEPTORS');
export const NG_MOCKS_ROOT_PROVIDERS = new InjectionToken<void>('NG_MOCKS_ROOT_PROVIDERS');

/**
* Use NG_MOCKS_GUARDS instead.
* Will be removed in v11.
* @deprecated
*/
export const NG_GUARDS = NG_MOCKS_GUARDS;

/**
* Use NG_MOCKS_INTERCEPTORS instead.
* Will be removed in v11.
* @deprecated
*/
export const NG_INTERCEPTORS = NG_MOCKS_INTERCEPTORS;
38 changes: 9 additions & 29 deletions lib/mock-component/mock-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';

import { flatten } from '../common/core.helpers';
import { directiveResolver } from '../common/core.reflect';
import { AnyType, Type } from '../common/core.types';
import { Type } from '../common/core.types';
import decorateInputs from '../common/decorate.inputs';
import decorateOutputs from '../common/decorate.outputs';
import decorateQueries from '../common/decorate.queries';
Expand All @@ -29,30 +29,13 @@ import mockServiceHelper from '../mock-service/helper';
import { MockedComponent } from './types';

export function MockComponents(...components: Array<Type<any>>): Array<Type<MockedComponent<any>>> {
return components.map(component => MockComponent(component, undefined));
return components.map(MockComponent);
}

/**
* @deprecated since version 10.0.0 and will be removed in 11.0.0
* feel free to open a github issue to discuss an alternative solution.
* https://github.com/ike18t/ng-mocks/issues
*/
export function MockComponent<TComponent>(
component: Type<TComponent>,
metaData: core.Directive
): Type<MockedComponent<TComponent>>;

/**
* @see https://github.com/ike18t/ng-mocks#how-to-mock-a-component
*/
export function MockComponent<TComponent>(
component: AnyType<TComponent>,
metaData?: core.Directive
): Type<MockedComponent<TComponent>>;
export function MockComponent<TComponent>(
component: Type<TComponent>,
metaData?: core.Directive
): Type<MockedComponent<TComponent>> {
export function MockComponent<TComponent>(component: Type<TComponent>): Type<MockedComponent<TComponent>> {
// we are inside of an 'it'.
// It's fine to to return a mock copy or to throw an exception if it wasn't replaced with its mock copy in TestBed.
if ((getTestBed() as any)._instantiated) {
Expand All @@ -66,15 +49,12 @@ export function MockComponent<TComponent>(
return ngMocksUniverse.cacheDeclarations.get(component);
}

let meta: core.Directive | undefined = metaData;
/* istanbul ignore else */
if (!meta) {
try {
meta = directiveResolver.resolve(component);
} catch (e) {
/* istanbul ignore next */
throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations');
}
let meta: core.Directive | undefined;
try {
meta = directiveResolver.resolve(component);
} catch (e) {
/* istanbul ignore next */
throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations');
}
const { exportAs, inputs, outputs, queries, selector, providers } = meta;

Expand Down
71 changes: 1 addition & 70 deletions lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { EventEmitter, InjectionToken, Provider } from '@angular/core';
import { ComponentFixture, TestModuleMetadata } from '@angular/core/testing';

import { AbstractType, AnyType, Type } from '../common/core.types';
import { AnyType, Type } from '../common/core.types';
import { NgModuleWithProviders } from '../common/func.is-ng-module-def-with-providers';
import { MockedDebugElement, MockedDebugNode } from '../mock-render/types';
import { MockedFunction } from '../mock-service/types';
Expand All @@ -21,75 +21,6 @@ import ngMocksOutput from './mock-helper.output';
import ngMocksReset from './mock-helper.reset';
import ngMocksStub from './mock-helper.stub';

/* istanbul ignore next */
/**
* @deprecated use ngMocks instead
*/
export const MockHelper: {
find<T>(debugElement: MockedDebugElement, component: Type<T>): null | MockedDebugElement<T>;
find<T>(debugElement: MockedDebugElement, component: AbstractType<T>): null | MockedDebugElement<T>;
find<T = any>(debugElement: MockedDebugElement, cssSelector: string): null | MockedDebugElement<T>;
findAll<T>(debugElement: MockedDebugElement, component: Type<T>): Array<MockedDebugElement<T>>;
findAll<T>(debugElement: MockedDebugElement, component: AbstractType<T>): Array<MockedDebugElement<T>>;
findAll<T = any>(debugElement: MockedDebugElement, cssSelector: string): Array<MockedDebugElement<T>>;
findDirective<T>(debugNode: MockedDebugNode, directive: Type<T>): undefined | T;
findDirective<T>(debugNode: MockedDebugNode, directive: AbstractType<T>): undefined | T;
findDirectiveOrFail<T>(debugNode: MockedDebugNode, directive: Type<T>): T;
findDirectiveOrFail<T>(debugNode: MockedDebugNode, directive: AbstractType<T>): T;
findDirectives<T>(debugNode: MockedDebugNode, directive: Type<T>): T[];
findDirectives<T>(debugNode: MockedDebugNode, directive: AbstractType<T>): T[];
findOrFail<T>(debugElement: MockedDebugElement, component: Type<T>): MockedDebugElement<T>;
findOrFail<T>(debugElement: MockedDebugElement, component: AbstractType<T>): MockedDebugElement<T>;
findOrFail<T = any>(debugElement: MockedDebugElement, cssSelector: string): MockedDebugElement<T>;
getDirective<T>(debugNode: MockedDebugNode, directive: Type<T>): undefined | T;
getDirective<T>(debugNode: MockedDebugNode, directive: AbstractType<T>): undefined | T;
getDirectiveOrFail<T>(debugNode: MockedDebugNode, directive: Type<T>): T;
getDirectiveOrFail<T>(debugNode: MockedDebugNode, directive: AbstractType<T>): T;
getInput<T = any>(debugNode: MockedDebugNode, input: string): undefined | T;
getInputOrFail<T = any>(debugNode: MockedDebugNode, input: string): T;
getOutput<T = any>(debugNode: MockedDebugNode, output: string): undefined | EventEmitter<T>;
getOutputOrFail<T = any>(debugNode: MockedDebugNode, output: string): EventEmitter<T>;
mockService<I extends object, O extends object>(instance: I, overrides: O): I & O;
mockService<T = MockedFunction>(instance: any, name: string, style?: 'get' | 'set'): T;
} = {
getInput: (debugNode: MockedDebugNode, input: string): any => ngMocks.input(debugNode, input, undefined),

getInputOrFail: (debugNode: MockedDebugNode, input: string): any => ngMocks.input(debugNode, input),

getOutput: (debugNode: MockedDebugNode, output: string): any => ngMocks.output(debugNode, output, undefined),

getOutputOrFail: (debugNode: MockedDebugNode, output: string): any => ngMocks.output(debugNode, output),

getDirectiveOrFail: <T>(debugNode: MockedDebugNode, directive: Type<T>): T => ngMocks.get(debugNode, directive),

getDirective: <T>(debugNode: MockedDebugNode, directive: Type<T>): undefined | T =>
ngMocks.get(debugNode, directive, undefined),

findDirectiveOrFail: <T>(debugNode: MockedDebugNode, directive: Type<T>): T =>
ngMocks.findInstance(debugNode, directive),

findDirective: <T>(debugNode: MockedDebugNode, directive: Type<T>): undefined | T =>
ngMocks.findInstance(debugNode, directive, undefined),

findDirectives: <T>(debugNode: MockedDebugNode, directive: Type<T>): T[] =>
ngMocks.findInstances(debugNode, directive),

findOrFail: (el: MockedDebugElement, sel: any) => ngMocks.find(el, sel),

// tslint:disable-next-line:no-null-keyword
find: (el: MockedDebugElement, sel: any) => ngMocks.find(el, sel, null),

findAll: (el: MockedDebugElement, sel: any) => ngMocks.findAll(el, sel),

mockService: <T = MockedFunction>(instance: any, override: any, style?: 'get' | 'set'): T => {
if (typeof override !== 'object') {
return ngMocks.stub(instance, override, style);
}

return ngMocks.stub(instance, override);
},
};

/**
* @see https://github.com/ike18t/ng-mocks#ngmocks
*/
Expand Down

0 comments on commit 2625352

Please sign in to comment.