Back to Repositories

Testing History Index Functionality in Rails Admin

This integration test suite validates the history index functionality in Rails Admin using PaperTrail for version tracking. It focuses on testing pagination, sorting, and display of historical changes for model objects.

Test Coverage Overview

The test suite provides comprehensive coverage of the history index feature:
  • Verification of history display with multiple versions
  • Pagination functionality testing with configurable page sizes
  • Sorting capabilities for history entries
  • Custom pagination parameter handling

Implementation Analysis

The testing approach utilizes RSpec request specs with PaperTrail integration:
  • Factory-based test data generation
  • Dynamic version creation with timestamps
  • CSS selector-based assertions
  • JavaScript-enabled sorting tests

Technical Details

Key technical components include:
  • RSpec request specs framework
  • PaperTrail for version tracking
  • FactoryBot for test data creation
  • Kaminari for pagination
  • Capybara for interface testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test contexts with proper setup and teardown
  • Clear separation of concerns in test scenarios
  • Comprehensive edge case coverage
  • Configurable test parameters
  • Clean test data management

railsadminteam/rails_admin

spec/integration/actions/history_index_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'HistoryIndex action', type: :request, active_record: true do
  subject { page }

  let(:user) { FactoryBot.create :user }
  let(:paper_trail_test) { FactoryBot.create :paper_trail_test }
  before(:each) do
    RailsAdmin.config do |config|
      config.audit_with :paper_trail, 'User', 'PaperTrail::Version'
    end

    PaperTrail::Version.delete_all
    with_versioning do
      PaperTrail.request.whodunnit = user.id
      30.times do |i|
        paper_trail_test.update!(name: "updated name #{i}")
      end
    end
  end

  it 'shows the history' do
    visit history_index_path(model_name: 'paper_trail_test')
    is_expected.to have_css(%([href="/admin/paper_trail_test/#{paper_trail_test.id}"]), count: 20)
  end

  it 'supports pagination' do
    visit history_index_path(model_name: 'paper_trail_test', page: 2)
    is_expected.to have_css(%([href="/admin/paper_trail_test/#{paper_trail_test.id}"]), count: 11)
  end

  it 'supports sorting', js: true do
    visit history_index_path(model_name: 'paper_trail_test')
    find('th.header', text: 'Item').click
    is_expected.to have_css('th.item.headerSortDown')
  end

  context "when Kaminari's custom param_name is set" do
    before { Kaminari.config.param_name = :pagina }
    after { Kaminari.config.param_name = :page }

    it 'picks the page value from params' do
      visit history_index_path(model_name: 'paper_trail_test', pagina: 2)
      is_expected.to have_css(%([href="/admin/paper_trail_test/#{paper_trail_test.id}"]), count: 11)
    end
  end
end