2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-31 19:48:03 +01:00
|
|
|
describe Champs::DecimalNumberChamp do
|
2024-03-29 12:25:24 +01:00
|
|
|
describe 'validation' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:champ) { Champs::DecimalNumberChamp.new(value:, dossier: build(:dossier)) }
|
|
|
|
before do
|
|
|
|
allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_decimal_number))
|
|
|
|
allow(champ).to receive(:visible?).and_return(true)
|
2024-08-22 17:33:32 +02:00
|
|
|
allow(champ).to receive(:in_dossier_revision?).and_return(true)
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.run_callbacks(:validation)
|
|
|
|
end
|
|
|
|
subject { champ.validate(:champs_public_value) }
|
|
|
|
|
2018-10-31 19:48:03 +01:00
|
|
|
context 'when the value is integer number' do
|
|
|
|
let(:value) { 2 }
|
|
|
|
|
2024-03-29 12:25:24 +01:00
|
|
|
it { is_expected.to be_truthy }
|
2018-10-31 19:48:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the value is decimal number' do
|
|
|
|
let(:value) { 2.6 }
|
|
|
|
|
2024-03-29 12:25:24 +01:00
|
|
|
it { is_expected.to be_truthy }
|
2018-10-31 19:48:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the value is not a number' do
|
|
|
|
let(:value) { 'toto' }
|
|
|
|
|
2024-03-29 12:25:24 +01:00
|
|
|
it 'is not valid and contains expected error' do
|
|
|
|
expect(subject).to be_falsey
|
2024-04-08 16:28:08 +02:00
|
|
|
expect(champ.errors[:value]).to eq(["doit comprendre maximum 3 chiffres après la virgule", "n'est pas un nombre"])
|
2024-03-29 12:25:24 +01:00
|
|
|
end
|
2024-03-18 18:10:26 +01:00
|
|
|
end
|
|
|
|
|
2024-06-25 18:01:48 +02:00
|
|
|
context 'when value contain space' do
|
2024-07-01 15:31:32 +02:00
|
|
|
before { champ.run_callbacks(:validation) }
|
2024-06-25 18:01:48 +02:00
|
|
|
let(:value) { ' 2.6666 ' }
|
|
|
|
it { expect(champ.value).to eq('2.6666') }
|
|
|
|
end
|
|
|
|
|
2024-03-18 18:10:26 +01:00
|
|
|
context 'when the value has too many decimal' do
|
|
|
|
let(:value) { '2.6666' }
|
|
|
|
|
2024-03-29 12:25:24 +01:00
|
|
|
it 'is not valid and contains expected error' do
|
|
|
|
expect(subject).to be_falsey
|
2024-04-08 16:28:08 +02:00
|
|
|
expect(champ.errors[:value]).to eq(["doit comprendre maximum 3 chiffres après la virgule"])
|
2024-03-29 12:25:24 +01:00
|
|
|
end
|
2018-10-31 19:48:03 +01:00
|
|
|
end
|
2018-11-29 14:35:42 +01:00
|
|
|
|
|
|
|
context 'when the value is blank' do
|
|
|
|
let(:value) { '' }
|
|
|
|
|
2024-03-29 12:25:24 +01:00
|
|
|
it { is_expected.to be_truthy }
|
2018-11-29 14:35:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the value is nil' do
|
|
|
|
let(:value) { nil }
|
|
|
|
|
2024-03-29 12:25:24 +01:00
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the champ is private, value is invalid, but validation is public' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:champ) { Champs::DecimalNumberChamp.new(value:, private: true, dossier: build(:dossier)) }
|
2024-03-29 12:25:24 +01:00
|
|
|
let(:value) { '2.6666' }
|
|
|
|
it { is_expected.to be_truthy }
|
2018-11-29 14:35:42 +01:00
|
|
|
end
|
2018-10-31 19:48:03 +01:00
|
|
|
end
|
2024-06-25 17:05:33 +02:00
|
|
|
|
|
|
|
describe 'for_export' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:champ) { Champs::DecimalNumberChamp.new(value:) }
|
|
|
|
before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_decimal_number)) }
|
2024-06-25 17:05:33 +02:00
|
|
|
subject { champ.for_export }
|
|
|
|
context 'with nil' do
|
|
|
|
let(:value) { 0 }
|
2024-06-25 17:38:34 +02:00
|
|
|
it { is_expected.to eq(0.0) }
|
2024-06-25 17:05:33 +02:00
|
|
|
end
|
|
|
|
context 'with simple number' do
|
|
|
|
let(:value) { "120" }
|
|
|
|
it { is_expected.to eq(120) }
|
|
|
|
end
|
2024-06-25 17:38:34 +02:00
|
|
|
context 'with number having spaces' do
|
2024-06-25 17:05:33 +02:00
|
|
|
let(:value) { " 120 " }
|
2024-06-25 18:01:48 +02:00
|
|
|
it { is_expected.to eq(120) }
|
2024-06-25 17:05:33 +02:00
|
|
|
end
|
|
|
|
end
|
2018-10-31 19:48:03 +01:00
|
|
|
end
|