Solución:
Prueba esto:
describe('Confirmation Dialog Component', () => {
const model: ConfirmationDialogModel = {
ActionButton: 'Delete',
SupportingText: 'Are you sure?',
};
let component: ConfirmationDialogComponent;
let fixture: ComponentFixture<ConfirmationDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
ConfirmationDialogComponent
],
imports: [
MatButtonModule,
MatDialogModule
],
providers: [
{
// I was expecting this will pass the desired value
provide: MAT_DIALOG_DATA,
useValue: model
}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ConfirmationDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', async(() => {
expect(component).toBeTruthy();
}));
it('should close dialog when close button clicked', fakeAsync(() => {
component.onCloseButtonClicked(0);
fixture.detectChanges();
tick();
expect(mockMainDialogRef.close.calls.count()).toBe(1, 'dialog closed');
}));
});
Aquí hay un ejemplo de cómo inyectar MAT_DIALOG_DATA
en una prueba unitaria:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MatDialogModule, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { ConfirmDialogComponent } from './confirm-dialog.component';
describe('ConfirmDialogComponent', () => {
let component: ConfirmDialogComponent;
let fixture: ComponentFixture<ConfirmDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ConfirmDialogComponent ],
imports: [ MatDialogModule ], // add here
providers: [
{ provide: MAT_DIALOG_DATA, useValue: {} } // add here
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ConfirmDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)