Requested changes: turbo_stream for success

- must respond via turbo for create and update for success
- changed the spec accordingly
This commit is contained in:
cyrillefr
2024-06-13 15:22:54 +02:00
parent c5decfc58b
commit 0470efa502
2 changed files with 19 additions and 10 deletions

View File

@@ -38,7 +38,10 @@ module Spree
@object.save!
flash[:success] = flash_message_for(@object, :successfully_created)
redirect_to location_after_save
respond_to do |format|
format.html { redirect_to location_after_save }
format.turbo_stream { render :update }
end
rescue ActiveRecord::RecordInvalid => e
respond_with_error(e)
end
@@ -50,7 +53,10 @@ module Spree
@object.update!(permitted_resource_params)
flash[:success] = flash_message_for(@object, :successfully_updated)
redirect_to location_after_save
respond_to do |format|
format.html { redirect_to location_after_save }
format.turbo_stream
end
rescue ActiveRecord::RecordInvalid => e
respond_with_error(e)
end

View File

@@ -11,7 +11,7 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do
login_as_admin
end
shared_examples "updating images" do
shared_examples "updating images" do |expected_http_status_code|
let(:params) do
{
image: {
@@ -21,14 +21,16 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do
}
end
it "creates a new image and redirects" do
it "creates a new image and redirects unless called by turbo" do
expect {
subject
product.reload
}.to change{ product.image&.attachment&.filename.to_s }
expect(response.status).to eq 302
expect(response.location).to end_with spree.admin_product_images_path(product)
expect(response.status).to eq expected_http_status_code
if expected_http_status_code == 302
expect(response.location).to end_with spree.admin_product_images_path(product)
end
expect(product.image.url(:product)).to end_with "logo.png"
end
@@ -57,12 +59,13 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do
describe "POST /admin/products/:product_id/images" do
subject { post(spree.admin_product_images_path(product), params:) }
it_behaves_like "updating images"
it_behaves_like "updating images", 302
end
describe "POST /admin/products/:product_id/images with turbo" do
subject { post(spree.admin_product_images_path(product), params:, as: :turbo_stream) }
it_behaves_like "updating images"
it_behaves_like "updating images", 200
end
describe "PATCH /admin/products/:product_id/images/:id" do
@@ -71,7 +74,7 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do
patch(spree.admin_product_image_path(product, product.image), params:)
}
it_behaves_like "updating images"
it_behaves_like "updating images", 302
end
describe "PATCH /admin/products/:product_id/images/:id with turbo" do
@@ -80,6 +83,6 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do
patch(spree.admin_product_image_path(product, product.image), params:, as: :turbo_stream)
}
it_behaves_like "updating images"
it_behaves_like "updating images", 200
end
end