Commit eb31f2a0 authored by Randall Leeds's avatar Randall Leeds

Test form-input interaction with form-validate

Mock and test that half of the interaction, and in doing so show that
it's also optional to use the validator with the input.
parent d7982a19
module.exports = ->
link: (scope, elem, attr, [form, model, validator]) ->
return unless form?.$name and model.$name and validator
link: (scope, elem, attr, [model, validator]) ->
return unless model
fieldClassName = 'form-field'
errorClassName = 'form-field-error'
......@@ -20,13 +20,14 @@ module.exports = ->
toggleClass(model.$invalid and model.$dirty)
render()
validator.addControl(model)
scope.$on '$destroy', -> validator.removeControl this
if validator?
validator.addControl(model)
scope.$on '$destroy', -> validator.removeControl model
scope.$watch ->
if model.$modelValue? or model.$pristine
toggleClass(model.$invalid and model.$dirty)
return
require: ['^?form', '?ngModel', '^?formValidate']
require: ['?ngModel', '^?formValidate']
restrict: 'C'
......@@ -12,7 +12,6 @@ describe 'form-input', ->
before ->
angular.module('h', ['ng'])
.directive('formInput', require('../form-input'))
.directive('formValidate', require('../form-validate'))
beforeEach module('h')
beforeEach inject (_$compile_, _$rootScope_) ->
......@@ -23,16 +22,14 @@ describe 'form-input', ->
$scope.model = {username: undefined}
template = '''
<form form-validate name="login" onsubmit="return false">
<div class="form-field">
<input type="text" class="form-input" name="username"
ng-model="model.username" name="username"
required ng-minlength="3" />
</div>
</form>
<div class="form-field">
<input type="text" class="form-input" name="username"
ng-model="model.username" name="username"
required ng-minlength="3" />
</div>
'''
$field = $compile(angular.element(template))($scope).find('div')
$field = $compile(angular.element(template))($scope)
$scope.$digest()
it 'should remove an error class to an valid field on change', ->
......@@ -109,3 +106,17 @@ describe 'form-input', ->
$scope.$digest()
assert.notInclude($field.prop('className'), 'form-field-error')
describe 'with form-validate', ->
link = require('../form-input')().link
it 'should register its model with the validator', ->
model = {'$parsers': []}
validator = {addControl: sinon.spy(), removeControl: sinon.spy()}
link($scope, $field, null, [model, validator])
assert.calledOnce(validator.addControl)
assert.calledWith(validator.addControl, model)
assert.notCalled(validator.removeControl)
$scope.$destroy()
assert.calledOnce(validator.removeControl)
assert.calledWith(validator.removeControl, model)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment