demarches-normaliennes/spec/models/champs/datetime_champ_spec.rb
Damien Le Thiec 5e26acb0e1
Make date and datetime prefillable (#8304)
* Make date and datetime prefillable

* Format in ISO8601 format
2023-01-12 17:42:02 +01:00

51 lines
1.4 KiB
Ruby

describe Champs::DatetimeChamp do
let(:datetime_champ) { build(:champ_datetime) }
describe '#convert_to_iso8601' do
it 'preserves nil' do
champ = champ_with_value(nil)
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if empty string' do
champ = champ_with_value("")
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if not ISO8601' do
champ = champ_with_value("12-21-2023 03:20")
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if not datetime' do
champ = champ_with_value("value")
champ.save
expect(champ.reload.value).to be_nil
end
it 'preserves if ISO8601' do
champ = champ_with_value("2023-12-21T03:20")
champ.save
expect(champ.reload.value).to eq("2023-12-21T03:20:00+01:00")
end
it 'converts to ISO8601 if form format' do
champ = champ_with_value("{3=>21, 2=>12, 1=>2023, 4=>3, 5=>20}")
champ.save
expect(champ.reload.value).to eq("2023-12-21T03:20:00+01:00")
end
it 'converts to ISO8601 if old browser form format' do
champ = champ_with_value("21/12/2023 03:20")
champ.save
expect(champ.reload.value).to eq("2023-12-21T03:20:00+01:00")
end
end
def champ_with_value(number)
datetime_champ.tap { |c| c.value = number }
end
end