Created
April 18, 2018 12:55
-
-
Save Flayed/fb363918ce91b8c279c586c46c017b76 to your computer and use it in GitHub Desktop.
testing modal dialogs with jasmine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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