Skip to content

Commit

Permalink
refactor: core config
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Nov 3, 2020
1 parent 83a7220 commit 468ee09
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
19 changes: 19 additions & 0 deletions lib/common/core.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CommonModule } from '@angular/common';
import { ApplicationModule } from '@angular/core';

export default {
neverMockModule: [ApplicationModule, CommonModule],
neverMockProvidedFunction: [
'DomRendererFactory2',
'DomSharedStylesHost',
'EventManager',
'Injector',
'RendererFactory2',
],
neverMockToken: [
'InjectionToken Set Injector scope.',
'InjectionToken Application Initializer',
'InjectionToken EventManagerPlugins',
'InjectionToken HammerGestureConfig',
],
};
15 changes: 7 additions & 8 deletions lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CommonModule } from '@angular/common';
import { core } from '@angular/compiler';
import { ApplicationModule, NgModule, Provider } from '@angular/core';
import { NgModule, Provider } from '@angular/core';
import { getTestBed } from '@angular/core/testing';

import ngConfig from '../common/core.config';
import { extendClass, flatten } from '../common/core.helpers';
import { ngModuleResolver } from '../common/core.reflect';
import { Type } from '../common/core.types';
Expand Down Expand Up @@ -43,10 +43,6 @@ export function MockModule(module: any): any {
ngModule = module;
}

if (NEVER_MOCK.indexOf(ngModule) !== -1) {
return module;
}

// We are inside of an 'it'.
// It's fine to to return a mock or to throw an exception if it wasn't mocked in TestBed.
if (!ngModuleProviders && (getTestBed() as any)._instantiated) {
Expand All @@ -63,6 +59,11 @@ export function MockModule(module: any): any {
mockModule = ngMocksUniverse.cacheMocks.get(ngModule);
}

if (ngConfig.neverMockModule.indexOf(ngModule) !== -1 && !ngMocksUniverse.flags.has('skipMock')) {
releaseSkipMockFlag = true;
ngMocksUniverse.flags.add('skipMock');
}

// Now we check if we need to keep the original module or to replace it with some other.
if (!mockModule && ngMocksUniverse.builder.has(ngModule)) {
const instance = ngMocksUniverse.builder.get(ngModule);
Expand Down Expand Up @@ -123,8 +124,6 @@ export function MockModule(module: any): any {
: mockModule;
}

const NEVER_MOCK: Array<Type<any>> = [CommonModule, ApplicationModule];

/**
* Can be changed at any time.
*
Expand Down
15 changes: 4 additions & 11 deletions lib/mock-service/mock-provider.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import { APP_INITIALIZER, Provider } from '@angular/core';
import { EVENT_MANAGER_PLUGINS, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
import { Provider } from '@angular/core';

import ngConfig from '../common/core.config';
import { isNgInjectionToken } from '../common/func.is-ng-injection-token';
import { ngMocksUniverse } from '../common/ng-mocks-universe';

import useFactory from './helper.use-factory';
import { MockService } from './mock-service';

const neverMockProvidedFunction = [
'DomRendererFactory2',
'DomSharedStylesHost',
'EventManager',
'Injector',
'RendererFactory2',
];
const neverMockToken = [APP_INITIALIZER, EVENT_MANAGER_PLUGINS, HAMMER_GESTURE_CONFIG];
const { neverMockProvidedFunction, neverMockToken } = ngConfig;

export default function (provider: any): Provider | undefined {
const provide = typeof provider === 'object' && provider.provide ? provider.provide : provider;

if (typeof provide === 'function' && neverMockProvidedFunction.indexOf(provide.name) !== -1) {
return provider;
}
if (isNgInjectionToken(provide) && neverMockToken.indexOf(provide) !== -1) {
if (isNgInjectionToken(provide) && neverMockToken.indexOf(provide.toString()) !== -1) {
return undefined;
}

Expand Down

0 comments on commit 468ee09

Please sign in to comment.