-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #727 from satanTime/issues/623
fix(mock-builder): provides globally exported providers from directives and components #623
- Loading branch information
Showing
11 changed files
with
450 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
libs/ng-mocks/src/lib/mock-builder/promise/add-missing-definition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { isNgDef } from '../../common/func.is-ng-def'; | ||
import ngMocksUniverse from '../../common/ng-mocks-universe'; | ||
|
||
export default (def: any, configDef: Map<any, any>): boolean => { | ||
if (!isNgDef(def, 'i') && isNgDef(def)) { | ||
return true; | ||
} | ||
|
||
const config = configDef.get(def); | ||
if (config?.dependency) { | ||
return true; | ||
} | ||
|
||
const configInstance = ngMocksUniverse.configInstance.get(def); | ||
if (ngMocksUniverse.touches.has(def) && (configInstance?.exported || !config?.export)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; |
13 changes: 2 additions & 11 deletions
13
...d-missed-keep-declarations-and-modules.ts → ...-missing-keep-declarations-and-modules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 2 additions & 11 deletions
13
...d-missed-mock-declarations-and-modules.ts → ...-missing-mock-declarations-and-modules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { Provider } from '@angular/core'; | ||
|
||
import { isNgDef } from '../common/func.is-ng-def'; | ||
import { isNgModuleDefWithProviders } from '../common/func.is-ng-module-def-with-providers'; | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
import { MockComponent } from '../mock-component/mock-component'; | ||
import { MockDirective } from '../mock-directive/mock-directive'; | ||
import { MockPipe } from '../mock-pipe/mock-pipe'; | ||
import helperMockService from '../mock-service/helper.mock-service'; | ||
|
||
import { MockModule } from './mock-module'; | ||
|
||
// tslint:disable-next-line variable-name | ||
let BrowserAnimationsModule: any; | ||
// tslint:disable-next-line variable-name | ||
let NoopAnimationsModule: any; | ||
// istanbul ignore next | ||
let replaceWithNoop: (def: any) => boolean = () => false; | ||
try { | ||
// tslint:disable-next-line no-require-imports no-var-requires | ||
const imports = require('@angular/platform-browser/animations'); | ||
BrowserAnimationsModule = imports.BrowserAnimationsModule; | ||
NoopAnimationsModule = imports.NoopAnimationsModule; | ||
replaceWithNoop = (def: any) => | ||
def === BrowserAnimationsModule && | ||
!!BrowserAnimationsModule && | ||
!!NoopAnimationsModule && | ||
!ngMocksUniverse.getResolution(def); | ||
} catch { | ||
// nothing to do | ||
} | ||
|
||
const processDefMap: Array<[any, any]> = [ | ||
['c', MockComponent], | ||
['d', MockDirective], | ||
['p', MockPipe], | ||
]; | ||
|
||
const processDef = (def: any) => { | ||
// BrowserAnimationsModule is a very special case. | ||
// If it is not resolved manually, we simply replace it with NoopAnimationsModule. | ||
if (replaceWithNoop(def)) { | ||
return NoopAnimationsModule; | ||
} | ||
|
||
if (isNgDef(def, 'm') || isNgModuleDefWithProviders(def)) { | ||
return MockModule(def as any); | ||
} | ||
if (ngMocksUniverse.hasBuildDeclaration(def)) { | ||
return ngMocksUniverse.getBuildDeclaration(def); | ||
} | ||
if (ngMocksUniverse.flags.has('skipMock') && ngMocksUniverse.getResolution(def) !== 'mock') { | ||
return def; | ||
} | ||
for (const [flag, func] of processDefMap) { | ||
if (isNgDef(def, flag)) { | ||
return func(def); | ||
} | ||
} | ||
}; | ||
|
||
// resolveProvider is a special case because of the def structure. | ||
const createResolveProvider = | ||
(resolutions: Map<any, any>, change: () => void): ((def: Provider) => any) => | ||
(def: Provider) => | ||
helperMockService.resolveProvider(def, resolutions, change); | ||
|
||
const createResolveWithProviders = (def: any, mockDef: any): boolean => | ||
mockDef && mockDef.ngModule && isNgModuleDefWithProviders(def); | ||
|
||
const createResolveExisting = (def: any, resolutions: Map<any, any>, change: (flag?: boolean) => void): any => { | ||
const mockDef = resolutions.get(def); | ||
if (def !== mockDef) { | ||
change(); | ||
} | ||
|
||
return mockDef; | ||
}; | ||
|
||
const createResolveExcluded = (def: any, resolutions: Map<any, any>, change: (flag?: boolean) => void): void => { | ||
resolutions.set(def, undefined); | ||
|
||
change(); | ||
}; | ||
|
||
const createResolve = | ||
(resolutions: Map<any, any>, change: (flag?: boolean) => void): ((def: any) => any) => | ||
(def: any) => { | ||
if (resolutions.has(def)) { | ||
return createResolveExisting(def, resolutions, change); | ||
} | ||
|
||
const detectedDef = isNgModuleDefWithProviders(def) ? def.ngModule : def; | ||
if (ngMocksUniverse.isExcludedDef(detectedDef)) { | ||
return createResolveExcluded(def, resolutions, change); | ||
} | ||
ngMocksUniverse.touches.add(detectedDef); | ||
|
||
const mockDef = processDef(def); | ||
if (createResolveWithProviders(def, mockDef)) { | ||
resolutions.set(def.ngModule, mockDef.ngModule); | ||
} | ||
if (ngMocksUniverse.flags.has('skipMock')) { | ||
ngMocksUniverse.config.get('ngMocksDepsSkip')?.add(mockDef); | ||
} | ||
resolutions.set(def, mockDef); | ||
change(mockDef !== def); | ||
|
||
return mockDef; | ||
}; | ||
|
||
export default ( | ||
change: () => void, | ||
resolutions: Map<any, any>, | ||
): { | ||
resolve: (def: any) => any; | ||
resolveProvider: (def: Provider) => any; | ||
} => { | ||
const resolve = createResolve(resolutions, change); | ||
const resolveProvider = createResolveProvider(resolutions, change); | ||
|
||
return { | ||
resolve, | ||
resolveProvider, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { flatten } from '../common/core.helpers'; | ||
import funcGetProvider from '../common/func.get-provider'; | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
|
||
export default (providers?: any[]): void => { | ||
for (const provider of flatten(providers ?? [])) { | ||
const provide = funcGetProvider(provider); | ||
|
||
const config = ngMocksUniverse.configInstance.get(provide) ?? {}; | ||
config.exported = true; | ||
ngMocksUniverse.configInstance.set(provide, config); | ||
} | ||
}; |
Oops, something went wrong.