Back to Repositories

Testing Multiple File Upload Management in Rails Admin

This integration test suite validates the MultipleCarrierwave field functionality in Rails Admin, focusing on file upload handling and management. The tests cover various aspects of multiple file upload operations, including creation, modification, and ordering of uploaded assets.

Test Coverage Overview

The test suite provides comprehensive coverage of multiple file upload operations in Rails Admin:

  • Multiple file upload validation
  • File appending to existing uploads
  • File deletion functionality
  • File reordering capabilities

Implementation Analysis

The testing approach utilizes RSpec request specs with JavaScript support for dynamic interactions. It implements factory-based test data generation and employs jQuery simulation for drag-and-drop functionality testing.

The tests use specific patterns for file handling and asset management, incorporating CarrierWave’s multiple upload features.

Technical Details

Key technical components include:

  • RSpec request specs with JS capability
  • jQuery for drag-and-drop simulation
  • CarrierWave configuration for multiple file handling
  • FactoryBot for test data generation
  • Custom JavaScript execution for sortable functionality

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test contexts for different scenarios
  • Proper setup and teardown of test data
  • Clear separation of concerns in test organization
  • Comprehensive coverage of edge cases
  • Effective use of helper methods and factories

railsadminteam/rails_admin

spec/integration/fields/multiple_carrierwave_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'MultipleCarrierwave field', type: :request, active_record: true do
  subject { page }
  before do
    RailsAdmin.config FieldTest do
      edit do
        field :carrierwave_assets
      end
    end
  end

  it 'supports uploading multiple files', js: true do
    visit new_path(model_name: 'field_test')
    attach_file 'Carrierwave assets', [file_path('test.jpg'), file_path('test.png')]
    click_button 'Save'
    is_expected.to have_content 'Field test successfully created'
    expect(FieldTest.first.carrierwave_assets.map { |image| File.basename(image.url) }).to match_array ['test.jpg', 'test.png']
  end

  context 'when working with existing files' do
    let(:field_test) { FactoryBot.create(:field_test, carrierwave_assets: ['test.jpg', 'test.png'].map { |img| File.open(file_path(img)) }) }

    it 'supports appending a file', js: true do
      visit edit_path(model_name: 'field_test', id: field_test.id)
      attach_file 'Carrierwave assets', [file_path('test.gif')]
      click_button 'Save'
      is_expected.to have_content 'Field test successfully updated'
      field_test.reload
      expect(field_test.carrierwave_assets.map { |image| File.basename(image.url) }).to eq ['test.jpg', 'test.png', 'test.gif']
    end

    it 'supports deleting a file', js: true do
      visit edit_path(model_name: 'field_test', id: field_test.id)
      click_link "Delete 'Carrierwave assets' #1"
      click_button 'Save'
      is_expected.to have_content 'Field test successfully updated'
      field_test.reload
      expect(field_test.carrierwave_assets.map { |image| File.basename(image.url) }).to eq ['test.png']
    end

    it 'supports reordering files', js: true do
      visit edit_path(model_name: 'field_test', id: field_test.id)
      page.execute_script File.read(File.expand_path('../../../vendor/assets/javascripts/rails_admin/jquery3.js', __dir__))
      page.execute_script File.read(File.expand_path('../../support/jquery.simulate.drag-sortable.js', __dir__))
      page.execute_script %{$(".ui-sortable-handle:first-child").simulateDragSortable({move: 1});}
      click_button 'Save'
      is_expected.to have_content 'Field test successfully updated'
      field_test.reload
      expect(field_test.carrierwave_assets.map { |image| File.basename(image.url) }).to eq ['test.png', 'test.jpg']
    end
  end
end