-
-
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: correct replacement of useExisting providers
- Loading branch information
Showing
2 changed files
with
73 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { | ||
Component, | ||
ContentChild, | ||
Directive, | ||
Input, | ||
NgModule, | ||
TemplateRef, | ||
} from '@angular/core'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Directive({ | ||
selector: '[tpl]', | ||
}) | ||
class TplDirective { | ||
@Input('tpl') public readonly name: string | null = null; | ||
|
||
public constructor(public readonly tpl: TemplateRef<any>) {} | ||
} | ||
|
||
@Directive({ | ||
providers: [ | ||
{ | ||
provide: TplDirective, | ||
useExisting: MockDirective, | ||
}, | ||
], | ||
selector: '[mock]', | ||
}) | ||
class MockDirective { | ||
public constructor(public readonly tpl: TemplateRef<any>) {} | ||
} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: ` | ||
<component> | ||
<ng-template mock>rendered-mock</ng-template> | ||
</component> | ||
`, | ||
}) | ||
class TargetComponent {} | ||
|
||
@Component({ | ||
selector: 'component', | ||
template: ``, | ||
}) | ||
class MockComponent { | ||
@ContentChild(TplDirective, {} as any) | ||
public readonly directive?: TplDirective; | ||
} | ||
|
||
@NgModule({ | ||
declarations: [ | ||
TargetComponent, | ||
MockComponent, | ||
MockDirective, | ||
TplDirective, | ||
], | ||
imports: [CommonModule], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('ng-mocks-render:use-existing', () => { | ||
beforeEach(() => MockBuilder(TargetComponent, TargetModule)); | ||
|
||
it('substitutes in mocks correctly', () => { | ||
MockRender(TargetComponent); | ||
const component = ngMocks.findInstance(MockComponent); | ||
expect(component.directive).toEqual(jasmine.any(MockDirective)); | ||
}); | ||
}); |