Commit 9dff14fe authored by marcheing's avatar marcheing Committed by Diego de Araújo Martinez Camarinha

Fix age verification for responsible fields

Signed-off-by: Diego de Araújo Martinez Camarinha's avatarDiego Araújo <diegoamc@protonmail.ch>
parent 1cd0075f
Pipeline #3596420 passed with stage
in 12 minutes and 1 second
@responsible_fields = (event)->
current_year = new Date().getFullYear()
if current_year - $('#profile_year').val() < 18
$('#responsible_fields').css('display', 'block')
$('#user-responsible-name').prop('required', true)
$('#user-responsible-id-number').prop('required', true)
else
$('#responsible_fields').css('display', 'none')
$('#user-responsible-name').prop('required', false)
$('#user-responsible-id-number').prop('required', false)
current_date = new Date()
year_difference = current_date.getFullYear() - $('#profile_year').val()
month_difference = current_date.getMonth() + 1 - $('#profile_month').val()
day_difference = current_date.getUTCDate() - $('#profile_day').val()
if year_difference <= 18
if month_difference <= 0
if day_difference < 0
$('#responsible_fields').css('display', 'block')
$('#user-responsible-name').prop('required', true)
$('#user-responsible-id-number').prop('required', true)
return
$('#responsible_fields').css('display', 'none')
$('#user-responsible-name').prop('required', false)
$('#user-responsible-id-number').prop('required', false)
$('#profile_year').on('change', @responsible_fields)
$('#profile_month').on('change', @responsible_fields)
$('#profile_day').on('change', @responsible_fields)
......@@ -3,13 +3,19 @@
describe 'responsible_fields', ->
before ->
@profile_year_input = sinon.stub()
@profile_year_input = val: ->
@profile_month_input = val: ->
@profile_day_input = val: ->
@val = val: ->
@fields = css: ->
@responsible_name = prop: ->
@responsible_id = prop: ->
@clock = sinon.useFakeTimers(new Date(2016, 0, 1).getTime())
sinon.stub(window, '$')
$.withArgs('#profile_year').returns(@profile_year_input)
$.withArgs('#profile_month').returns(@profile_month_input)
$.withArgs('#profile_day').returns(@profile_day_input)
$.withArgs('#responsible_fields').returns(@fields)
$.withArgs('#user-responsible-name').returns(@responsible_name)
$.withArgs('#user-responsible-id-number').returns(@responsible_id)
......@@ -20,8 +26,12 @@ describe 'responsible_fields', ->
describe 'when the user is younger than 18', ->
before ->
@birth_year = 2001
@profile_year_input.val = sinon.stub().returns(@birth_year)
@birth_year = 1999
@birth_month = 12
@birth_day = 31
@profile_year_stub = sinon.stub(@profile_year_input, 'val').returns(@birth_year)
@profile_month_stub = sinon.stub(@profile_month_input, 'val').returns(@birth_month)
@profile_day_stub = sinon.stub(@profile_day_input, 'val').returns(@birth_day)
it 'shows the responsible fields', ->
@css_spy_mock.expects('css').once().withArgs('display', 'block')
......@@ -30,10 +40,23 @@ describe 'responsible_fields', ->
responsible_fields()
sinon.assert.called(@profile_year_stub)
sinon.assert.called(@profile_month_stub)
sinon.assert.called(@profile_day_stub)
after ->
@profile_year_input.val.restore()
@profile_month_input.val.restore()
@profile_day_input.val.restore()
describe 'when the user is older than 18', ->
before ->
@birth_year = 1990
@profile_year_input.val = sinon.stub().returns(@birth_year)
@birth_year = 1998
@birth_month = 1
@birth_day = 1
@profile_year_stub = sinon.stub(@profile_year_input, 'val').returns(@birth_year)
@profile_month_stub = sinon.stub(@profile_month_input, 'val').returns(@birth_month)
@profile_day_stub = sinon.stub(@profile_day_input, 'val').returns(@birth_day)
it 'shows the responsible fields', ->
@css_spy_mock.expects('css').once().withArgs('display', 'none')
......@@ -42,8 +65,18 @@ describe 'responsible_fields', ->
responsible_fields()
sinon.assert.called(@profile_year_stub)
sinon.assert.called(@profile_month_stub)
sinon.assert.called(@profile_day_stub)
after ->
@profile_year_input.val.restore()
@profile_month_input.val.restore()
@profile_day_input.val.restore()
after ->
@css_spy_mock.verify()
@name_prop_mock.verify()
@id_prop_mock.verify()
$.restore()
@clock.restore()
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