Skip to content

Commit

Permalink
fix(core): correct type for AbstractType
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed May 14, 2022
1 parent 017d860 commit 6fbf18d
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 99 deletions.
7 changes: 3 additions & 4 deletions libs/ng-mocks/src/lib/common/core.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { InjectionToken } from '@angular/core';
import { getTestBed } from '@angular/core/testing';

import coreDefineProperty from './core.define-property';
import coreReflectParametersResolve from './core.reflect.parameters-resolve';
import { AnyType, Type } from './core.types';
import { AnyDeclaration, AnyType, Type } from './core.types';
import funcGetGlobal from './func.get-global';
import funcGetName from './func.get-name';

Expand All @@ -13,7 +12,7 @@ import funcGetName from './func.get-name';
* @deprecated
* @internal
*/
export const getTestBedInjection = <I>(token: AnyType<I> | InjectionToken<I>): I | undefined => {
export const getTestBedInjection = <I>(token: AnyDeclaration<I>): I | undefined => {
try {
// istanbul ignore next
return getInjection(token);
Expand All @@ -28,7 +27,7 @@ export const getTestBedInjection = <I>(token: AnyType<I> | InjectionToken<I>): I
* @deprecated
* @internal
*/
export const getInjection = <I>(token: AnyType<I> | InjectionToken<I>): I => {
export const getInjection = <I>(token: AnyDeclaration<I>): I => {
const testBed: any = getTestBed();

// istanbul ignore next
Expand Down
16 changes: 12 additions & 4 deletions libs/ng-mocks/src/lib/common/core.types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// istanbul ignore file
/* eslint-disable @typescript-eslint/ban-types */

import { DebugNode } from '@angular/core';
import { DebugNode, InjectionToken } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';

/**
* It has to be an interface.
* A5 requires it to be a type, because interface doesn't work with A5.
* It matches abstract classes.
*
* @internal
*/
export interface AbstractType<T> extends Function {
type AbstractType<T> = Function & {
prototype: T;
}
};

/**
* It has to be an interface.
Expand All @@ -30,6 +31,13 @@ export interface Type<T> extends Function {
*/
export type AnyType<T> = Type<T> | AbstractType<T>;

/**
* It matches any declaration in angular.
*
* @internal
*/
export type AnyDeclaration<T> = AnyType<T> | InjectionToken<T>;

/**
* DebugNodeSelector describes supported types of selectors
* to search elements and instances in fixtures.
Expand Down
6 changes: 2 additions & 4 deletions libs/ng-mocks/src/lib/common/ng-mocks-universe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { InjectionToken } from '@angular/core';

import { IMockBuilderConfig } from '../mock-builder/types';

import coreConfig from './core.config';
import { AnyType } from './core.types';
import { AnyDeclaration } from './core.types';
import funcGetGlobal from './func.get-global';
import funcGetName from './func.get-name';

Expand Down Expand Up @@ -33,7 +31,7 @@ interface NgMocksUniverse {
hasBuildDeclaration: (def: any) => boolean;
isExcludedDef: (def: any) => boolean;
isProvidedDef: (def: any) => boolean;
touches: Set<AnyType<any> | InjectionToken<any> | string>;
touches: Set<AnyDeclaration<any> | string>;
}

funcGetGlobal().ngMocksUniverse = funcGetGlobal().ngMocksUniverse || {};
Expand Down
6 changes: 2 additions & 4 deletions libs/ng-mocks/src/lib/mock-builder/mock-builder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { InjectionToken } from '@angular/core';

import { flatten } from '../common/core.helpers';
import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import { NgModuleWithProviders } from '../common/func.is-ng-module-def-with-providers';

import { MockBuilderPerformance } from './mock-builder.performance';
import { IMockBuilder } from './types';

export type MockBuilderParam = string | AnyType<any> | InjectionToken<any> | NgModuleWithProviders;
export type MockBuilderParam = string | AnyDeclaration<any> | NgModuleWithProviders;

/**
* MockBuilder provides reach and simple interfaces of chain functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DebugNode, InjectionToken, Injector } from '@angular/core';
import { DebugNode, Injector } from '@angular/core';

import coreInjector from '../common/core.injector';
import { Type } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import { isNgDef } from '../common/func.is-ng-def';

import { Node } from './func.get-from-node';
Expand All @@ -19,7 +19,7 @@ const getParentWithInjector = (node: (DebugNode & Node) | null): Injector | unde
return undefined;
};

export default <T>(result: T[], node: DebugNode & Node, proto: Type<T> | InjectionToken<T>): void => {
export default <T>(result: T[], node: DebugNode & Node, proto: AnyDeclaration<T>): void => {
if (!node.injector || node.injector.constructor.name === 'NullInjector') {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions libs/ng-mocks/src/lib/mock-helper/func.get-from-node-ivy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugNode } from '@angular/core';

import { Type } from '../common/core.types';
import { AnyType } from '../common/core.types';

import { Node } from './func.get-from-node';
import funcGetFromNodeElement from './func.get-from-node-element';
Expand Down Expand Up @@ -44,7 +44,7 @@ const detectContext = (node: DebugNode): any => {

const contextToNodes = (context: any): any => (Array.isArray(context) ? context : context?.lView);

export default <T>(result: T[], node: (DebugNode & Node) | null | undefined, proto: Type<T>): void => {
export default <T>(result: T[], node: (DebugNode & Node) | null | undefined, proto: AnyType<T>): void => {
if (!node || node._debugContext) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions libs/ng-mocks/src/lib/mock-helper/func.get-from-node-scan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugNode } from '@angular/core';

import { Type } from '../common/core.types';
import { AnyType } from '../common/core.types';

const detectGatherFlag = (gather: boolean, el: DebugNode | null, node: any): boolean => {
// LContainer for structural directives can be a trigger for pipes.
Expand Down Expand Up @@ -52,7 +52,7 @@ const scan = <T>(
el: DebugNode | null;
nodes: any[];
normalize: (item: any) => any;
proto: Type<T>;
proto: AnyType<T>;
result: T[];
},
gatherDefault: boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugNode } from '@angular/core';

import { Type } from '../common/core.types';
import { AnyType } from '../common/core.types';

import { Node } from './func.get-from-node';
import funcGetFromNodeElement from './func.get-from-node-element';
Expand All @@ -20,7 +20,7 @@ const normalize = (item: any): any => {
return null;
};

export default <T>(result: T[], node: (DebugNode & Node) | null | undefined, proto: Type<T>): void => {
export default <T>(result: T[], node: (DebugNode & Node) | null | undefined, proto: AnyType<T>): void => {
if (!node || !node._debugContext) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions libs/ng-mocks/src/lib/mock-helper/func.get-from-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugNode, InjectionToken } from '@angular/core';
import { DebugNode } from '@angular/core';

import { Type } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import { isNgDef } from '../common/func.is-ng-def';

import funcGetFromNodeInjector from './func.get-from-node-injector';
Expand Down Expand Up @@ -28,7 +28,7 @@ export interface Node {
parent?: (DebugNode & Node) | null;
}

export default <T>(result: T[], node: DebugNode & Node, proto: Type<T> | InjectionToken<T>): T[] => {
export default <T>(result: T[], node: DebugNode & Node, proto: AnyDeclaration<T>): T[] => {
funcGetFromNodeInjector(result, node, proto);
if (!isNgDef(proto, 't')) {
funcGetFromNodeStandard(result, node, proto);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { InjectionToken } from '@angular/core';

import { flatten } from '../common/core.helpers';
import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';
import { IMockBuilderConfig } from '../mock-builder/types';

export default <T>(
def: AnyType<T> | InjectionToken<T> | string | Array<AnyType<T> | InjectionToken<T> | string>,
def: AnyDeclaration<T> | string | Array<AnyDeclaration<T> | string>,
config?: IMockBuilderConfig,
): void => {
const map = ngMocksUniverse.getConfigMock();
Expand Down
6 changes: 3 additions & 3 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.default-mock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { InjectionToken, Injector } from '@angular/core';
import { Injector } from '@angular/core';

import { flatten } from '../common/core.helpers';
import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

export default <T>(
def: AnyType<T> | InjectionToken<T> | string | Array<AnyType<T> | InjectionToken<T> | string>,
def: AnyDeclaration<T> | string | Array<AnyDeclaration<T> | string>,
callback?: (instance: undefined | T, injector: Injector) => void | Partial<T>,
): void => {
const map = ngMocksUniverse.getOverrides();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { InjectionToken } from '@angular/core';

import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';

export default (source: AnyType<any> | InjectionToken<any>): void => {
export default (source: AnyDeclaration<any>): void => {
funcGlobalPrepare();
ngMocksUniverse.getDefaults().set(source, ['exclude']);
};
6 changes: 2 additions & 4 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.global-keep.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { InjectionToken } from '@angular/core';

import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';

export default (source: AnyType<any> | InjectionToken<any>): void => {
export default (source: AnyDeclaration<any>): void => {
funcGlobalPrepare();
ngMocksUniverse.getDefaults().set(source, ['keep']);
};
6 changes: 2 additions & 4 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.global-mock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { InjectionToken } from '@angular/core';

import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';

export default (source: AnyType<any> | InjectionToken<any>): void => {
export default (source: AnyDeclaration<any>): void => {
funcGlobalPrepare();
ngMocksUniverse.getDefaults().set(source, ['mock']);
};
6 changes: 2 additions & 4 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.global-wipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { InjectionToken } from '@angular/core';

import { AnyType } from '../common/core.types';
import { AnyDeclaration } from '../common/core.types';
import ngMocksUniverse from '../common/ng-mocks-universe';

import funcGlobalPrepare from './func.global-prepare';
import mockHelperDefaultMock from './mock-helper.default-mock';

export default (source: AnyType<any> | InjectionToken<any>): void => {
export default (source: AnyDeclaration<any>): void => {
funcGlobalPrepare();
ngMocksUniverse.getDefaults().delete(source);
mockHelperDefaultMock(source);
Expand Down
Loading

0 comments on commit 6fbf18d

Please sign in to comment.