Permalink
Browse files

feat(dialog-service): add .closeAll()

  • Loading branch information...
StrahilKazlachev committed Oct 2, 2016
1 parent 50de877 commit f59dfc71efa419255a737b5cab0889214b9f7fea
Showing with 53 additions and 0 deletions.
  1. +8 −0 src/dialog-service.js
  2. +45 −0 test/unit/dialog-service.spec.js
View
@@ -64,6 +64,14 @@ export class DialogService {
return settings.yieldController ? openResult : openResult.then(result => result.wasCancelled ? result : result.closeResult);
}
+
+ /**
+ * Closes all open dialogs at the time of invocation.
+ * @return Promise A promise that settles when all dialogs have closed.
+ */
+ closeAll(): Promise<void> {
+ return Promise.all(this.controllers.map((controller) => controller.cancel().catch((reason) => reason)));
+ }
}
function _createSettings(settings) {
@@ -319,4 +319,49 @@ describe('the Dialog Service', function () {
});
});
});
+
+ it('".open" properly propagates errors', (done) => {
+ let catchWasCalled = false;
+ let promise = dialogService.open({ viewModel: 'test/fixtures/non-existent' })
+ .catch(() => {
+ catchWasCalled = true;
+ }).then(() => {
+ expect(catchWasCalled).toBe(true);
+ done();
+ });
+ });
+
+ it('reports no active dialog after ".closeAll" has been invoked', (done) => {
+ const settings = { viewModel: TestElement };
+ const dialogsToOpen = 6;
+ let i = dialogsToOpen;
+ let controllersPromises = [];
+ let controllers;
+ let catchWasCalled = false;
+
+ expect(dialogService.hasActiveDialog).toBe(false);
+ while (i--) {
+ controllersPromises.push(dialogService.openAndYieldController(settings));
+ }
+ expect(dialogService.hasActiveDialog).toBe(false);
+ const closeAll = Promise.all(controllersPromises).then((ctrls) => {
+ controllers = ctrls;
+ ctrls.forEach((controller) => {
+ spyOn(controller, 'cancel').and.callThrough();
+ });
+ return dialogService.closeAll();
+ }).catch((reason => {
+ catchWasCalled = true;
+ })).then(() => {
+ expect(catchWasCalled).toBe(false);
+ if (catchWasCalled) {
+ return done();
+ }
+ controllers.forEach((controller) => {
+ expect(controller.cancel).toHaveBeenCalled();
+ });
+ expect(dialogService.controllers.length).toBe(0);
+ done();
+ });
+ });
});

0 comments on commit f59dfc7

Please sign in to comment.