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
263a5583
Commit
263a5583
authored
Aug 05, 2014
by
gergely-ujvari
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1379 from hypothesis/1375-fix-server-errors
Fix server errors in login form Fix #1375
parents
3f8d0c87
a10c1b57
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
29 deletions
+60
-29
directives.coffee
h/static/scripts/directives.coffee
+15
-5
directives-test.coffee
tests/js/directives-test.coffee
+45
-24
No files found.
h/static/scripts/directives.coffee
View file @
263a5583
...
...
@@ -14,9 +14,19 @@ formValidate = ->
else
toggleClass
(
field
,
addClass
:
true
)
# Immediately show feedback for corrections.
elem
.
on
'keyup'
,
':input'
,
->
updateField
(
form
[
this
.
name
])
if
form
[
this
.
name
]
?
.
$valid
# A custom parser for each form field that is used to reset the "response"
# error state whenever the $viewValue changes.
fieldParser
=
(
field
,
value
)
->
field
.
$setValidity
(
'response'
,
true
)
updateField
(
field
)
if
field
.
$valid
return
value
forEachField
=
(
fn
)
->
fn
(
field
)
for
own
_
,
field
of
form
when
field
?
.
$name
?
forEachField
(
field
)
->
parser
=
angular
.
bind
(
null
,
fieldParser
,
field
)
field
.
$parsers
.
push
(
parser
)
# Validate field when the content changes.
elem
.
on
'change'
,
':input'
,
->
...
...
@@ -24,12 +34,12 @@ formValidate = ->
# Validate the field when submit is clicked.
elem
.
on
'submit'
,
(
event
)
->
updateField
(
field
)
for
own
_
,
field
of
form
when
field
?
.
$name
?
forEachField
(
updateField
)
# Validate when a response is processed.
scope
.
$on
'error'
,
(
event
,
name
)
->
return
unless
form
.
$name
==
name
updateField
(
field
)
for
own
_
,
field
of
form
when
field
?
.
$name
?
forEachField
(
updateField
)
require
:
'form'
...
...
tests/js/directives-test.coffee
View file @
263a5583
...
...
@@ -48,28 +48,36 @@ describe 'h.directives', ->
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'
$input
=
$element
.
find
(
'[name=username]'
)
controller
=
$input
.
controller
(
'ngModel'
)
controller
.
$setViewValue
(
'ab'
)
$input
.
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'
controller
=
$input
.
controller
(
'ngModel'
)
controller
.
$setViewValue
(
'abc'
)
$input
.
triggerHandler
(
'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'
$element
.
trigger
Handler
(
'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'
)
controller
=
$element
.
find
(
'[name=username]'
).
controller
(
'ngModel'
)
controller
.
$setViewValue
(
'abc'
)
$element
.
trigger
(
'submit'
)
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
$element
.
trigger
Handler
(
'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'
)
...
...
@@ -78,23 +86,36 @@ describe 'h.directives', ->
$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
()
it
'should remove an error class on valid input when the view changes'
,
->
$field
=
$element
.
find
(
'.form-field'
).
addClass
(
'form-field-error'
)
$element
.
find
(
'[name=username]'
).
keyup
()
controller
=
$element
.
find
(
'[name=username]'
).
controller
(
'ngModel'
)
controller
.
$setViewValue
(
'abc'
)
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
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
()
it
'should not add an error class on invalid input on when the view changes'
,
->
$field
=
$element
.
find
(
'.form-field'
)
controller
=
$element
.
find
(
'[name=username]'
).
controller
(
'ngModel'
)
controller
.
$setViewValue
(
'ab'
)
assert
.
notInclude
(
$field
.
prop
(
'className'
),
'form-field-error'
)
it
'should reset the "response" error on change'
,
->
$field
=
$element
.
find
(
'.form-field'
)
$element
.
find
(
'[name=username]'
).
keyup
()
$input
=
$element
.
find
(
'[name=username]'
)
controller
=
$input
.
controller
(
'ngModel'
)
controller
.
$setViewValue
(
'abc'
)
# Submit Event
controller
.
$setValidity
(
'response'
,
false
)
controller
.
responseErrorMessage
=
'fail'
$scope
.
$emit
(
'error'
,
$input
.
controller
(
'form'
).
$name
)
assert
.
include
(
$field
.
prop
(
'className'
),
'form-field-error'
)
controller
.
$setViewValue
(
'abc'
)
$scope
.
$digest
()
assert
.
notInclude
$field
.
prop
(
'className'
),
'form-field-error'
assert
.
notInclude
(
$field
.
prop
(
'className'
),
'form-field-error'
)
describe
'.username'
,
->
...
...
@@ -116,7 +137,7 @@ describe 'h.directives', ->
it
'prevents the default browser action on click'
,
->
event
=
jQuery
.
Event
(
'click'
)
$element
.
find
(
'.user'
).
trigger
(
event
)
$element
.
find
(
'.user'
).
trigger
Handler
(
event
)
assert
(
event
.
isDefaultPrevented
())
...
...
@@ -158,7 +179,7 @@ describe 'h.directives', ->
it
'calls the given search function'
,
->
$scope
.
query
=
"Test query"
$scope
.
$digest
()
$element
.
trigger
(
'submit'
)
$element
.
trigger
Handler
(
'submit'
)
sinon
.
assert
.
calledWith
(
$scope
.
update
,
"Test query"
)
it
'calls the given clear function'
,
->
...
...
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