-
-
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 #751 from satanTime/issues/736
fix(core): allowing spies on ComponentFactoryResolver.resolveComponentFactory #736
- Loading branch information
Showing
3 changed files
with
84 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// tslint:disable strict-type-predicates | ||
|
||
import { | ||
Component, | ||
ComponentFactoryResolver, | ||
OnInit, | ||
} from '@angular/core'; | ||
import { getMockedNgDefOf, MockBuilder, MockRender } from 'ng-mocks'; | ||
|
||
@Component({ | ||
selector: 'modal', | ||
template: 'modal', | ||
}) | ||
class ModalComponent {} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: 'target', | ||
}) | ||
class TargetComponent implements OnInit { | ||
public constructor( | ||
public readonly componentFactoryResolver: ComponentFactoryResolver, | ||
) {} | ||
|
||
public ngOnInit(): void { | ||
this.componentFactoryResolver.resolveComponentFactory( | ||
ModalComponent, | ||
); | ||
} | ||
} | ||
|
||
describe('issue-736', () => { | ||
beforeEach(() => | ||
MockBuilder(TargetComponent) | ||
.mock(ModalComponent) | ||
.provide({ | ||
provide: ComponentFactoryResolver, | ||
useValue: { | ||
resolveComponentFactory: | ||
typeof jest !== 'undefined' | ||
? jest | ||
.fn() | ||
.mockName( | ||
'ComponentFactoryResolver.resolveComponentFactory', | ||
) | ||
: jasmine.createSpy( | ||
'ComponentFactoryResolver.resolveComponentFactory', | ||
), | ||
}, | ||
}), | ||
); | ||
|
||
it('allows to mock resolveComponentFactory', () => { | ||
// creating fixture without a render | ||
const fixture = MockRender(TargetComponent, undefined, false); | ||
|
||
// getting current instance of mock ComponentFactoryResolver | ||
const componentFactoryResolver = | ||
fixture.debugElement.injector.get(ComponentFactoryResolver); | ||
|
||
// its spied resolveComponentFactory shouldn't be called | ||
// the bug was that it is not a spy anymore. | ||
expect( | ||
componentFactoryResolver.resolveComponentFactory, | ||
).not.toHaveBeenCalled(); | ||
|
||
// triggering ngOnInit | ||
fixture.detectChanges(); | ||
|
||
// resolveComponentFactory should have been called | ||
expect( | ||
componentFactoryResolver.resolveComponentFactory, | ||
).toHaveBeenCalledWith(getMockedNgDefOf(ModalComponent)); | ||
}); | ||
}); |