I have a simple component, which does not inject the DomSanitizer. Let's say it is
export class ExampleComponent {
@Input()
public safeHtml: SafeHtml | undefined;
}
How can I use the DomSanitizer inside a unit test? I've tried providing and injecting it. Here goes my spec.ts file:
describe('ExampleComponent', () => {
let component: ExampleComponent;
let fixture: ComponentFixture<ExampleComponent>;
let sanitizer: DomSanitizer;
await TestBed.configureTestingModule({
beforeEach(async() => {
declarations: [ExampleComponent],
providers: [DomSanitizer] // it is not injected by ExampleComponent
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ExampleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
sanitizr = TestBed.inject(DomSanitizer); // my attempt, though...
});
it('should work with SafeHtml input', () => {
expect(component).toBeTruthy();
let text = 'bla­bla';
let safeText: SafeHtml = sanitizer.bypassSecurityTrustHtml(text); // TypeError here
component.safeHtml = safeText;
expect(true);
});
}
The TypeError says: TypeError: sanitizr.bypassSecurityTrustHtml is not a function
.
Is there a way to make use of a real DoMSanitizer in a testbed even if the real component does not use it?