-
-
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.
fix(core): supports mocks for viewProviders #726
- Loading branch information
Showing
4 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
libs/ng-mocks/src/lib/common/core.reflect.directive-resolve.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,10 @@ | ||
import { MockDirectiveResolver } from '@angular/compiler/testing'; | ||
import { Directive } from '@angular/core'; | ||
import { Component, Directive } from '@angular/core'; | ||
|
||
import coreReflectBodyCatch from './core.reflect.body-catch'; | ||
import coreReflectBodyGlobal from './core.reflect.body-global'; | ||
|
||
const coreReflectDirective = coreReflectBodyGlobal(MockDirectiveResolver); | ||
|
||
export default (def: any): Directive => coreReflectBodyCatch((arg: any) => coreReflectDirective().resolve(arg))(def); | ||
export default (def: any): Directive & Partial<Component> => | ||
coreReflectBodyCatch((arg: any) => coreReflectDirective().resolve(arg))(def); |
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,90 @@ | ||
import { Component, Injectable, NgModule } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, MockRenderFactory, ngMocks } from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class TargetService { | ||
public readonly name = 'target'; | ||
} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: `{{ service.name }}`, | ||
}) | ||
class TargetComponent { | ||
public constructor(public readonly service: TargetService) {} | ||
} | ||
|
||
@Component({ | ||
selector: 'view', | ||
template: `<ng-content></ng-content>`, | ||
viewProviders: [TargetService], | ||
}) | ||
class ViewComponent {} | ||
|
||
@Component({ | ||
providers: [TargetService], | ||
selector: 'provider', | ||
template: `<ng-content></ng-content>`, | ||
}) | ||
class ProviderComponent {} | ||
|
||
@NgModule({ | ||
declarations: [TargetComponent, ViewComponent, ProviderComponent], | ||
exports: [TargetComponent, ViewComponent, ProviderComponent], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('issue-726', () => { | ||
const view = MockRenderFactory(`<view><target></target></view>`); | ||
const provider = MockRenderFactory( | ||
`<provider><target></target></provider>`, | ||
); | ||
const viewComponent = MockRenderFactory(ViewComponent); | ||
|
||
describe('TestBed', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [TargetModule], | ||
}).compileComponents(), | ||
); | ||
beforeEach(view.configureTestBed); | ||
beforeEach(provider.configureTestBed); | ||
beforeEach(viewComponent.configureTestBed); | ||
|
||
it('finds the view provider', () => { | ||
// TargetComponent doesn't have the access to TargetService. | ||
expect(view).toThrowError(/No provider for TargetService/); | ||
|
||
// Container knows how to provide TargetService for its views. | ||
expect(provider).not.toThrow(); | ||
|
||
// TargetService is accessed directly view ViewComponent. | ||
const fixture = viewComponent(); | ||
expect(() => | ||
ngMocks.get(fixture.point, TargetService), | ||
).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('MockBuilder', () => { | ||
beforeEach(() => MockBuilder(TargetComponent, TargetModule)); | ||
beforeEach(view.configureTestBed); | ||
beforeEach(provider.configureTestBed); | ||
beforeEach(viewComponent.configureTestBed); | ||
|
||
it('finds the view provider', () => { | ||
// TargetComponent doesn't have the access to TargetService. | ||
expect(view).toThrowError(/No provider for TargetService/); | ||
|
||
// Container knows how to provide TargetService for its views. | ||
expect(provider).not.toThrow(); | ||
|
||
// TargetService is accessed directly view ViewComponent. | ||
const fixture = viewComponent(); | ||
expect(() => | ||
ngMocks.get(fixture.point, TargetService), | ||
).not.toThrow(); | ||
}); | ||
}); | ||
}); |