Back to Repositories

Testing Timestamp Field Implementation in rails_admin

This test suite validates the RailsAdmin timestamp field type functionality, focusing on parsing and formatting timestamp data in the admin interface. The tests ensure proper handling of timestamp fields across different time zones and format specifications.

Test Coverage Overview

The test coverage encompasses core timestamp field functionality and parsing capabilities.

  • Validates generic field type behavior for timestamp fields
  • Tests timestamp parsing with specific format (%B %d, %Y %H:%M)
  • Verifies UTC time zone handling

Implementation Analysis

The testing approach utilizes RSpec’s shared examples pattern for consistent field type validation. The implementation focuses on isolated unit tests with proper setup and teardown phases using before/after blocks.

Tests leverage FactoryBot for test data generation and incorporate time zone management.

Technical Details

  • RSpec testing framework
  • FactoryBot for test data generation
  • Time zone manipulation utilities
  • Shared example patterns for field type testing
  • ActiveRecord integration testing

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper isolation, cleanup, and specific assertions.

  • Isolated test setup and teardown
  • Specific time format testing
  • Proper use of shared examples
  • Clear test case organization

railsadminteam/rails_admin

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

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RailsAdmin::Config::Fields::Types::Timestamp, active_record: true do
  it_behaves_like 'a generic field type', :timestamp_field, :timestamp

  describe '#parse_input' do
    before :each do
      @object = FactoryBot.create(:field_test)
      @time = ::Time.now.getutc
      @field = RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :timestamp_field }
    end

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

    it 'reads %B %d, %Y %H:%M' do
      @object.timestamp_field = @field.parse_input(timestamp_field: @time.strftime('%B %d, %Y %H:%M'))
      expect(@object.timestamp_field.strftime('%Y-%m-%d %H:%M')).to eq(@time.strftime('%Y-%m-%d %H:%M'))
    end
  end
end