From 4bcbac1ba3600613373443e7943879fdbbf5da74 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 8 May 2020 09:48:28 -0400 Subject: [PATCH] Unselect a Seat from the `` For comparison [see this commit][commit] Nothing exciting here. The ergonomics is similar to the Stimulus version. [commit]: https://github.com/seanpdoyle/select-your-own-seat/commit/2e69ac9158d9ef30c244ac034fb910ccd0e6095d --- app/models/cart.rb | 4 ++++ app/views/seats/show.json.props | 19 +++++++++++----- test/application_system_test_case.rb | 4 ++++ .../controllers/selections_controller_test.rb | 2 +- test/models/cart_test.rb | 22 +++++++++++++++++++ test/system/visitor_unselects_seat_test.rb | 17 ++++++++++++++ test/system/visitor_views_seats_test.rb | 4 ---- 7 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 test/models/cart_test.rb diff --git a/app/models/cart.rb b/app/models/cart.rb index 62b78c91..9e146a33 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -3,4 +3,8 @@ class Cart < ApplicationRecord has_many :seat_selections has_many :seats, through: :seat_selections + + def include?(seat) + seat_ids.include?(seat.id) + end end diff --git a/app/views/seats/show.json.props b/app/views/seats/show.json.props index 9b74fc2f..6548e29d 100644 --- a/app/views/seats/show.json.props +++ b/app/views/seats/show.json.props @@ -1,9 +1,9 @@ json.venue_name venue.name -json.sections(partial: ['sections', locals: local_assigns]) do +json.sections(partial: ["sections", locals: local_assigns]) do end -json.cart(partial: ['cart', locals: local_assigns]) do +json.cart(partial: ["cart", locals: local_assigns]) do end json.seat do @@ -11,9 +11,18 @@ json.seat do json.section_name seat.section.name json.row_number seat.row_number json.price number_to_currency(seat.section.price / 100.0) - json.seat_selection_form do - form_props(url: seat_selections_path(seat)) do |f| - f.submit + + if Current.cart.include?(seat) + json.seat_selection_form do + form_props(url: seat_selections_path(seat), method: :delete) do |f| + f.submit(text: "Remove") + end + end + else + json.seat_selection_form do + form_props(url: seat_selections_path(seat)) do |f| + f.submit(text: "Select") + end end end end diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index d19212ab..6d85d7a4 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -2,4 +2,8 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :chrome, screen_size: [1400, 1400] + + def click_on_seat(row_number) + find(%{[aria-label*="#{row_number}"]}).click + end end diff --git a/test/controllers/selections_controller_test.rb b/test/controllers/selections_controller_test.rb index 76b31c94..2f59574e 100644 --- a/test/controllers/selections_controller_test.rb +++ b/test/controllers/selections_controller_test.rb @@ -26,7 +26,7 @@ class SelectionsControllerTest < ActionDispatch::IntegrationTest seat = seat_selection.seat cookies[:cart_token] = cart.token - delete seat_selection_path(seat, seat_selection) + delete seat_selection_path(seat) assert_equal cart.seats.ids, [] end diff --git a/test/models/cart_test.rb b/test/models/cart_test.rb new file mode 100644 index 00000000..e98e76fd --- /dev/null +++ b/test/models/cart_test.rb @@ -0,0 +1,22 @@ +require "test_helper" + +class CartTest < ActiveSupport::TestCase + test "#include? returns true when a seat is selected" do + seat_selection = create(:seat_selection) + seat = seat_selection.seat + cart = seat_selection.cart + + included = cart.include?(seat) + + assert included, "cart includes selected seat" + end + + test "#include? returns false when a seat is not yet selected" do + seat = create(:seat) + cart = create(:cart) + + included = cart.include?(seat) + + refute included, "cart includes selected seat" + end +end diff --git a/test/system/visitor_unselects_seat_test.rb b/test/system/visitor_unselects_seat_test.rb index bfb6a23a..439bbb59 100644 --- a/test/system/visitor_unselects_seat_test.rb +++ b/test/system/visitor_unselects_seat_test.rb @@ -15,4 +15,21 @@ class VisitorUnselectsSeatTest < ApplicationSystemTestCase assert_no_text "$10.00" end end + + test "from the details dialog of an already selected seat" do + venue = create(:benedum_center) + floor = create(:orchestra, venue: venue) + section = create(:section, floor: floor, price: 10_00) + seat = create(:seat, row: "AA", number: "101", section: section) + + visit "/venues/benedum_center/floors/orchestra/seats" + click_on_seat "AA-101" + click_on "Select" + click_on_seat "AA-101" + within("dialog") { click_on "Remove" } + + within "#cart-summary" do + assert_no_text "$10.00" + end + end end diff --git a/test/system/visitor_views_seats_test.rb b/test/system/visitor_views_seats_test.rb index 6678b429..4461a435 100644 --- a/test/system/visitor_views_seats_test.rb +++ b/test/system/visitor_views_seats_test.rb @@ -12,8 +12,4 @@ class VisitorViewsSeatsTest < ApplicationSystemTestCase assert_text("$10.00") end - - def click_on_seat(row_number) - find(%{[aria-label*="#{row_number}"]}).click - end end