Files
grecia/spec/models/verification/management/document_spec.rb
Javi Martín 3b7948a139 Use a date field to select the date of birth
The default `date_select` used in fields presents an accessibility
issue, because in generates three select controls but only one label.
That means that there are two controls without a label.

So we're using a date field instead. This type is field is supported by
about 99% of the browsers, and we've already got JavaScript code
converting this field to a jQuery UI datepicker in case the browser
doesn't support date fields.

Note that, since we no longer need to parse the three date fields into
one, we can simplify the code in both the models and the tests.

Another slight improvement is that, previously, we couldn't restrict the
month and day controls in order to set the minimum date, so the maximum
selectable date was always the 31st of December of the year set by the
minimum age setting. As seen in the component test, now that we use only
one field, we can set a specific date as the maximum one.
2024-11-12 15:15:34 +01:00

117 lines
4.4 KiB
Ruby

require "rails_helper"
describe Verification::Management::Document do
let(:verification_document) { build(:verification_document, document_number: "12345678Z") }
describe "validations" do
it "is valid" do
expect(verification_document).to be_valid
end
it "is not valid without a document number" do
verification_document.document_number = nil
expect(verification_document).not_to be_valid
end
it "is not valid without a document type" do
verification_document.document_type = nil
expect(verification_document).not_to be_valid
end
it "is valid without a date of birth" do
verification_document.date_of_birth = nil
expect(verification_document).to be_valid
end
it "is valid without a postal code" do
verification_document.postal_code = nil
expect(verification_document).to be_valid
end
describe "custom validations with RemoteCensus enabled", :remote_census do
it "is valid" do
expect(verification_document).to be_valid
end
it "is not valid without a document number" do
verification_document.document_number = nil
expect(verification_document).not_to be_valid
end
it "is not valid without a document type" do
verification_document.document_type = nil
expect(verification_document).not_to be_valid
end
it "is not valid without a date of birth" do
verification_document.date_of_birth = nil
expect(verification_document).not_to be_valid
end
it "is not valid without a postal_code" do
verification_document.postal_code = nil
expect(verification_document).not_to be_valid
end
describe "dates" do
it "is valid with a valid date of birth" do
verification_document = Verification::Management::Document.new(date_of_birth: "1980-01-01")
expect(verification_document.errors[:date_of_birth]).to be_empty
end
it "is not valid without a date of birth" do
verification_document = Verification::Management::Document.new(date_of_birth: "")
expect(verification_document).not_to be_valid
expect(verification_document.errors[:date_of_birth]).to include("can't be blank")
end
end
end
describe "Allowed Age" do
let(:min_age) { User.minimum_required_age }
let(:over_minimum_age_date_of_birth) { Date.new((min_age + 10).years.ago.year, 12, 31) }
let(:under_minimum_age_date_of_birth) { Date.new(min_age.years.ago.year, 12, 31) }
let(:just_minimum_age_date_of_birth) do
Date.new(min_age.years.ago.year, min_age.years.ago.month, min_age.years.ago.day)
end
describe "#valid_age?" do
it "returns false when the user is younger than the user's minimum required age" do
census_response = double(date_of_birth: under_minimum_age_date_of_birth)
expect(Verification::Management::Document.new.valid_age?(census_response)).to be false
end
it "returns true when the user has the user's minimum required age" do
census_response = double(date_of_birth: just_minimum_age_date_of_birth)
expect(Verification::Management::Document.new.valid_age?(census_response)).to be true
end
it "returns true when the user is older than the user's minimum required age" do
census_response = double(date_of_birth: over_minimum_age_date_of_birth)
expect(Verification::Management::Document.new.valid_age?(census_response)).to be true
end
end
describe "#under_age?" do
it "returns true when the user is younger than the user's minimum required age" do
census_response = double(date_of_birth: under_minimum_age_date_of_birth)
expect(Verification::Management::Document.new.under_age?(census_response)).to be true
end
it "returns false when the user is user's minimum required age" do
census_response = double(date_of_birth: just_minimum_age_date_of_birth)
expect(Verification::Management::Document.new.under_age?(census_response)).to be false
end
it "returns false when the user is older than user's minimum required age" do
census_response = double(date_of_birth: over_minimum_age_date_of_birth)
expect(Verification::Management::Document.new.under_age?(census_response)).to be false
end
end
end
end
end