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