Skip to content

Commit

Permalink
fix: correct replacement of useExisting providers
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Feb 15, 2021
1 parent 20d9b27 commit 6908e5f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/ng-mocks/src/lib/mock/clone-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const processTokens = (mockType: AnyType<any>, provider: any) => {
const processOwnUseExisting = (sourceType: AnyType<any>, mockType: AnyType<any>, provider: any) => {
const provide = funcGetProvider(provider);
if (provider !== provide && provider.useExisting === sourceType) {
return toExistingProvider(provider, mockType);
return toExistingProvider(provide, mockType);
}
if (
provider !== provide &&
Expand Down
72 changes: 72 additions & 0 deletions tests/ng-mocks-render/use-existing.spec.ts
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));
});
});

0 comments on commit 6908e5f

Please sign in to comment.