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

Commit 685c6fc

Browse files
committed
fix(panel): Make all tests wait for promises to resolve.
1 parent 0c4ad17 commit 685c6fc

File tree

2 files changed

+109
-106
lines changed

2 files changed

+109
-106
lines changed

src/components/panel/panel.js

Lines changed: 80 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ angular
155155
* @param {Object=} opt_config Specific configuration object that may contain
156156
* the properties defined in `$mdPanel.create`.
157157
*
158-
* @returns {MdPanelRef} panelRef
158+
* @returns {angular.$q.Promise<MdPanelRef>} panelRef A promise that resolves
159+
* to an instance of the panel.
159160
*/
160161

161162

@@ -245,7 +246,7 @@ angular
245246

246247
/**
247248
* @ngdoc method
248-
* @name MdPanelRef#attachOnly
249+
* @name MdPanelRef#attach
249250
* @description
250251
* Create the panel elements and attach them to the DOM. The panel will be
251252
* hidden by default.
@@ -258,7 +259,7 @@ angular
258259
* @ngdoc method
259260
* @name MdPanelRef#detach
260261
* @description
261-
* Removes the panel from the DOM. This will hide the panel before removing it.
262+
* Removes the panel from the DOM. This will NOT hide the panel before removing it.
262263
*
263264
* @returns {!angular.$q.Promise} A promise that is resolved when the panel is
264265
* detached.
@@ -612,26 +613,23 @@ function MdPanelService($rootElement, $rootScope, $injector) {
612613
focusOnOpen: true,
613614
fullscreen: false,
614615
hasBackdrop: false,
615-
transformTemplate: angular.bind(this, this.wrapTemplate_),
616+
transformTemplate: angular.bind(this, this._wrapTemplate),
616617
trapFocus: false,
617618
zIndex: defaultZIndex
618619
};
619620

620-
/** @private {!angular.Scope} */
621-
this._$rootScope = $rootScope;
622-
623621
/** @private {!Object} */
624622
this._config = {};
625623

626-
/** @private {!angular.$injector} */
627-
this._$injector = $injector;
628-
629-
/** @private {!angular.$injector} */
624+
/** @private @const */
630625
this._$rootScope = $rootScope;
631626

632-
/** @private {!angular.JQLite} */
627+
/** @private @const */
633628
this._$rootElement = $rootElement;
634629

630+
/** @private @const */
631+
this._$injector = $injector;
632+
635633
/**
636634
* Default animations that can be used within the panel.
637635
* @type {enum}
@@ -678,33 +676,13 @@ MdPanelService.prototype.create = function(opt_config) {
678676
/**
679677
* Creates and opens a panel with the specified options.
680678
* @param {!Object=} opt_config Configuration object for the panel.
681-
* @returns {!MdPanelRef} The panel created from create.
679+
* @returns {!angular.$q.Promise<MdPanelRef>} The panel created from create.
682680
*/
683681
MdPanelService.prototype.open = function(opt_config) {
684682
var panelRef = this.create(opt_config);
685-
panelRef.open();
686-
return panelRef;
687-
};
688-
689-
690-
/**
691-
* Wraps the users template in two elements, md-panel-container, which covers
692-
* the entire attachTo element, and md-panel, which contains only the
693-
* template. This allows the panel control over positioning, animations,
694-
* and similar properties.
695-
*
696-
* @param {string} origTemplate The original template.
697-
* @returns {string} The wrapped template.
698-
* @private
699-
*/
700-
MdPanelService.prototype.wrapTemplate_ = function(origTemplate) {
701-
var template = origTemplate || '';
702-
703-
return '<div class="md-panel-outer-wrapper">' +
704-
'<div class="md-panel">' +
705-
template +
706-
'</div>' +
707-
'</div>';
683+
return panelRef.open().then(function() {
684+
return panelRef;
685+
});
708686
};
709687

710688

@@ -730,6 +708,25 @@ MdPanelService.prototype.newPanelAnimation = function() {
730708
};
731709

732710

711+
/**
712+
* Wraps the users template in two elements, md-panel-outer-wrapper, which
713+
* covers the entire attachTo element, and md-panel, which contains only the
714+
* template. This allows the panel control over positioning, animations,
715+
* and similar properties.
716+
*
717+
* @param {string} origTemplate The original template.
718+
* @returns {string} The wrapped template.
719+
* @private
720+
*/
721+
MdPanelService.prototype._wrapTemplate = function(origTemplate) {
722+
var template = origTemplate || '';
723+
724+
return '' +
725+
'<div class="md-panel-outer-wrapper">' +
726+
' <div class="md-panel">' + template + '</div>' +
727+
'</div>';
728+
};
729+
733730
/*****************************************************************************
734731
* MdPanelRef *
735732
*****************************************************************************/
@@ -828,8 +825,8 @@ function MdPanelRef(config, $injector) {
828825
* Opens an already created and configured panel. If the panel is already
829826
* visible, does nothing.
830827
*
831-
* @returns {!angular.$q.Promise} A promise that is resolved when the panel
832-
* is opened and animations finish.
828+
* @returns {!angular.$q.Promise<MdPanelRef>} A promise that is resolved when
829+
* the panel is opened and animations finish.
833830
*/
834831
MdPanelRef.prototype.open = function() {
835832
if (this._openPromise) {
@@ -841,9 +838,12 @@ MdPanelRef.prototype.open = function() {
841838
// TODO(ErinCoughlan) - Cancel any in-progress actions.
842839

843840
var self = this;
844-
this._openPromise = this.attachOnly()
841+
this._openPromise = this.attach()
845842
.then(function() {
846843
return self.show();
844+
}, this._$q.reject)
845+
.then(function() {
846+
return self; // Return a reference to the MdPanelRef.
847847
}, this._$q.reject);
848848

849849
return this._openPromise;
@@ -877,10 +877,10 @@ MdPanelRef.prototype.close = function() {
877877
/**
878878
* Attaches the panel. The panel will be hidden afterwards.
879879
*
880-
* @returns {!angular.$q.Promise} A promise that is resolved when the panel is
881-
* attached.
880+
* @returns {!angular.$q.Promise<MdPanelRef>} A promise that is resolved when
881+
* the panel is attached.
882882
*/
883-
MdPanelRef.prototype.attachOnly = function() {
883+
MdPanelRef.prototype.attach = function() {
884884
if (this.isAttached) {
885885
return this._attachPromise;
886886
}
@@ -903,7 +903,7 @@ MdPanelRef.prototype.attachOnly = function() {
903903

904904

905905
/**
906-
* Detaches the panel. Will hide the panel first if visible.
906+
* Only detaches the panel. Will NOT hide the panel first.
907907
*
908908
* @returns {!angular.$q.Promise} A promise that is resolved when the panel is
909909
* detached.
@@ -935,12 +935,10 @@ MdPanelRef.prototype.detach = function() {
935935
return self._$q.resolve(self);
936936
};
937937

938-
this._detachPromise = this.hide().then(function () {
939-
return self._$q.all([
940-
detachFn(),
941-
self._backdropRef ? self._backdropRef.detach() : self._$q.resolve(self)
942-
]);
943-
}, this._$q.reject);
938+
this._detachPromise = self._$q.all([
939+
detachFn(),
940+
self._backdropRef ? self._backdropRef.detach() : self._$q.resolve(self)
941+
]);
944942

945943
return this._detachPromise;
946944
};
@@ -969,7 +967,7 @@ MdPanelRef.prototype.show = function() {
969967
var animatePromise = function() {
970968
self.removeClass(MD_PANEL_HIDDEN);
971969
return self._animateOpen();
972-
}
970+
};
973971

974972
this._showPromise = this._$q.all([
975973
this._backdropRef ? this._backdropRef.show() : this._$q.resolve(self),
@@ -1084,7 +1082,7 @@ MdPanelRef.prototype._createPanel = function() {
10841082
self._config.locals = {};
10851083
}
10861084
self._config.locals.mdPanelRef = self;
1087-
self._$mdCompiler.compile(self._config)
1085+
return self._$mdCompiler.compile(self._config)
10881086
.then(function(compileData) {
10891087
self._panelContainer = compileData.link(self._config['scope']);
10901088
getElement(self._config['attachTo']).append(self._panelContainer);
@@ -1104,16 +1102,19 @@ MdPanelRef.prototype._createPanel = function() {
11041102
getElement(self._config['attachTo']));
11051103
}
11061104

1107-
self._addStyles();
11081105
self._configureTrapFocus();
1109-
return resolve(self);
1106+
return self._addStyles().then(function() {
1107+
self._panelContainer.addClass(MD_PANEL_HIDDEN);
1108+
resolve(self);
1109+
}, reject);
11101110
}, reject);
11111111
});
11121112
};
11131113

11141114

11151115
/**
11161116
* Adds the styles for the panel, such as positioning and z-index.
1117+
* @return {!angular.$q.Promise}
11171118
* @private
11181119
*/
11191120
MdPanelRef.prototype._addStyles = function() {
@@ -1123,34 +1124,42 @@ MdPanelRef.prototype._addStyles = function() {
11231124

11241125
if (this._config['fullscreen']) {
11251126
this._panelEl.addClass('_md-panel-fullscreen');
1126-
return; // Don't setup positioning.
1127+
return this._$q.resolve(this); // Don't setup positioning.
11271128
}
11281129

1129-
// Wait for angular to finish processing the template, then position it
1130-
// correctly. This is necessary so that the panel will have a defined height
1131-
// and width.
1132-
this._$rootScope['$$postDigest'](angular.bind(this, this._configurePosition));
1133-
this._panelContainer.addClass(MD_PANEL_HIDDEN);
1130+
return this._configurePosition();
11341131
};
11351132

11361133

11371134
/**
11381135
* Configure the position of the panel.
1136+
* @return {!angular.$q.Promise}
11391137
* @private
11401138
*/
11411139
MdPanelRef.prototype._configurePosition = function() {
11421140
var positionConfig = this._config['position'];
1141+
if (!positionConfig) {
1142+
return this._$q.resolve(this);
1143+
}
11431144

1144-
if (!positionConfig) { return; }
1145-
1146-
this._panelEl.css('top', positionConfig.getTop(this._panelEl));
1147-
this._panelEl.css('bottom', positionConfig.getBottom(this._panelEl));
1148-
this._panelEl.css('left', positionConfig.getLeft(this._panelEl));
1149-
this._panelEl.css('right', positionConfig.getRight(this._panelEl));
1145+
var self = this;
1146+
return this._$q(function(resolve) {
1147+
// Wait for angular to finish processing the template, then position it
1148+
// correctly. This is necessary so that the panel will have a defined
1149+
// height and width.
1150+
return self._$rootScope['$$postDigest'](function () {
1151+
self._panelEl.css('top', positionConfig.getTop(self._panelEl));
1152+
self._panelEl.css('bottom', positionConfig.getBottom(self._panelEl));
1153+
self._panelEl.css('left', positionConfig.getLeft(self._panelEl));
1154+
self._panelEl.css('right', positionConfig.getRight(self._panelEl));
1155+
1156+
// Use the vendor prefixed version of transform.
1157+
var prefixedTransform = self._$mdConstant.CSS.TRANSFORM;
1158+
self._panelEl.css(prefixedTransform, positionConfig.getTransform());
11501159

1151-
// Use the vendor prefixed version of transform.
1152-
var prefixedTransform = this._$mdConstant.CSS.TRANSFORM;
1153-
this._panelEl.css(prefixedTransform, positionConfig.getTransform());
1160+
return resolve(self);
1161+
});
1162+
});
11541163
};
11551164

11561165

@@ -1195,7 +1204,7 @@ MdPanelRef.prototype._createBackdrop = function() {
11951204
zIndex: this._config.zIndex - 1
11961205
}
11971206
this._backdropRef = this._$mdPanel.create(backdropConfig);
1198-
return this._backdropRef.attachOnly();
1207+
return this._backdropRef.attach();
11991208
}
12001209
return this._$q.resolve();
12011210
};
@@ -1362,7 +1371,7 @@ MdPanelRef.prototype._animateClose = function() {
13621371

13631372
var self = this;
13641373
return this._$q(function(resolve, reject) {
1365-
animationConfig.animateClose(self._$q).then(function(){
1374+
animationConfig.animateClose(self._$q).then(function() {
13661375
self.removeClass('md-panel-is-showing');
13671376
return resolve(self);
13681377
}, reject);
@@ -2014,7 +2023,7 @@ MdPanelAnimation.prototype.animateOpen = function(panelEl, animator) {
20142023

20152024
/**
20162025
* Animate the panel close.
2017-
* @param $q
2026+
* @param {!angular.$q} $q
20182027
* @returns {!angular.$q.Promise}
20192028
*/
20202029
MdPanelAnimation.prototype.animateClose = function($q) {

0 commit comments

Comments
 (0)