I'm trying to test functionality which is basically doing,
- Open confirmation modal
- Return result of modal (button value which are OK or Cancel)
- If condition checks result
- Calling another function
My controller function is;
openPopup() {
confirmationModal.open().then((result) => {
if(result === 'OK') {
someService.doSomething()
.then(() => {
showSuccess();
});
}
}).finally(() => {
confirmationModal.close();
});
}
And my test is;
describe('confirmation modal', () => {
beforeEach(() => {
inject(($controller, _$q_) => {
var q = _$q_;
someService = {
doSomething: jasmine.createSpy()
};
var modalResult = {
then: function(callback) {
callback("OK");
}
};
confirmationModal = {
open: jasmine.createSpy().and.returnValue(q.when({ result: modalResult })),
close: jasmine.createSpy()
};
Ctrl = $controller('MainController', {
$scope: scope, confirmationModal: confirmationModal, someService: someService
});
});
});
it('should pass OK value', () => {
Ctrl.openPopup();
scope.$digest();
expect(someService.doSomething).toHaveBeenCalled();
});
});
When I run this through Karma - PhantomJS, I'm getting Expected spy unknown to have been called. which is I assume I can't go through If condition with modal result. Basically need to test conditional modal result If I can. When I test and expect open or close functions of confirmationModal, test passes but If I expect function after confirmationModal has been called, test fails.
I'm struggling with this maybe basic thing and I'm really sorry already If I miss some rules or my bad english.
Thanks already!