Skip to content

Commit

Permalink
Merge pull request #232 from satanTime/issues/decorators
Browse files Browse the repository at this point in the history
fix: better handling of double decorations
  • Loading branch information
satanTime committed Nov 14, 2020
2 parents e610ba3 + 60bbebc commit 939e322
Show file tree
Hide file tree
Showing 30 changed files with 662 additions and 155 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ describe('MockPipe', () => {
)
);

it('transforms values to jsoin', () => {
it('transforms values to json', () => {
const fixture = MockRender(TestedComponent);

const pipeElement = ngMocks.find(fixture.debugElement, 'span');
Expand Down Expand Up @@ -2527,7 +2527,8 @@ This function verifies how a class has been decorated.
- `isNgDef( SomeClass, 'c' )` - checks whether `SomeClass` is a component
- `isNgDef( SomeClass, 'd' )` - checks whether `SomeClass` is a directive
- `isNgDef( SomeClass, 'p' )` - checks whether `SomeClass` is a pipe
- `isNgDef( SomeClass )` - checks whether `SomeClass` is a module / component / directive / pipe.
- `isNgDef( SomeClass, 'i' )` - checks whether `SomeClass` is a service
- `isNgDef( SomeClass )` - checks whether `SomeClass` is a module / component / directive / pipe / service.

#### getSourceOfMock

Expand Down
2 changes: 1 addition & 1 deletion examples/MockPipe/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestedComponent {}
describe('MockPipe', () => {
beforeEach(() => MockBuilder(TestedComponent).mock(DependencyPipe, (...args: string[]) => JSON.stringify(args)));

it('transforms values to jsoin', () => {
it('transforms values to json', () => {
const fixture = MockRender(TestedComponent);

const pipeElement = ngMocks.find(fixture.debugElement, 'span');
Expand Down
6 changes: 3 additions & 3 deletions lib/common/func.get-mocked-ng-def-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getTestBedInjection } from './core.helpers';
import { NG_MOCKS } from './core.tokens';
import { Type } from './core.types';
import { isMockedNgDefOf } from './func.is-mocked-ng-def-of';
import { ngMocksUniverse } from './ng-mocks-universe';
import ngMocksUniverse from './ng-mocks-universe';

/**
* Returns a def of a mock module based on a mock module or a source module.
Expand Down Expand Up @@ -60,8 +60,8 @@ export function getMockedNgDefOf(declaration: any, type?: any): any {
// If we are not in the MockBuilder env we can rely on the current cache.
if (!mock && source !== declaration) {
mock = declaration;
} else if (!mock && ngMocksUniverse.cacheMocks.has(source)) {
mock = ngMocksUniverse.cacheMocks.get(source);
} else if (!mock && ngMocksUniverse.cacheDeclarations.has(source)) {
mock = ngMocksUniverse.cacheDeclarations.get(source);
}

if (mock && !type) {
Expand Down
10 changes: 9 additions & 1 deletion lib/common/func.is-ng-def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export function isNgDef(declaration: any, ngType: 'd'): declaration is Type<any>
*/
export function isNgDef(declaration: any, ngType: 'p'): declaration is Type<PipeTransform>;

/**
* Checks whether a class was decorated by @Injectable.
*
* @see https://github.com/ike18t/ng-mocks#isngdef
*/
export function isNgDef(declaration: any, ngType: 'i'): declaration is Type<any>;

/**
* Checks whether a class was decorated by a ng type.
*
Expand All @@ -43,5 +50,6 @@ export function isNgDef(declaration: any, ngType?: string): declaration is Type<
const isComponent = (!ngType || ngType === 'c') && isNgType(declaration, 'Component');
const isDirective = (!ngType || ngType === 'd') && isNgType(declaration, 'Directive');
const isPipe = (!ngType || ngType === 'p') && isNgType(declaration, 'Pipe');
return isModule || isComponent || isDirective || isPipe;
const isInjectable = (!ngType || ngType === 'i') && isNgType(declaration, 'Injectable');
return isModule || isComponent || isDirective || isPipe || isInjectable;
}
2 changes: 1 addition & 1 deletion lib/common/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NgControl } from '@angular/forms';

import mockServiceHelper from '../mock-service/helper';

import { ngMocksUniverse } from './ng-mocks-universe';
import ngMocksUniverse from './ng-mocks-universe';

export type ngMocksMockConfig = {
outputs?: string[];
Expand Down
23 changes: 15 additions & 8 deletions lib/common/ng-mocks-universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import { InjectionToken } from '@angular/core';

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

/**
* Can be changed any time.
*
* @internal
*/
export const ngMocksUniverse = {
builder: new Map(),
cacheMocks: new Map(),
/* istanbul ignore next */
const getGlobal = (): any => window || global;

getGlobal().ngMocksUniverse = getGlobal().ngMocksUniverse || {
builtDeclarations: new Map(),
builtProviders: new Map(),
cacheDeclarations: new Map(),
cacheProviders: new Map(),
config: new Map(),
flags: new Set<string>(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider']),
global: new Map(),
touches: new Set<AnyType<any> | InjectionToken<any>>(),
};

/**
* DO NOT USE this object outside of the library.
* It can be changed any time without a notice.
*
* @internal
*/
export default (() => getGlobal().ngMocksUniverse)();
2 changes: 1 addition & 1 deletion lib/mock-builder/mock-builder-performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NgModule } from '@angular/core';
import { TestBed, TestModuleMetadata } from '@angular/core/testing';

import { flatten, mapEntries, mapKeys, mapValues } from '../common/core.helpers';
import { ngMocksUniverse } from '../common/ng-mocks-universe';
import ngMocksUniverse from '../common/ng-mocks-universe';

import { MockBuilderPromise } from './mock-builder-promise';
import { IMockBuilderResult } from './types';
Expand Down
2 changes: 1 addition & 1 deletion lib/mock-builder/mock-builder-promise.skip-dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DOCUMENT } from '@angular/common';
import { isNgInjectionToken } from 'ng-mocks';

import ngConfig from '../common/core.config';
import { ngMocksUniverse } from '../common/ng-mocks-universe';
import ngMocksUniverse from '../common/ng-mocks-universe';

// Checks if we should avoid mocking of the provider.
export default (provide: any): boolean => {
Expand Down

0 comments on commit 939e322

Please sign in to comment.