require 'rails_helper'

RSpec.describe DamMedia, type: :model do
  describe 'should return the existing mounted uploader based on its type' do
    it 'is image' do
      dam_media = create(:dam_media, :image)
      expect(dam_media.media).to be_a(DamImageUploader)
    end

    it 'is audio' do
      allow_any_instance_of(DamAudioUploader).to receive(:move_to_public).and_return(true)
      dam_media = create(:dam_media, :audio)
      expect(dam_media.media).to be_a(DamAudioUploader)
    end

    it 'is video' do
      allow_any_instance_of(DamVideoUploader).to receive(:move_to_public).and_return(true)
      dam_media = create(:dam_media, :video)
      expect(dam_media.media).to be_a(DamVideoUploader)
    end
  end

  it 'should create a IMAGE media if file type is present in the ImageUploader whitelist' do
    file_path = File.join(Rails.root, '../../spec', 'support', 'files', 'sample.jpg')
    allow(File).to receive(:open)
    expect do |block|
      DamMedia.create_by_file(file_path, &block)
    end.to yield_with_args(DamMedia, 'IMAGE')
  end

  it 'should create a AUDIO media if file type is present in the AudioUploader whitelist' do
    file_path = File.join(Rails.root, '../../spec', 'support', 'files', 'sample.mp3')
    allow(File).to receive(:open)
    expect do |block|
      DamMedia.create_by_file(file_path, &block)
    end.to yield_with_args(DamMedia, 'AUDIO')
  end

  it 'should create a VIDEO media if file type is present in the VideoUploader whitelist' do
    file_path = File.join(Rails.root, '../../spec', 'support', 'files', 'sample.mp4')
    allow(File).to receive(:open)
    expect do |block|
      DamMedia.create_by_file(file_path, &block)
    end.to yield_with_args(DamMedia, 'VIDEO')
  end

  it 'should raise invalid file extension exception if invalid' do
    file_path = File.join(Rails.root, '../../spec', 'support', 'files', 'sample.pdf')
    allow(File).to receive(:open)
    expect do |block|
      DamMedia.create_by_file(file_path, &block)
    end.to raise_exception(InvalidFileExtensionException)
  end
end
