Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 8bf174b

Browse files
devversionThomasBurleson
authored andcommitted
fix(interimElement): show method should cancel existing interim element
* Currently the interimElement factory always `hides` the existing interim element, if present. This is no correct, because the interimElement should either cancel the previous existing interim element, otherwise the interimElement service will resolve the deferred promise with `undefined`, instead of rejecting properly. Fixes #8533. Closes #8600
1 parent b487834 commit 8bf174b

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/components/dialog/dialog.spec.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,49 @@ describe('$mdDialog', function() {
634634
.parent(parent)
635635
.textContent('Hello world')
636636
.placeholder('placeholder text')
637-
)
637+
);
638638

639639
runAnimation(parent.find('md-dialog'));
640640

641641
expect($document.activeElement).toBe(parent[0].querySelector('input'));
642642
}));
643643

644+
it('should cancel the first dialog when opening a second', inject(function($mdDialog, $rootScope, $document) {
645+
var firstParent = angular.element('<div>');
646+
var secondParent = angular.element('<div>');
647+
var isCancelled = false;
648+
649+
$mdDialog.show(
650+
$mdDialog
651+
.prompt()
652+
.parent(firstParent)
653+
.textContent('Hello world')
654+
.placeholder('placeholder text')
655+
).catch(function() {
656+
isCancelled = true;
657+
});
658+
659+
$rootScope.$apply();
660+
runAnimation();
661+
662+
expect(firstParent.find('md-dialog').length).toBe(1);
663+
664+
$mdDialog.show(
665+
$mdDialog
666+
.prompt()
667+
.parent(secondParent)
668+
.textContent('Hello world')
669+
.placeholder('placeholder text')
670+
);
671+
672+
$rootScope.$apply();
673+
runAnimation();
674+
675+
expect(firstParent.find('md-dialog').length).toBe(0);
676+
expect(secondParent.find('md-dialog').length).toBe(1);
677+
expect(isCancelled).toBe(true);
678+
}));
679+
644680
it('should submit after ENTER key', inject(function($mdDialog, $rootScope, $timeout, $mdConstant) {
645681
jasmine.mockElementFocus(this);
646682
var parent = angular.element('<div>');

src/core/services/interimElement/interimElement.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,10 @@ function InterimElementProvider() {
286286
function show(options) {
287287
options = options || {};
288288
var interimElement = new InterimElement(options || {});
289-
var hideExisting = !options.skipHide && stack.length ? service.hide() : $q.when(true);
289+
// When an interim element is currently showing, we have to cancel it.
290+
// Just hiding it, will resolve the InterimElement's promise, the promise should be
291+
// rejected instead.
292+
var hideExisting = !options.skipHide && stack.length ? service.cancel() : $q.when(true);
290293

291294
// This hide()s only the current interim element before showing the next, new one
292295
// NOTE: this is not reversible (e.g. interim elements are not stackable)

src/core/services/interimElement/interimElement.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,24 @@ describe('$$interimElement service', function() {
342342

343343
}));
344344

345+
it('should cancel a previous shown interim element', inject(function() {
346+
var isCancelled = false;
347+
348+
Service.show({
349+
template: '<div>First Interim</div>'
350+
}).catch(function() {
351+
isCancelled = true;
352+
});
353+
354+
// Once we show the second interim, the first interim should be cancelled and the promise
355+
// should be rejected with no reason.
356+
Service.show({
357+
template: '<div>Second Interim</div>'
358+
});
359+
360+
expect(isCancelled).toBe(true);
361+
}));
362+
345363
it('forwards options to $mdCompiler', inject(function() {
346364
var options = {template: '<testing />'};
347365
Service.show(options);

0 commit comments

Comments
 (0)