Is there a way to submit a form in unit test ?
I have a form like
<!-- notice that the controller is defined using controllerAs syntax -->
<form name="sign_up" ng-submit="auth.signUp(email, password)" role="form">
for example
SignUpCtrl = new _$controller_ 'SignUpCtrl', $scope: scope
SignUpForm = scope.sign_up # this holds the signup form
it '#signUp(email, password)', ->
controllerSignUpMethod = sinon.spy SignUpCtrl, 'signUp'
SignUpForm.email.$setViewValue 'em@il.com'
SignUpForm.password.$setViewValue 'pa$$word'
SignUpForm.confirm_password.$setViewValue 'pa$$word'
# I can't find a way to submit the form here
expect(controllerSignUpMethod).to.be.called
the SignUpForm
is angular fromController
.. but it I can't find a way to use it or the scope
to submit the form...
One last thing... due some unfortunates I cannot use protractor
or any other e2e
package... my only option is to go with the unit tests... and the views (signup form) could be changed later... so there must exist a unit test for it to insure that it's still calling the correct controller with the correct parameters... the SignUpForm
is loaded from templates in each test... so it's the actual form not just a mocked $templateCache
of it
since the controller is defined using controllerAs: 'auth'
syntax..
use the following code :
SignUpCtrl = new _$controller_ 'SignUpCtrl as auth', $scope: scope
SignUpForm = scope.sign_up
it '#signUp(email, password)', ->
controllerSignUpMethod = sinon.spy SignUpCtrl, 'signUp'
SignUpForm.email.$setViewValue 'em@i.l'
SignUpForm.password.$setViewValue 'pa$$word'
SignUpForm.confirm_password.$setViewValue 'pa$$word'
#=> trigger the submit event on the anugular element (or the compiled form)
#=> you can get this using element.find('form')
#=> or save it as the output of $compile(form)(scope)
FormElement.triggerHandler('submit')
expect(spy).to.be.calledWith 'em@i.l', 'pa$$word'