Skip to content

fix(cdk/dialog): use config injector if provided #25331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/cdk/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Directive,
Inject,
Injector,
Optional,
TemplateRef,
ViewChild,
ViewContainerRef,
Expand Down Expand Up @@ -125,6 +126,21 @@ describe('Dialog', () => {
);
});

it('should use custom test injector', () => {
// Create a custom injector for the component.
let viewContainerFixtureChocolateInjector = TestBed.createComponent(PizzaSnack);
viewContainerFixtureChocolateInjector.detectChanges();
let chocolateInjector = new ChocolateInjector(
viewContainerFixtureChocolateInjector.componentInstance.injector,
);

let dialogRef = dialog.open(PizzaSnack, {
injector: chocolateInjector,
});

expect(dialogRef.componentInstance!.snack.toString()).toBe('Chocolate');
});

it('should open a dialog with a component and no ViewContainerRef', () => {
let dialogRef = dialog.open(PizzaMsg);

Expand Down Expand Up @@ -1223,3 +1239,26 @@ class DialogWithoutFocusableElements {}
encapsulation: ViewEncapsulation.ShadowDom,
})
class ShadowDomComponent {}

class Chocolate {
toString() {
return 'Chocolate';
}
}

class ChocolateInjector {
constructor(public parentInjector: Injector) {}

get(token: any) {
return token === Chocolate ? new Chocolate() : this.parentInjector.get<any>(token);
}
}

/** Simple component for testing custom injector. */
@Component({
selector: 'pizza-snack',
template: '<p>Pizza</p><p>{{snack}}</p><ng-content></ng-content>',
})
class PizzaSnack {
constructor(@Optional() public snack: Chocolate, public injector: Injector) {}
}
2 changes: 1 addition & 1 deletion src/cdk/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class Dialog implements OnDestroy {
});
}

return Injector.create({parent: userInjector || this._injector, providers});
return Injector.create({parent: config.injector || userInjector || this._injector, providers});
}

/**
Expand Down