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

This commit is contained in:
mfo 2024-03-29 12:25:24 +01:00
parent b07bbfa455
commit 04f3b6a844
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 22 additions and 11 deletions

View file

@ -14,7 +14,7 @@ class Champs::DecimalNumberChamp < Champ
message: -> (object, _data) {
"« #{object.libelle} » " + object.errors.generate_message(:value, :not_a_number)
}
}
}, if: -> { validate_champ_value? || validation_context == :prefill }
def for_export
processed_value

View file

@ -1,43 +1,54 @@
describe Champs::DecimalNumberChamp do
subject { build(:champ_decimal_number, value: value).tap(&:valid?) }
let(:champ) { build(:champ_decimal_number, value:) }
subject { champ.validate(:champs_public_value) }
describe '#valid?' do
describe 'validation' 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 be_valid }
it { is_expected.to be_truthy }
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 comprendre maximum 3 chiffres après la virgule", "« #{subject.libelle} » n'est pas un nombre"]) }
it 'is not valid and contains expected error' do
expect(subject).to be_falsey
expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit comprendre maximum 3 chiffres après la virgule", "« #{champ.libelle} » n'est pas un nombre"])
end
end
context 'when the value has too many decimal' do
let(:value) { '2.6666' }
it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit comprendre maximum 3 chiffres après la virgule"]) }
it 'is not valid and contains expected error' do
expect(subject).to be_falsey
expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit comprendre maximum 3 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
context 'when the champ is private, value is invalid, but validation is public' do
let(:champ) { build(:champ_decimal_number, :private, value:) }
let(:value) { '2.6666' }
it { is_expected.to be_truthy }
end
end
end