Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
0e57b163
Commit
0e57b163
authored
Jul 23, 2014
by
Aron Carroll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests for the formValidate directive
parent
c8223fe2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
23 deletions
+81
-23
controllers.coffee
h/static/scripts/controllers.coffee
+1
-0
directives.coffee
h/static/scripts/directives.coffee
+6
-23
directives-test.coffee
tests/js/directives-test.coffee
+74
-0
No files found.
h/static/scripts/controllers.coffee
View file @
0e57b163
...
...
@@ -715,6 +715,7 @@ class Auth
angular
.
extend
$scope
.
model
,
base
for
own
_
,
ctrl
of
$scope
when
angular
.
isFunction
ctrl
?
.
$setPristine
ctrl
.
$setPristine
()
form
.
responseErrorMessage
=
null
_updateFormValidity
=
(
form
,
reason
)
->
if
reason
==
'Invalid username or password.'
...
...
h/static/scripts/directives.coffee
View file @
0e57b163
formField
=
->
link
:
(
scope
,
elem
,
attr
,
form
)
->
field
=
form
[
attr
.
target
]
errorClass
=
attr
.
errorClass
return
unless
field
?
unless
errorClass
throw
new
Error
(
'FormField directive requires an error-class attribute'
)
field
.
unsetFieldError
=
->
elem
.
removeClass
(
errorClass
)
field
.
setFieldError
=
->
elem
.
addClass
(
errorClass
)
require
:
'^form'
restrict
:
'C'
formValidate
=
->
link
:
(
scope
,
elem
,
attr
,
form
)
->
toggleClass
=
(
field
,
{
addClass
})
->
fieldEl
=
elem
.
find
(
"[data-target=
#{
field
.
$name
}
]"
)
fieldEl
.
toggleClass
(
'form-field-error'
,
addClass
)
updateField
=
(
field
)
->
return
unless
field
?
if
field
.
$valid
field
.
unsetFieldError
(
)
toggleClass
(
field
,
addClass
:
false
)
else
field
.
setFieldError
(
)
toggleClass
(
field
,
addClass
:
true
)
# Immediately show feedback for corrections.
elem
.
on
'keyup'
,
':input'
,
->
...
...
@@ -465,7 +449,6 @@ whenscrolled = ['$window', ($window) ->
angular
.
module
(
'h.directives'
,
[
'ngSanitize'
])
.
directive
(
'formField'
,
formField
)
.
directive
(
'formValidate'
,
formValidate
)
.
directive
(
'fuzzytime'
,
fuzzytime
)
.
directive
(
'markdown'
,
markdown
)
...
...
tests/js/directives-test.coffee
View file @
0e57b163
...
...
@@ -7,8 +7,12 @@ describe 'h.directives', ->
beforeEach
module
(
$provide
,
$filterProvider
)
->
fakeWindow
=
{
open
:
sinon
.
spy
()}
fakeDocument
=
angular
.
element
({
createElement
:
(
tag
)
->
document
.
createElement
(
tag
)
})
$provide
.
value
(
'$window'
,
fakeWindow
)
$provide
.
value
(
'$document'
,
fakeDocument
)
$filterProvider
.
register
'persona'
,
->
(
user
,
part
)
->
...
...
@@ -23,6 +27,76 @@ describe 'h.directives', ->
$compile
=
_$compile_
$scope
=
_$rootScope_
.
$new
()
describe
'.formValidate'
,
->
$element
=
null
beforeEach
->
$scope
.
model
=
{
username
:
''
}
template
=
'''
<form form-validate name="login" onsubmit="return false">
<div class="form-field" data-error-class="form-field-error" data-target="username">
<input type="text" class="" ng-model="model.username" name="username" required ng-minlength="3" />
</div>
</form>
'''
# Needs to be passed through angular.element() to work. Otherwise it
# will not link the form-validate directive.
$element
=
$compile
(
angular
.
element
(
template
))(
$scope
)
$scope
.
$digest
()
it
'should apply an error class to an invalid field on change'
,
->
$field
=
$element
.
find
(
'.form-field'
)
$element
.
find
(
'[name=username]'
).
val
(
'ab'
).
change
()
assert
.
include
$field
.
prop
(
'className'
),
'form-field-error'
it
'should remove an error class to an valid field on change'
,
->
$field
=
$element
.
find
(
'.form-field'
).
addClass
(
'form-field-error'
)
$input
=
$element
.
find
(
'[name=username]'
)
$input
.
val
(
'abc'
).
change
()
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
it
'should apply an error class to an invalid field on submit'
,
->
$field
=
$element
.
find
(
'.form-field'
)
$element
.
trigger
(
'submit'
)
assert
.
include
$field
.
prop
(
'className'
),
'form-field-error'
it
'should remove an error class from a valid field on submit'
,
->
$scope
.
model
.
username
=
'abc'
$scope
.
$digest
()
$field
=
$element
.
find
(
'.form-field'
).
addClass
(
'form-field-error'
)
$element
.
trigger
(
'submit'
)
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
it
'should apply an error class to an invalid field on "error" event'
,
->
$scope
.
$emit
(
'error'
,
'login'
)
$element
.
controller
(
'form'
).
username
.
$setValidity
(
'response'
,
false
)
$field
=
$element
.
find
(
'.form-field'
)
assert
.
include
$field
.
prop
(
'className'
),
'form-field-error'
it
'should remove an error class on valid input on keyup'
,
->
$scope
.
model
.
username
=
'abc'
$scope
.
$digest
()
$field
=
$element
.
find
(
'.form-field'
).
addClass
(
'form-field-error'
)
$element
.
find
(
'[name=username]'
).
keyup
()
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
it
'should not add an error class on invalid input on keyup'
,
->
$scope
.
model
.
username
=
''
$scope
.
$digest
()
$field
=
$element
.
find
(
'.form-field'
)
$element
.
find
(
'[name=username]'
).
keyup
()
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
describe
'.username'
,
->
$element
=
null
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment