# frozen_string_literal: true

require 'spec_helper'

module Api
  describe V0::ProductImagesController, type: :controller do
    include AuthenticationHelper
    include FileHelper
    render_views

    describe "uploading an image" do
      before do
        allow(controller).to receive(:spree_current_user) { current_api_user }
      end

      let(:image) { Rack::Test::UploadedFile.new(black_logo_file, 'image/png') }
      let!(:product_without_image) { create(:product) }
      let!(:product_with_image) { create(:product_with_image) }
      let(:current_api_user) { create(:admin_user) }

      it "saves a new image when none is present" do
        post :update_product_image, xhr: true,
                                    params: { product_id: product_without_image.id, file: image, use_route: :product_images }

        expect(response.status).to eq 201
        expect(product_without_image.images.first.id).to eq json_response['id']
      end

      it "updates an existing product image" do
        post :update_product_image, xhr: true,
                                    params: { product_id: product_with_image.id, file: image, use_route: :product_images }

        expect(response.status).to eq 200
        expect(product_with_image.images.first.id).to eq json_response['id']
      end
    end
  end
end

# frozen_string_literal: true

require 'spec_helper'

describe Api::V0::ProductImagesController, type: :controller do
  include AuthenticationHelper
  include FileHelper
  render_views

  describe "uploading an image" do
    let(:image) { Rack::Test::UploadedFile.new(black_logo_file, 'image/png') }
    let(:product_without_image) { create(:product) }
    let(:product_with_image) { create(:product_with_image) }
    let(:current_api_user) { create(:admin_user) }

    before do
      allow(controller).to receive(:spree_current_user) { current_api_user }
    end

    it "saves a new image when none is present" do
      post :update_product_image, xhr: true, params: {
        product_id: product_without_image.id, file: image, use_route: :product_images
      }

      expect(response.status).to eq 201
      expect(product_without_image.images.first.id).to eq json_response['id']
    end

    it "updates an existing product image" do
      post :update_product_image, xhr: true, params: {
        product_id: product_with_image.id, file: image, use_route: :product_images
      }

      expect(response.status).to eq 200
      expect(product_with_image.images.first.id).to eq json_response['id']
    end
  end
end

Increase readability of image controller spec
Increase readability of image controller spec

Best viewed without whitespace changes.

- Decrease indent.
- Move `let` to top like in other specs.
- Avoid `let!` to speed up the specs.
