Back to Repositories

Testing Time Field Parsing and Formatting in RailsAdmin

This test suite validates the Time field type implementation in RailsAdmin, focusing on parsing and formatting time inputs across different formats and timezone scenarios. The tests ensure reliable time handling for the admin interface.

Test Coverage Overview

The test suite provides comprehensive coverage of the RailsAdmin Time field type functionality.

Key areas tested include:
  • Standard time format parsing (%H:%M)
  • Timezone handling and interpretation
  • Custom time format support
  • Integration with ActiveRecord models

Implementation Analysis

The testing approach uses RSpec’s shared examples pattern for generic field type validation, combined with specific time parsing scenarios. The implementation leverages RSpec’s context blocks and before/after hooks to manage test state and timezone configurations.

Technical patterns include:
  • Factory-based test object creation
  • Isolated timezone testing
  • Custom format configuration testing

Technical Details

Testing tools and configuration:
  • RSpec as the testing framework
  • FactoryBot for test data generation
  • ActiveRecord integration testing
  • Time zone manipulation capabilities
  • Custom strftime format configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation using before/after hooks
  • Clear test case organization
  • Comprehensive edge case coverage
  • Explicit timezone handling
  • Modular test configuration

railsadminteam/rails_admin

spec/rails_admin/config/fields/types/time_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RailsAdmin::Config::Fields::Types::Time, active_record: true do
  it_behaves_like 'a generic field type', :time_field, :time

  describe '#parse_input' do
    let(:field) do
      RailsAdmin.config(FieldTest).fields.detect do |f|
        f.name == :time_field
      end
    end

    before do
      RailsAdmin.config(FieldTest) do
        field :time_field, :time
      end
    end

    before :each do
      @object = FactoryBot.create(:field_test)
      @time = ::Time.new(2000, 1, 1, 3, 45)
    end

    after :each do
      Time.zone = 'UTC'
    end

    it 'reads %H:%M' do
      @object.time_field = field.parse_input(time_field: @time.strftime('%H:%M'))
      expect(@object.time_field.strftime('%H:%M')).to eq(@time.strftime('%H:%M'))
    end

    it 'interprets time value as local time when timezone is specified' do
      Time.zone = 'Eastern Time (US & Canada)' # -05:00
      @object.time_field = field.parse_input(time_field: '2000-01-01T03:45:00')
      expect(@object.time_field.strftime('%H:%M')).to eq('03:45')
    end

    context 'with a custom strftime_format' do
      let(:field) do
        RailsAdmin.config(FieldTest).fields.detect do |f|
          f.name == :time_field
        end
      end

      before do
        RailsAdmin.config(FieldTest) do
          field :time_field, :time do
            strftime_format '%I:%M %p'
          end
        end
      end

      it 'has a customization option' do
        @object.time_field = field.parse_input(time_field: @time.strftime('%I:%M %p'))
        expect(@object.time_field.strftime('%H:%M')).to eq(@time.strftime('%H:%M'))
      end
    end
  end
end