Skip to content
This repository
tree: 40b68ae6e1
Fetching contributors…

Cannot retrieve contributors at this time

file 113 lines (86 sloc) 4.366 kb
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
require "spec_helper"

module Refinery
  describe "AdminImages" do
    refinery_login_with :refinery_user

    context "when no images" do
      it "invites to add one" do
        visit refinery.admin_images_path
        page.should have_content(::I18n.t('no_images_yet', :scope => 'refinery.admin.images.records'))
      end
    end

    it "shows add new image link" do
      visit refinery.admin_images_path
      page.should have_content(::I18n.t('create_new_image', :scope => 'refinery.admin.images.actions'))
      page.should have_selector("a[href*='#{refinery.new_admin_image_path}']")
    end

    context "new/create" do
      it "uploads image", :js => true do
        visit refinery.admin_images_path

        click_link ::I18n.t('create_new_image', :scope => 'refinery.admin.images.actions')

        page.should have_selector 'iframe#dialog_iframe'

        page.within_frame('dialog_iframe') do
          attach_file "image_image", Refinery.roots(:'refinery/images').
                                              join("spec/fixtures/image-with-dashes.jpg")
          click_button ::I18n.t('save', :scope => 'refinery.admin.form_actions')
        end
        page.should have_content(::I18n.t("uploaded_successfully", :scope => "refinery.admin.images.form"))
        page.should have_selector("img[src*='image-with-dashes.jpg']")
        Refinery::Image.count.should == 1
      end

      it 'is accessible via url' do
        image = Refinery::Image.create(:image => Refinery.roots(:'refinery/images').join("spec/fixtures/image-with-dashes.jpg"))
        get image.url

        response.should be_success
      end
    end

    context "when an image exists" do
      let!(:image) { FactoryGirl.create(:image) }

      context "edit/update" do
        it "updates image" do
          visit refinery.admin_images_path
          page.should have_selector("a[href='#{refinery.edit_admin_image_path(image)}']")

          click_link ::I18n.t('edit', :scope => 'refinery.admin.images')

          page.should have_content("Use current image or replace it with this one...")
          page.should have_selector("a[href*='#{refinery.admin_images_path}']")

          attach_file "image_image", Refinery.roots(:'refinery/images').join("spec/fixtures/beach.jpeg")
          click_button ::I18n.t('save', :scope => 'refinery.admin.form_actions')

          page.should have_content(::I18n.t('updated', :scope => 'refinery.crudify', :what => "'Beach'"))
          Refinery::Image.count.should == 1

          lambda { click_link "View this image" }.should_not raise_error
        end

        it "doesn't allow updating if image has different file name" do
          visit refinery.edit_admin_image_path(image)

          attach_file "image_image", Refinery.roots(:'refinery/images').join("spec/fixtures/fathead.png")
          click_button ::I18n.t('save', :scope => 'refinery.admin.form_actions')

          page.should have_content(::I18n.t("different_file_name",
                                            :scope => "activerecord.errors.models.refinery/image"))
        end
      end

      context "destroy" do
        it "removes image" do
          visit refinery.admin_images_path
          page.should have_selector("a[href='#{refinery.admin_image_path(image)}']")

          click_link ::I18n.t('delete', :scope => 'refinery.admin.images')

          page.should have_content(::I18n.t('destroyed', :scope => 'refinery.crudify', :what => "'Beach'"))
          Refinery::Image.count.should == 0
        end
      end

      context "download" do
        it "succeeds" do
          visit refinery.admin_images_path

          lambda { click_link "View this image" }.should_not raise_error
        end
      end

      describe "switch view" do
        it "shows images in grid" do
          visit refinery.admin_images_path
          page.should have_content(::I18n.t('switch_to', :view_name => 'list', :scope => 'refinery.admin.images.index.view'))
          page.should have_selector("a[href='#{refinery.admin_images_path(:view => 'list')}']")

          click_link "Switch to list view"

          page.should have_content(::I18n.t('switch_to', :view_name => 'grid', :scope => 'refinery.admin.images.index.view'))
          page.should have_selector("a[href='#{refinery.admin_images_path(:view => 'grid')}']")
        end
      end
    end
  end
end
Something went wrong with that request. Please try again.