2023-01-12 17:42:02 +01:00
|
|
|
describe Champs::DateChamp do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:type_de_champ) { create(:type_de_champ_date) }
|
|
|
|
let(:date_champ) { described_class.new }
|
|
|
|
before { allow(date_champ).to receive(:type_de_champ).and_return(type_de_champ) }
|
2023-01-12 17:42:02 +01:00
|
|
|
|
|
|
|
describe '#convert_to_iso8601' do
|
|
|
|
it 'preserves nil' do
|
|
|
|
champ = champ_with_value(nil)
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to be_nil
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if empty string' do
|
|
|
|
champ = champ_with_value("")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to be_nil
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if not ISO8601' do
|
|
|
|
champ = champ_with_value("12-21-2023")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to be_nil
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if not date' do
|
|
|
|
champ = champ_with_value("value")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to be_nil
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "converts %d/%m/%Y format to ISO" do
|
|
|
|
champ = champ_with_value("31/12/2017")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to eq("2017-12-31")
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'preserves if ISO8601' do
|
|
|
|
champ = champ_with_value("2023-12-21")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to eq("2023-12-21")
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
2023-01-26 10:53:47 +01:00
|
|
|
it 'converts to nil if false iso' do
|
|
|
|
champ = champ_with_value("2023-27-02")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.validate
|
|
|
|
expect(champ.value).to eq(nil)
|
2023-01-26 10:28:24 +01:00
|
|
|
end
|
|
|
|
end
|
2023-01-26 10:53:47 +01:00
|
|
|
|
2023-04-05 12:16:52 +02:00
|
|
|
describe "#to_s" do
|
|
|
|
it "format the date" do
|
|
|
|
champ_with_value("2020-06-20")
|
|
|
|
expect(date_champ.to_s).to eq("20 juin 2020")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not fail when value is not iso" do
|
|
|
|
champ_with_value("2023-30-01")
|
|
|
|
expect(date_champ.to_s).to eq("2023-30-01")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-12 17:42:02 +01:00
|
|
|
def champ_with_value(number)
|
|
|
|
date_champ.tap { |c| c.value = number }
|
|
|
|
end
|
|
|
|
end
|