fix(Champs::IntegerNumberChamp): validates Champs::IntegerNumberChamp respecting it's validation context.

This commit is contained in:
mfo 2024-03-30 08:03:19 +01:00
parent fd5faad31f
commit 374d763084
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 14 additions and 9 deletions

View file

@ -7,7 +7,7 @@ class Champs::IntegerNumberChamp < Champ
# i18n-tasks-use t('errors.messages.not_an_integer')
"« #{object.libelle} » " + object.errors.generate_message(:value, :not_an_integer)
}
}
}, if: -> { validate_champ_value? || validation_context == :prefill }
def for_export
processed_value

View file

@ -1,37 +1,42 @@
describe Champs::IntegerNumberChamp do
subject { build(:champ_integer_number, value: value).tap(&:valid?) }
let(:champ) { build(:champ_integer_number, value:) }
subject { champ.validate(:champs_public_value) }
describe '#valid?' do
context 'when the value is integer number' do
let(:value) { 2 }
it { is_expected.to be_valid }
it { is_expected.to be_truthy }
end
context 'when the value is decimal number' do
let(:value) { 2.6 }
it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) }
it 'is not valid and contains errors' do
is_expected.to be_falsey
expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit être un nombre entier (sans chiffres après la virgule)"])
end
end
context 'when the value is not a number' do
let(:value) { 'toto' }
it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) }
it 'is not valid and contains errors' do
is_expected.to be_falsey
expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit être un nombre entier (sans chiffres après la virgule)"])
end
end
context 'when the value is blank' do
let(:value) { '' }
it { is_expected.to be_valid }
it { is_expected.to be_truthy }
end
context 'when the value is nil' do
let(:value) { nil }
it { is_expected.to be_valid }
it { is_expected.to be_truthy }
end
end
end