1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
require 'spec_helper'
describe CarrierWave::Uploader do
let(:uploader_class) { Class.new(CarrierWave::Uploader::Base) }
let(:uploader) { uploader_class.new }
after { FileUtils.rm_rf(public_path) }
describe '.process' do
context "when a symbol is given" do
before { uploader_class.process(process_param) }
after { uploader.process! }
let(:process_param) { :sepiatone }
it "adds a single processor" do
expect(uploader).to receive(:sepiatone)
end
end
context "when an array of symbols is given" do
before { uploader_class.process(*process_param) }
after { uploader.process! }
let(:process_param) { [:sepiatone, :desaturate, :invert] }
it "adds multiple processors" do
expect(uploader).to receive(:sepiatone)
expect(uploader).to receive(:desaturate)
expect(uploader).to receive(:invert)
end
end
it "adds a single processor with an argument when a hash is given" do
uploader_class.process :format => 'png'
expect(uploader).to receive(:format).with('png')
uploader.process!
end
it "adds a single processor with several argument when a hash is given" do
uploader_class.process :resize => [200, 300]
expect(uploader).to receive(:resize).with(200, 300)
uploader.process!
end
it "adds multiple processors when an hash with multiple keys is given" do
uploader_class.process :resize => [200, 300], :format => 'png'
expect(uploader).to receive(:resize).with(200, 300)
expect(uploader).to receive(:format).with('png')
uploader.process!
end
it "calls the processor if the condition method returns true" do
uploader_class.process :resize => [200, 300], :if => :true?
uploader_class.process :fancy, :if => :true?
expect(uploader).to receive(:true?).with("test.jpg").twice.and_return(true)
expect(uploader).to receive(:resize).with(200, 300)
expect(uploader).to receive(:fancy)
uploader.process!("test.jpg")
end
it "doesn't call the processor if the condition method returns false" do
uploader_class.process :resize => [200, 300], :if => :false?
uploader_class.process :fancy, :if => :false?
expect(uploader).to receive(:false?).with("test.jpg").twice.and_return(false)
expect(uploader).not_to receive(:resize)
expect(uploader).not_to receive(:fancy)
uploader.process!("test.jpg")
end
it "calls the processor if the condition block returns true" do
uploader_class.process :resize => [200, 300], :if => lambda{|record, args| record.true?(args[:file])}
uploader_class.process :fancy, :if => :true?
expect(uploader).to receive(:true?).with("test.jpg").twice.and_return(true)
expect(uploader).to receive(:resize).with(200, 300)
expect(uploader).to receive(:fancy)
uploader.process!("test.jpg")
end
it "doesn't call the processor if the condition block returns false" do
uploader_class.process :resize => [200, 300], :if => lambda{|record, args| record.false?(args[:file])}
uploader_class.process :fancy, :if => :false?
expect(uploader).to receive(:false?).with("test.jpg").twice.and_return(false)
expect(uploader).not_to receive(:resize)
expect(uploader).not_to receive(:fancy)
uploader.process!("test.jpg")
end
context "when using RMagick", :rmagick => true do
before do
def uploader.cover
manipulate! { |frame, index| frame if index.zero? }
end
uploader_class.send :include, CarrierWave::RMagick
end
after { uploader.instance_eval { undef cover } }
context "with a multi-page PDF" do
before { uploader.cache! File.open(file_path("multi_page.pdf")) }
it "successfully processes" do
uploader_class.process :convert => 'jpg'
uploader.process!
end
it "supports page specific transformations" do
uploader_class.process :cover
uploader.process!
end
end
context "with a simple image" do
before { uploader.cache! File.open(file_path("portrait.jpg")) }
it "allows page specific transformations" do
uploader_class.process :cover
uploader.process!
end
end
end
context "with 'enable_processing' set to false" do
before { uploader_class.enable_processing = false }
it "doesn't do any processing" do
uploader_class.process :sepiatone, :desaturate, :invert
expect(uploader).not_to receive(:sepiatone)
expect(uploader).not_to receive(:desaturate)
expect(uploader).not_to receive(:invert)
uploader.process!
end
end
end
describe '#cache!' do
before do
allow(CarrierWave).to receive(:generate_cache_id).and_return('1369894322-345-1234-2255')
end
it "triggers a process!" do
expect(uploader).to receive(:process!)
uploader.cache!(File.open(file_path('test.jpg')))
end
end
describe '#recreate_versions!' do
before do
allow(CarrierWave).to receive(:generate_cache_id).and_return('1369894322-345-1234-2255')
end
it "triggers a process!" do
uploader.store!(File.open(file_path('test.jpg')))
expect(uploader).to receive(:process!)
uploader.recreate_versions!
end
end
end
|