Commit aef68c43 authored by Nick Stenning's avatar Nick Stenning

Merge pull request #2127 from hypothesis/disentangle-form-helpers

Test form-input interaction with form-validate
parents 1d3904ca eb31f2a0
module.exports = -> module.exports = ->
link: (scope, elem, attr, [form, model, validator]) -> link: (scope, elem, attr, [model, validator]) ->
return unless form?.$name and model.$name and validator return unless model
fieldClassName = 'form-field' fieldClassName = 'form-field'
errorClassName = 'form-field-error' errorClassName = 'form-field-error'
...@@ -20,13 +20,14 @@ module.exports = -> ...@@ -20,13 +20,14 @@ module.exports = ->
toggleClass(model.$invalid and model.$dirty) toggleClass(model.$invalid and model.$dirty)
render() render()
validator.addControl(model) if validator?
scope.$on '$destroy', -> validator.removeControl this validator.addControl(model)
scope.$on '$destroy', -> validator.removeControl model
scope.$watch -> scope.$watch ->
if model.$modelValue? or model.$pristine if model.$modelValue? or model.$pristine
toggleClass(model.$invalid and model.$dirty) toggleClass(model.$invalid and model.$dirty)
return return
require: ['^?form', '?ngModel', '^?formValidate'] require: ['?ngModel', '^?formValidate']
restrict: 'C' restrict: 'C'
...@@ -12,7 +12,6 @@ describe 'form-input', -> ...@@ -12,7 +12,6 @@ describe 'form-input', ->
before -> before ->
angular.module('h', ['ng']) angular.module('h', ['ng'])
.directive('formInput', require('../form-input')) .directive('formInput', require('../form-input'))
.directive('formValidate', require('../form-validate'))
beforeEach module('h') beforeEach module('h')
beforeEach inject (_$compile_, _$rootScope_) -> beforeEach inject (_$compile_, _$rootScope_) ->
...@@ -23,16 +22,14 @@ describe 'form-input', -> ...@@ -23,16 +22,14 @@ describe 'form-input', ->
$scope.model = {username: undefined} $scope.model = {username: undefined}
template = ''' template = '''
<form form-validate name="login" onsubmit="return false"> <div class="form-field">
<div class="form-field"> <input type="text" class="form-input" name="username"
<input type="text" class="form-input" name="username" ng-model="model.username" name="username"
ng-model="model.username" name="username" required ng-minlength="3" />
required ng-minlength="3" /> </div>
</div>
</form>
''' '''
$field = $compile(angular.element(template))($scope).find('div') $field = $compile(angular.element(template))($scope)
$scope.$digest() $scope.$digest()
it 'should remove an error class to an valid field on change', -> it 'should remove an error class to an valid field on change', ->
...@@ -109,3 +106,17 @@ describe 'form-input', -> ...@@ -109,3 +106,17 @@ describe 'form-input', ->
$scope.$digest() $scope.$digest()
assert.notInclude($field.prop('className'), 'form-field-error') 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