Files
sure/test/controllers/family_merchants_controller_test.rb
T
4930aacdb8 feat: create merchant inline from transaction detail (#3106)
* feat: create merchant inline from transaction detail

Add a searchable "create or select" merchant combobox (DS::MerchantSelect,
mirroring the existing DS::TagSelect pattern) so a new FamilyMerchant can be
created directly from the transaction detail and new-transaction forms,
instead of requiring a trip to Settings > Merchants first.

FamilyMerchantsController#create now also responds to JSON so the combobox
can create-and-select a merchant without a full page reload.

* fix: address PR review feedback on merchant inline creation

- Handle Turbo validation failures in FamilyMerchantsController#create
  (missing format.turbo_stream branch raised ActionController::UnknownFormat)
- Guard connect() so disabled merchant selectors (no menu target) don't throw
- Select the exact match (or block form submission) on Enter when the
  create row is hidden
- Catch network/parse failures in createMerchant and show an error
- Localize the fallback "could not create merchant" error message
- Move the create button/error text outside the listbox and add
  aria-controls for accessibility
- Align create-option avatar size with the selected-value display

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: keep created merchant option inside the listbox, move logo URL logic to component

- Add a dedicated listbox target and insert newly created merchant
  options inside it (previously landed outside role="listbox" after
  the create button was moved out in the prior review-fix commit)
- Move selected-merchant logo URL transformation out of the template
  into DS::MerchantSelect#selected_merchant_logo_url

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: address maintainer review feedback on merchant inline creation

- Drop the explicit format.turbo_stream branch in the merchant creation
  failure path: respond_to's turbo_stream matching forces the response
  Content-Type to text/vnd.turbo-stream.html before the block runs, so
  render :new, formats: [:html] only changed template lookup, not the
  Content-Type. Turbo's client then received a turbo-stream response
  with no <turbo-stream> tags and silently did nothing. Leaving
  turbo_stream undeclared lets Rails negotiate down to format.html,
  which renders :new with the correct text/html type.
- Add truncate/shrink-0 classes to the merchant option partial's name
  and avatar spans so they match the selected-value display once
  updateSelectionDisplay clones them into the trigger button.
- Add a regression test simulating a Turbo form submission (Accept:
  turbo-stream, text/html) against a duplicate merchant name.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Gerald <248542187+gfr-free@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-26 22:40:46 +02:00

84 lines
2.6 KiB
Ruby

require "test_helper"
class FamilyMerchantsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@merchant = merchants(:netflix)
end
test "index" do
get family_merchants_path
assert_response :success
end
test "new" do
get new_family_merchant_path
assert_response :success
end
test "should create merchant" do
assert_difference("FamilyMerchant.count") do
post family_merchants_url, params: { family_merchant: { name: "new merchant", color: "#000000" } }
end
assert_redirected_to family_merchants_path
created_merchant = FamilyMerchant.find_by(name: "new merchant")
assert_equal "#000000", created_merchant.color
end
test "should update merchant" do
patch family_merchant_url(@merchant), params: { family_merchant: { name: "new name", color: "#000000" } }
assert_redirected_to family_merchants_path
assert_equal "#000000", @merchant.reload.color
end
test "should destroy merchant" do
assert_difference("FamilyMerchant.count", -1) do
delete family_merchant_url(@merchant)
end
assert_redirected_to family_merchants_path
end
test "should create merchant as json" do
assert_difference("FamilyMerchant.count") do
post family_merchants_url(format: :json), params: { family_merchant: { name: "Quick Merchant" } }
end
assert_response :created
response_body = JSON.parse(response.body)
assert_equal "Quick Merchant", response_body["name"]
assert response_body["id"].present?
assert_includes response_body["html"], "data-merchant-select-target=\"option\""
end
test "should return json validation errors for duplicate merchant name" do
assert_no_difference("FamilyMerchant.count") do
post family_merchants_url(format: :json), params: { family_merchant: { name: @merchant.name } }
end
assert_response :unprocessable_entity
assert JSON.parse(response.body)["errors"].present?
end
test "renders html (not turbo-stream) for duplicate merchant name when submitted like a Turbo form" do
assert_no_difference("FamilyMerchant.count") do
post family_merchants_url,
params: { family_merchant: { name: @merchant.name } },
headers: { "Accept" => "text/vnd.turbo-stream.html, text/html, application/xhtml+xml" }
end
assert_response :unprocessable_entity
assert_equal "text/html", response.media_type
assert_includes response.body, @merchant.name
end
test "enhance enqueues job and redirects" do
assert_enqueued_with(job: EnhanceProviderMerchantsJob) do
post enhance_family_merchants_path
end
assert_redirected_to family_merchants_path
end
end