Chapter 8
Writing tests for asynchronous validation
We set up the test similar to the previous validation directive:
beforeEach(inject(function($compile, $rootScope, _Users_){
Users = _Users_;
spyOn(Users, 'query').andCallThrough();
...
}));
We are spying on the Users.query() function but we also want it to call
through to our mocked out function so that we can simulate responses with
Users.respondWith().
The significant unit tests are as follows:
it('should call Users.query when the view changes', function() {
testInput.$setViewValue('different');
expect(Users.query).toHaveBeenCalled();
});
it('should set model to invalid if the Users.query response contains
users', function() {
testInput.$setViewValue('different');
Users.respondWith(['someUser']);
expect(testInput.$valid).toBe(false);
});
it('should set model to valid if the Users.query response contains no
users', function() {
testInput.$setViewValue('different');
Users.respondWith([]);
expect(testInput.$valid).toBe(true);
});
We are checking to see if Users.query() was called. Also, since Users.query()
tracks the response callback, we can simulate a response from the server with Users.
respondWith().
One issue, which we need to test for, is that we don't want to query the server if the
user re-enters the same value as was provided by the model. For instance, if we are
editing a user rather than creating a user, then the user's original e-mail is in the
database on the server but it is a valid e-mail address.
it('should not call Users.query if the view changes to be the same as
the original model', function() {
$scope.model.testValue = '[email protected]';