2023-01-12 17:42:02 +01:00
|
|
|
describe Champs::DatetimeChamp do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:datetime_champ) { described_class.new }
|
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.run_callbacks(:validation)
|
|
|
|
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.run_callbacks(:validation)
|
|
|
|
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 03:20")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.run_callbacks(:validation)
|
|
|
|
expect(champ.value).to be_nil
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if not datetime' do
|
|
|
|
champ = champ_with_value("value")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.run_callbacks(:validation)
|
|
|
|
expect(champ.value).to be_nil
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'preserves if ISO8601' do
|
|
|
|
champ = champ_with_value("2023-12-21T03:20")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.run_callbacks(:validation)
|
|
|
|
expect(champ.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
|
2023-01-12 17:42:02 +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}")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.run_callbacks(:validation)
|
|
|
|
expect(champ.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to ISO8601 if old browser form format' do
|
|
|
|
champ = champ_with_value("21/12/2023 03:20")
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.run_callbacks(:validation)
|
|
|
|
expect(champ.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def champ_with_value(number)
|
|
|
|
datetime_champ.tap { |c| c.value = number }
|
|
|
|
end
|
|
|
|
end
|