Skip to content

Instantly share code, notes, and snippets.

@Flayed
Created April 18, 2018 12:55
Show Gist options
  • Save Flayed/fb363918ce91b8c279c586c46c017b76 to your computer and use it in GitHub Desktop.
Save Flayed/fb363918ce91b8c279c586c46c017b76 to your computer and use it in GitHub Desktop.
testing modal dialogs with jasmine
// Create the test module
@NgModule({
imports: [NgbModule.forRoot(), CommonModule],
exports: [YesNoModalComponent, MyCustomComponent],
declarations: [YesNoModalComponent, MyCustomComponent],
entryComponents: [YesNoModalComponent]
})
class DialogTestModule { }
describe('MyTestComponent', () => {
let component: PickDefinitionComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyTestComponent],
providers: [ModalService], // ModalService includes the YesNoModalComponent
imports: [NgbModule.forRoot(), DialogTestModule], // Import the test module
})
.compileComponents();
}));
});
beforeEach(() => {
fixture = TestBed.createComponent(MyTestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should call YesNoDialog', async () => {
// Spy on the yesNoDialog
const modalService = TestBed.get(ModalService);
const yesNoDialogSpy = spyOn(modalService, 'openYesNoDialog')
.and.returnValue(new Promise<boolean>((resolve, reject) => {
resolve(true);
}));
await component.showModal();
await fixture.whenStable();
await expect(yesNoDialogSpy).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment