How is unit test for form / invalid in Jasmine for AngularJS?

This app and AngularJS app with:

A form named 'LoginForm'

<div class="well well-large loginForm" ng-controller="LoginController" ng-hide="isAuthenticated"> <form id="loginForm" class="form-signin" name="LoginForm"> <h2 class="form-signin-heading">Please sign in</h2> <input type="text" name="username" ng-model="user.username" required class="input-block-level input-large" placeholder="Username" autocorrect="off" autocapitalize="off" tabindex="1"> <input type="password" name="password" ng-model="user.password" required class="input-block-level input-large" placeholder="Password" tabindex="2"> <p ng-show="error401" class="text-error">Username and/or password was incorrect</p> <p ng-show="error500" class="text-error">Whoops! Something went wrong</p> <button class="btn btn-primary loginButton" ng-click="login(user)" tabindex="3">Sign in</button> </form> </div> 

A controller named 'LoginController', with the function $ scope.login ()

 $scope.login = function(user) { if ($scope.LoginForm.$invalid) { log('LoginForm is invalid'); return; } else { log('LoginForm is valid'); } // ... snip ... other stuff like actual ajax login ... } 

How to write a Jasmine test for $ scope.LoginForm. $ invalid?

I found this property to be readonly, so I tried setting the layout of the object in the $ scope area:

 scope.LoginForm = { "user" : { "password": "something" } }; 

The above still passed the test, although it should not be because both fields are required.

+6
source share
1 answer

You can set $ invalid property to true in unit test:

$ scope.LoginForm. $ invalid = true;

+1
source

Source: https://habr.com/ru/post/945576/


All Articles