Sha256: 6316127149d0b9b8dc9b3257d8e1310f5fdce838c3716f5a758f286aa3392bea

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Normalizy::Filters::Date do
  it { expect(subject.call('')).to eq '' }

  it { expect(subject.call('1984-10-23')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }

  it { expect(subject.call('84/10/23', format: '%y/%m/%d')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }

  it { expect(subject.call('84/10/23', format: '%y/%m/%d')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }

  it { expect(subject.call(Time.new(1984, 10, 23), adjust: :end)).to      eq Time.new(1984, 10, 23).end_of_day }
  it { expect(subject.call(Time.new(1984, 10, 23, 1), adjust: :begin)).to eq Time.new(1984, 10, 23).beginning_of_day }

  it 'accepts time zone' do
    time = subject.call('1984-10-23', time_zone: 'Tokelau Is.').utc

    expect(time).to eq Time.new(1984, 10, 23, time.hour, 0, 0, 0)
  end

  context 'with invalid date' do
    let!(:object)  { ModelDate.new }
    let!(:options) { { attribute: :date, object: object } }

    context 'with i18n present' do
      before do
        allow(I18n).to receive(:t).with(:date,
          scope:   ['normalizy.errors.date', 'model_date'],
          value:   '1984-10-00',
          default: '%{value} is an invalid date.') { 'date.error' }
      end

      it 'writes an error on object and does not set the values' do
        expected = Time.new(1984, 10, 23, 0, 0, 0, 0)

        subject.call '1984-10-00', options

        expect(object.errors[:date]).to eq ['date.error']
        expect(object.date).to          eq nil
      end
    end

    context 'with no I18n present' do
      it 'writes a default error on object and does not set the values' do
        expected = Time.new(1984, 10, 23, 0, 0, 0, 0)

        subject.call '1984-10-00', options

        expect(object.errors[:date]).to eq ['1984-10-00 is an invalid date.']
        expect(object.date).to          eq nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
normalizy-1.0.1 spec/normalizy/filters/date_spec.rb
normalizy-1.0.0 spec/normalizy/filters/date_spec.rb