// b) The one I am using here. Attaching to vm/this because AngularJS is performing new MyController().
vm.doModal = function (size) {
var cars = ['car1', 'car2', 'car3'];
+ // The modalInstance object will be seen from here and it will also be injected in CarsErrorModal controller
var modalInstance = $modal.open({
animation: true,
templateUrl: 'app/cars/cars-error-modal.html',
});
modalInstance.result.then(function (selectedItem) {
+ // following this path after calling modalInstance.close from either this controller or
+ // CarsErrorModal controller.
vm.selected = selectedItem;
}, function (reason) {
+ // following this path after calling modalInstance.dismiss from either this controller or
+ // CarsErrorModal controller.
if (reason === '$uibUnscheduledDestruction') {
console.log('Modal\'s scope destroyed by unexpected mechanism');
}
});
$timeout(function() {
- console.log('closed by tiemout at: ' + new Date());
- modalInstance.close('closed by tiemout');
+ console.log('closed by timeout at: ' + new Date());
+ modalInstance.close('closed by timeout');
}, 5000);
$timeout(function() {
- console.log('dismissed by tiemout at: ' + new Date());
- modalInstance.dismiss('dismissed by tiemout');
+ console.log('dismissed by timeout at: ' + new Date());
+ modalInstance.dismiss('dismissed by timeout');
}, 10000);
};
}
};
})
};
+ var $modalInstance = {
+ close: jasmine.createSpy('$modalInstance.close'),
+ dismiss: jasmine.createSpy('$modalInstance.dismiss'),
+ result: {
+ then: jasmine.createSpy('$modalInstance.result.then')
+ .and.callFake(function(successCallback, errorCallback) {
+ if (isSuccessCallBack) {
+ successCallback(onFulfilledValue);
+ } else {
+ errorCallback(onRejectedValue);
+ }
+ })
+ },
+ opened: {
+ then: jasmine.createSpy('$modalInstance.opened.then')
+ .and.callFake(function(successCallback, errorCallback) {
+ if (isSuccessCallBack) {
+ successCallback(onFulfilledValue);
+ } else {
+ errorCallback(onRejectedValue);
+ }
+ })
+ },
+ rendered: {
+ then: jasmine.createSpy('$modalInstance.rendered.then')
+ .and.callFake(function(successCallback, errorCallback) {
+ if (isSuccessCallBack) {
+ successCallback(onFulfilledValue);
+ } else {
+ errorCallback(onRejectedValue);
+ }
+ })
+ }
+ };
+ var $modal = {
+ open: jasmine.createSpy('$modal.open')
+ .and.callFake(function() {
+ return $modalInstance;
+ })
+ };
+ var $timeout;
var Cars;
var $q;
beforeEach(function() {
module('app.cars');
- inject(function($controller, $modal, $timeout, _$q_) {
+ inject(function($controller, _$timeout_, _$q_) {
Cars = $controller('Cars', {
$modal: $modal,
- $timeout: $timeout,
+ $timeout: _$timeout_,
cars: cars
});
$q = _$q_;
+ $timeout = _$timeout_;
});
});
});
it('should invoke GET all cars in service with error: alternative way', function () {
-
isSuccessCallBack = false;
spyOn(Cars, 'doModal')
.and.callFake(function() {
expect(cars.getAll).toHaveBeenCalled();
expect(Cars.doModal).toHaveBeenCalled();
+
+ isSuccessCallBack = true;
+ });
+
+ it('should invoke $modal.open', function () {
+
+ Cars.doModal('lg');
+
+ expect($modal.open).toHaveBeenCalled();
+ });
+
+ it('should invoke $modalInstance.result with success', function () {
+
+ Cars.doModal('lg');
+
+ expect($modalInstance.result.then).toHaveBeenCalled();
+ expect(Cars.selected).toEqual(onFulfilledValue);
+ });
+
+ it('should invoke $modalInstance.result with error', function () {
+ isSuccessCallBack = false;
+
+ Cars.doModal('lg');
+
+ expect($modalInstance.result.then).toHaveBeenCalled();
+
+ isSuccessCallBack = true;
+ });
+
+ it('should invoke $modalInstance.opened with success', function () {
+
+ Cars.doModal('lg');
+
+ expect($modalInstance.opened.then).toHaveBeenCalled();
+
+ });
+
+ it('should invoke $modalInstance.opened with error', function () {
+ isSuccessCallBack = false;
+
+ Cars.doModal('lg');
+
+ expect($modalInstance.opened.then).toHaveBeenCalled();
+
+ isSuccessCallBack = true;
+ });
+
+ it('should invoke $modalInstance.rendered with success', function () {
+
+ Cars.doModal('lg');
+
+ expect($modalInstance.rendered.then).toHaveBeenCalled();
+
+ });
+
+ it('should invoke $modalInstance.rendered with error', function () {
+ isSuccessCallBack = false;
+
+ Cars.doModal('lg');
+
+ expect($modalInstance.rendered.then).toHaveBeenCalled();
+
+ isSuccessCallBack = true;
+ });
+
+ it('should invoke $modalInstance.close because of timeout', function () {
+
+ Cars.doModal('lg');
+ $timeout.flush();
+
+ expect($modalInstance.close).toHaveBeenCalledWith('closed by timeout');
+
+ });
+
+ it('should invoke $modalInstance.dismiss because of timeout', function () {
+
+ Cars.doModal('lg');
+ $timeout.flush();
+
+ expect($modalInstance.dismiss).toHaveBeenCalledWith('dismissed by timeout');
+
});
});