Permalink
Browse files

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

  • Loading branch information...
StrahilKazlachev committed Oct 2, 2016
1 parent f59dfc7 commit 76048e62af04651ce652bd9d4869668558769d39
Showing with 53 additions and 0 deletions.
  1. +8 −0 src/dialog-service.js
  2. +45 −0 test/unit/dialog-service.spec.js
View
@@ -72,6 +72,14 @@ export class DialogService {
closeAll(): Promise<void> {
return Promise.all(this.controllers.map((controller) => controller.cancel().catch((reason) => reason)));
}
+
+ /**
+ * 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) {
@@ -364,4 +364,49 @@ describe('the Dialog Service', function () {
done();
});
});
+
+ 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 76048e6

Please sign in to comment.