mardi 28 juillet 2015

Unit Testing an Angular Directive that Uses ngModel

I am currently writing unit tests for an app I have been working on, but I am unsure of how to handle this case. How does one create a spyOn function for ngModel.$validate()?

let moduleName = 'compareTo';

// ## Directive Definition
class compareTo {

  constructor() {

    this.restrict = 'EA';
    this.require = 'ngModel';
    this.scope = { otherModelValue : '=compareTo' };
  }

  // ### Optional Link Function
  link (scope, elem, attrs, ngModel) {

    ngModel.$validators.compareTo = (modelValue) => {

      return modelValue == scope.otherModelValue;
    };

    scope.$watch('otherModelValue', () => {

      // I am trying to spy on this function call.
      ngModel.$validate();
    });
  }
}

export default [moduleName, directiveFactory(compareTo)];

I am trying to write a test like so:

describe('link function', () => {

  it('should', () => {

    spyOn(/* not sure what should go here? */, '$validate');
  });
});

Any help is much appreciated!

Aucun commentaire:

Enregistrer un commentaire