-
-
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.
Merge pull request #7440 from satanTime/issues/6928
fix(core): respecting the order of declaration in TestBed #6928
- Loading branch information
Showing
7 changed files
with
248 additions
and
34 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
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,15 +1,10 @@ | ||
import { flatten } from '../common/core.helpers'; | ||
import funcGetType from '../common/func.get-type'; | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
import markExported from '../mock/mark-exported'; | ||
|
||
export default (providers?: any[]): void => { | ||
for (const provider of flatten(providers ?? [])) { | ||
const provide = funcGetType(provider); | ||
|
||
const config = ngMocksUniverse.configInstance.get(provide) ?? {}; | ||
if (!config.exported) { | ||
config.exported = true; | ||
} | ||
ngMocksUniverse.configInstance.set(provide, config); | ||
const instance = funcGetType(provider); | ||
markExported(instance); | ||
} | ||
}; |
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,17 @@ | ||
import { getSourceOfMock } from '../common/func.get-source-of-mock'; | ||
import ngMocksUniverse from '../common/ng-mocks-universe'; | ||
|
||
export default (instanceDef: any, ngModuleDef?: any) => { | ||
const instance = getSourceOfMock(instanceDef); | ||
const configInstance = ngMocksUniverse.configInstance.get(instance) ?? { __set: true }; | ||
if (!configInstance.exported) { | ||
configInstance.exported = new Set(); | ||
} | ||
if (ngModuleDef) { | ||
configInstance.exported.add(getSourceOfMock(ngModuleDef)); | ||
} | ||
if (configInstance.__set) { | ||
configInstance.__set = undefined; | ||
ngMocksUniverse.configInstance.set(instance, configInstance); | ||
} | ||
}; |
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,154 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, NgModule, VERSION } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { | ||
MockBuilder, | ||
MockComponent, | ||
MockModule, | ||
ngMocks, | ||
} from 'ng-mocks'; | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/6928 | ||
describe('issue-6928', () => { | ||
if (Number.parseInt(VERSION.major, 10) < 14) { | ||
it('needs >=a14', () => { | ||
expect(true).toBeTruthy(); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
ngMocks.throwOnConsole(); | ||
|
||
@Component({ | ||
selector: 'app-shared1', | ||
template: '', | ||
}) | ||
class Shared1Component {} | ||
|
||
@Component({ | ||
selector: 'app-shared2', | ||
template: '', | ||
}) | ||
class Shared2Component {} | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
declarations: [Shared1Component, Shared2Component], | ||
exports: [Shared1Component, Shared2Component], | ||
}) | ||
class SharedModule {} | ||
|
||
@Component( | ||
{ | ||
selector: 'app-standalone', | ||
template: '<app-shared1></app-shared1>', | ||
standalone: true, | ||
imports: [CommonModule, SharedModule], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
) | ||
class StandaloneComponent {} | ||
|
||
@Component({ | ||
selector: 'app-my-component', | ||
template: | ||
'<app-shared2></app-shared2><app-standalone></app-standalone>', | ||
}) | ||
class MyComponent {} | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
StandaloneComponent, | ||
SharedModule, | ||
] as never /* TODO: remove after upgrade to a14 */, | ||
declarations: [MyComponent], | ||
}) | ||
class AppModule {} | ||
|
||
describe('missing module import', () => { | ||
it('throws on 2 declarations w/o ng-mocks', () => | ||
expect(() => { | ||
TestBed.configureTestingModule( | ||
{ | ||
imports: [StandaloneComponent], | ||
declarations: [MyComponent, Shared2Component], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
).compileComponents(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).toThrowError( | ||
/is part of the declarations of 2 modules: DynamicTestModule/, | ||
)); | ||
|
||
it('handles TestBed correctly w/ ng-mocks', () => { | ||
expect(() => { | ||
TestBed.configureTestingModule( | ||
{ | ||
imports: [MockComponent(StandaloneComponent)], | ||
declarations: [ | ||
MyComponent, | ||
MockComponent(Shared2Component), | ||
], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
).compileComponents(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('handles TestBed correctly w/ MockBuilder', () => { | ||
expect(() => { | ||
MockBuilder(MyComponent, [ | ||
StandaloneComponent, | ||
Shared2Component, | ||
]).then(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('correct module import', () => { | ||
it('passes w/o ng-mocks', () => | ||
expect(() => { | ||
TestBed.configureTestingModule( | ||
{ | ||
imports: [StandaloneComponent, SharedModule], | ||
declarations: [MyComponent], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
).compileComponents(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).not.toThrow()); | ||
|
||
it('passes w/ ng-mocks', () => | ||
expect(() => { | ||
TestBed.configureTestingModule( | ||
{ | ||
imports: [ | ||
MockComponent(StandaloneComponent), | ||
MockModule(SharedModule), | ||
], | ||
declarations: [MyComponent], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
).compileComponents(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).not.toThrow()); | ||
|
||
it('passes w/ MockBuilder', () => | ||
expect(() => { | ||
MockBuilder(MyComponent, [ | ||
StandaloneComponent, | ||
SharedModule, | ||
]).then(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).not.toThrow()); | ||
|
||
it('passes w/ MockBuilder and AppModule', () => | ||
expect(() => { | ||
MockBuilder(MyComponent, [ | ||
AppModule, | ||
Shared2Component, | ||
]).then(); | ||
TestBed.createComponent(MyComponent).detectChanges(); | ||
}).not.toThrow()); | ||
}); | ||
}); |