Back to Repositories

Testing History Show Action Implementation in Rails Admin

This test suite validates the History Show functionality in Rails Admin, focusing on PaperTrail integration for audit logging and version history display. It verifies pagination behavior and custom parameter handling for historical record viewing.

Test Coverage Overview

The test suite covers comprehensive history tracking functionality with PaperTrail integration.

  • Basic history display verification
  • Pagination functionality testing
  • Custom pagination parameter handling
  • Version history tracking across multiple updates

Implementation Analysis

The implementation uses RSpec request specs to validate the history show action functionality. It leverages FactoryBot for test data generation and PaperTrail’s versioning system to create historical records. The tests demonstrate proper setup of audit trails and version tracking.

Technical Details

  • RSpec request specs framework
  • FactoryBot for test data creation
  • PaperTrail for version tracking
  • Kaminari for pagination
  • Rails Admin configuration for audit logging

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper isolation of test scenarios, clean setup and teardown of test data, and comprehensive edge case coverage. It demonstrates effective use of context blocks for different test scenarios and proper handling of configuration changes.

  • Isolated test environments
  • Proper cleanup of test data
  • Clear test case organization
  • Configuration management

railsadminteam/rails_admin

spec/integration/actions/history_show_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'HistoryShow action', type: :request, active_record: true do
  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_show_path(model_name: 'paper_trail_test', id: paper_trail_test.id)
    expect(all('table#history tbody tr').count).to eq(20)
  end

  it 'supports pagination' do
    visit history_show_path(model_name: 'paper_trail_test', id: paper_trail_test.id, page: 2)
    expect(all('table#history tbody tr').count).to eq(11)
  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_show_path(model_name: 'paper_trail_test', id: paper_trail_test.id, pagina: 2)
      expect(all('table#history tbody tr').count).to eq(11)
    end
  end
end