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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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