Testing the delete functionality of my app with a mock delete $modal to simulate cancelling or confirming deletion.
var modalInstanceMock=
{
result: {
then: function(confirmCallback, cancelCallback) {
//Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
}
},
confirmCallBack: function(item){
return true;
},
cancelCallback: function(type){
return false;
},
close: function( item ) {
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
this.result.confirmCallBack( item );
},
dismiss: function( type ) {
//The user clicked cancel on the modal dialog, call the stored cancel callback
this.result.cancelCallback( type );
}
};
Setting up the test scenario:
beforeEach(inject(function($modal) {
spyOn($modal, 'open').andReturn(modalInstanceMock);
}));
Successfully testing the delete functionality:
var newRes = scope.deleteCar(car);
scope.modalInstance.close("ok");
Encountering an issue with dismissing:
var newRes = scope.deleteCar(car);
scope.modalInstance.dismiss("ok");
Receiving a Type:error undefined is not a function at Object.modalInstanceMock.dismiss message.
Confused about why close works correctly but dismiss is throwing an error.