Add unit tests for responsible_fields script

Signed-off-by: marcheing's avatarHeitor Reis <[email protected]>
parent 3e926945
responsible_fields = (event)->
@responsible_fields = (event)->
current_year = new Date().getFullYear()
if current_year - $('#profile_year').val() < 18
$('#responsible_fields').css('display', 'block')
......@@ -9,4 +9,4 @@ responsible_fields = (event)->
$('#user-responsible-name').prop('required', false)
$('#user-responsible-id-number').prop('required', false)
$('#profile_year').on('change', responsible_fields)
$('#profile_year').on('change', @responsible_fields)
......@@ -2,27 +2,47 @@
#= require profiles/responsible_fields
describe 'responsible_fields', ->
before ->
@profile_year_input = sinon.stub()
@fields = css: ->
@responsible_name = prop: ->
@responsible_id = prop: ->
sinon.stub(window, '$')
$.withArgs('#profile_year').returns(@profile_year_input)
$.withArgs('#responsible_fields').returns(@fields)
$.withArgs('#user-responsible-name').returns(@responsible_name)
$.withArgs('#user-responsible-id-number').returns(@responsible_id)
@css_spy_mock = sinon.mock(@fields)
@name_prop_mock = sinon.mock(@responsible_name)
@id_prop_mock = sinon.mock(@responsible_id)
describe 'when the user is younger than 18', ->
before ->
@age = 15
@profile_year_input = sinon.stub()
@fields = sinon.stub()
@responsible_name = sinon.stub()
@responsible_id = sinon.stub()
@profile_year_input.val = sinon.stub().returns(@age)
@jquery_stub = sinon.stub(window, '$')
@jquery_stub.withArgs('#profile_year').returns(@profile_year_input)
@jquery_stub.withArgs('#responsible_fields').returns(@fields)
@jquery_stub.withArgs('#user-responsible-name').returns(@responsible_name)
@jquery_stub.withArgs('#user-responsible-id-number').returns(@responsible_id)
@birth_year = 2001
@profile_year_input.val = sinon.stub().returns(@birth_year)
it 'shows the responsible fields', ->
@css_spy_mock.expects('css').once().withArgs('display', 'block')
@name_prop_mock.expects('prop').once().withArgs('required', true)
@id_prop_mock.expects('prop').once().withArgs('required', true)
responsible_fields()
describe 'when the user is older than 18', ->
before ->
@birth_year = 1990
@profile_year_input.val = sinon.stub().returns(@birth_year)
it 'shows the responsible fields', ->
@css_spy_mock = sinon.spy(@fields, 'css')
@name_prop_mock = sinon.spy(@responsible_name, 'prop')
@id_prop_mock = sinon.spy(@responsible_id, 'prop')
@css_spy_mock.expects('css').once().withArgs('display', 'none')
@name_prop_mock.expects('prop').once().withArgs('required', false)
@id_prop_mock.expects('prop').once().withArgs('required', false)
responsible_fields()
assert(@css_spy_mock.calledWith('display', 'block'))
assert(@name_prop_mock.calledWith('required', true))
assert(@id_prop_mock.calledWith('required', true))
after ->
@css_spy_mock.verify()
@name_prop_mock.verify()
@id_prop_mock.verify()
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