-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngMocks): keep CommonModule #5047
- Loading branch information
Showing
21 changed files
with
421 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; | ||
import { MockService, ngMocks } from 'ng-mocks'; | ||
|
||
import { MockService, ngMocks } from 'ng-mocks'; // eslint-disable-line import/order | ||
|
||
ngMocks.autoSpy('jest'); | ||
|
||
// In case, if you use @angular/router and Angular 14+. | ||
// You might want to set a mock of DefaultTitleStrategy as TitleStrategy. | ||
// A14 fix: making DefaultTitleStrategy to be a default mock for TitleStrategy | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; // eslint-disable-line import/order | ||
ngMocks.defaultMock(TitleStrategy, () => MockService(DefaultTitleStrategy)); | ||
|
||
// Usually, *ngIf and other declarations from CommonModule aren't expected to be mocked. | ||
// The code below keeps them. | ||
import { CommonModule } from '@angular/common'; // eslint-disable-line import/order | ||
import { ApplicationModule } from '@angular/core'; // eslint-disable-line import/order | ||
import { BrowserModule } from '@angular/platform-browser'; // eslint-disable-line import/order | ||
ngMocks.globalKeep(ApplicationModule, true); | ||
ngMocks.globalKeep(CommonModule, true); | ||
ngMocks.globalKeep(BrowserModule, true); |
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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; | ||
import { MockService, ngMocks } from 'ng-mocks'; | ||
|
||
import { MockService, ngMocks } from 'ng-mocks'; // eslint-disable-line import/order | ||
|
||
ngMocks.autoSpy('jest'); | ||
|
||
// In case, if you use @angular/router and Angular 14+. | ||
// You might want to set a mock of DefaultTitleStrategy as TitleStrategy. | ||
// A14 fix: making DefaultTitleStrategy to be a default mock for TitleStrategy | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; // eslint-disable-line import/order | ||
ngMocks.defaultMock(TitleStrategy, () => MockService(DefaultTitleStrategy)); | ||
|
||
// Usually, *ngIf and other declarations from CommonModule aren't expected to be mocked. | ||
// The code below keeps them. | ||
import { CommonModule } from '@angular/common'; // eslint-disable-line import/order | ||
import { ApplicationModule } from '@angular/core'; // eslint-disable-line import/order | ||
import { BrowserModule } from '@angular/platform-browser'; // eslint-disable-line import/order | ||
ngMocks.globalKeep(ApplicationModule, true); | ||
ngMocks.globalKeep(CommonModule, true); | ||
ngMocks.globalKeep(BrowserModule, true); |
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 |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
|
||
import { ngMocks } from 'ng-mocks'; | ||
import { MockService, ngMocks } from 'ng-mocks'; // eslint-disable-line import/order | ||
|
||
ngMocks.autoSpy('jest'); | ||
|
||
// In case, if you use @angular/router and Angular 14+. | ||
// You might want to set a mock of DefaultTitleStrategy as TitleStrategy. | ||
// A14 fix: making DefaultTitleStrategy to be a default mock for TitleStrategy | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; // eslint-disable-line import/order | ||
ngMocks.defaultMock(TitleStrategy, () => MockService(DefaultTitleStrategy)); | ||
|
||
// Usually, *ngIf and other declarations from CommonModule aren't expected to be mocked. | ||
// The code below keeps them. | ||
import { CommonModule } from '@angular/common'; // eslint-disable-line import/order | ||
import { ApplicationModule } from '@angular/core'; // eslint-disable-line import/order | ||
import { BrowserModule } from '@angular/platform-browser'; // eslint-disable-line import/order | ||
ngMocks.globalKeep(ApplicationModule, true); | ||
ngMocks.globalKeep(CommonModule, true); | ||
ngMocks.globalKeep(BrowserModule, true); |
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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; | ||
import { MockService, ngMocks } from 'ng-mocks'; | ||
|
||
import { MockService, ngMocks } from 'ng-mocks'; // eslint-disable-line import/order | ||
|
||
ngMocks.autoSpy('jest'); | ||
|
||
// In case, if you use @angular/router and Angular 14+. | ||
// You might want to set a mock of DefaultTitleStrategy as TitleStrategy. | ||
// A14 fix: making DefaultTitleStrategy to be a default mock for TitleStrategy | ||
import { DefaultTitleStrategy, TitleStrategy } from '@angular/router'; // eslint-disable-line import/order | ||
ngMocks.defaultMock(TitleStrategy, () => MockService(DefaultTitleStrategy)); | ||
|
||
// Usually, *ngIf and other declarations from CommonModule aren't expected to be mocked. | ||
// The code below keeps them. | ||
import { CommonModule } from '@angular/common'; // eslint-disable-line import/order | ||
import { ApplicationModule } from '@angular/core'; // eslint-disable-line import/order | ||
import { BrowserModule } from '@angular/platform-browser'; // eslint-disable-line import/order | ||
ngMocks.globalKeep(ApplicationModule, true); | ||
ngMocks.globalKeep(CommonModule, true); | ||
ngMocks.globalKeep(BrowserModule, true); |
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,32 @@ | ||
import collectDeclarations from '../resolve/collect-declarations'; | ||
|
||
import coreConfig from './core.config'; | ||
import { flatten } from './core.helpers'; | ||
import { AnyDeclaration } from './core.types'; | ||
import funcGetType from './func.get-type'; | ||
|
||
const iterator = <T = any>( | ||
source: AnyDeclaration<T>, | ||
callback: (source: AnyDeclaration<T>) => void, | ||
scanned = new Set<any>(), | ||
): void => { | ||
const meta = collectDeclarations(source); | ||
for (const decorator of meta.decorators) { | ||
for (const key of coreConfig.dependencies) { | ||
if (!meta[decorator][key]) { | ||
continue; | ||
} | ||
for (const def of flatten(meta[decorator][key])) { | ||
const declaration = funcGetType(def); | ||
if (!declaration || scanned.has(declaration)) { | ||
continue; | ||
} | ||
scanned.add(declaration); | ||
callback(declaration); | ||
iterator(declaration, callback, scanned); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
export default iterator; |
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
12 changes: 10 additions & 2 deletions
12
libs/ng-mocks/src/lib/mock-helper/mock-helper.global-exclude.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 |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import { AnyDeclaration } from '../common/core.types'; | ||
import funcIterateDeclaration from '../common/func.iterate-declaration'; | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
|
||
import funcGlobalPrepare from './func.global-prepare'; | ||
|
||
export default (source: AnyDeclaration<any>): void => { | ||
funcGlobalPrepare(); | ||
const action = (source: AnyDeclaration<any>): void => { | ||
ngMocksUniverse.getDefaults().set(source, ['exclude']); | ||
}; | ||
export default (source: AnyDeclaration<any>, recursively = false): void => { | ||
funcGlobalPrepare(); | ||
action(source); | ||
|
||
if (recursively) { | ||
funcIterateDeclaration(source, action); | ||
} | ||
}; |
Oops, something went wrong.