From ae233b7925daf8351a1c7e310414905d5c4a8ba1 Mon Sep 17 00:00:00 2001 From: wipegup Date: Thu, 4 Apr 2019 17:09:35 -0600 Subject: [PATCH 01/13] Add changing quantities test for cart --- app/views/carts/_cart_item.html.erb | 8 +++++--- spec/features/cart/show_cart_spec.rb | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/views/carts/_cart_item.html.erb b/app/views/carts/_cart_item.html.erb index 949d124c..c0605139 100644 --- a/app/views/carts/_cart_item.html.erb +++ b/app/views/carts/_cart_item.html.erb @@ -1,7 +1,9 @@
> -
<%=item.name%>
+
Item Name: <%=item.name%>
<%=item.user.name%>
<%=item.current_price%>
-
<%=quantity%>
-
<%=quantity*item.current_price%>
+
+ <%=number_field_tag("Quantity", value = quantity, min:0, max:item.quantity, step:1)%> +
+
<%=(quantity*item.current_price).round(2)%>
diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index f4aa3555..64ed2aff 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -57,6 +57,32 @@ expect(page).to have_selector('div', id:"total", text:total.round(2)) end + it 'can update quantities of items in the cart' do + visit item_path(@item_1) + click_button "Add to Cart" + binding.pry + visit cart_path + + fill_in "Quantity", with: 3 + click_button "Update Cart" + + expect(current_path).to eq(cart_path) + expect(page).to have_field('Quantity', with:3 ) + end + + it 'upon updating a quantity to 0, that item is removed from the cart' do + visit item_path(@item_1) + click_button "Add to Cart" + binding.pry + visit cart_path + + fill_in "Quantity", with: 0 + click_button "Update Cart" + + expect(current_path).to eq(cart_path) + expect(page).to have_content(@empty_cart_message) + end + end RSpec.describe 'partial for items in cart' ,type: :view do @@ -69,7 +95,7 @@ expect(rendered).to have_selector('div', id:"item-name", text:item.name) expect(rendered).to have_selector('div', id:"item-merchant", text:item.user.name) expect(rendered).to have_selector('div', id:"item-price", text:item.current_price) - expect(rendered).to have_selector('div', id:"item-quantity", text:quantity) + expect(rendered).to have_field('Quantity', with:quantity) expect(rendered).to have_selector('div', id:"subtotal", text:"#{item.current_price * quantity}") end end From 9e3d7408d577e11a6d2580dd1b9e95db21b920a1 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:21:41 -0600 Subject: [PATCH 02/13] Change quantity is now an update feature --- app/controllers/carts_controller.rb | 7 ++++++- app/views/carts/_cart_item.html.erb | 3 +++ config/routes.rb | 1 + spec/features/cart/show_cart_spec.rb | 9 +++++---- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb index f535814f..7856a6da 100644 --- a/app/controllers/carts_controller.rb +++ b/app/controllers/carts_controller.rb @@ -1,11 +1,16 @@ class CartsController < ApplicationController before_action :require_customer + def update + @cart.update_quantity(params[:item_id], params[:Quantity]) + redirect_to cart_path + end + def destroy session[:cart] = {} redirect_to cart_path end - + def show @items = {} @cart.contents.each do |item_id, quantity| diff --git a/app/views/carts/_cart_item.html.erb b/app/views/carts/_cart_item.html.erb index c0605139..b2f078f3 100644 --- a/app/views/carts/_cart_item.html.erb +++ b/app/views/carts/_cart_item.html.erb @@ -3,7 +3,10 @@
<%=item.user.name%>
<%=item.current_price%>
+ <%= form_tag cart_path(item_id: item.id), method: :patch do %> <%=number_field_tag("Quantity", value = quantity, min:0, max:item.quantity, step:1)%> + <%= submit_tag "Update Quantity"%> + <%end%>
<%=(quantity*item.current_price).round(2)%>
diff --git a/config/routes.rb b/config/routes.rb index a74150af..73bd35ba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,7 @@ get '/profile', to: "users#show" get '/cart', to: 'carts#show' delete '/cart', to: 'carts#destroy' + patch '/cart', to: 'carts#update' resources :carts, only: [:create] get '/merchants', to: "merchants#index" diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index 64ed2aff..f1da5508 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -60,11 +60,12 @@ it 'can update quantities of items in the cart' do visit item_path(@item_1) click_button "Add to Cart" - binding.pry + # binding.pry visit cart_path fill_in "Quantity", with: 3 - click_button "Update Cart" + save_and_open_page + click_button "Update Quantity" expect(current_path).to eq(cart_path) expect(page).to have_field('Quantity', with:3 ) @@ -73,11 +74,11 @@ it 'upon updating a quantity to 0, that item is removed from the cart' do visit item_path(@item_1) click_button "Add to Cart" - binding.pry + # binding.pry visit cart_path fill_in "Quantity", with: 0 - click_button "Update Cart" + click_button "Update quantity" expect(current_path).to eq(cart_path) expect(page).to have_content(@empty_cart_message) From 483dd9096c245a4906466711a41c892f99f853d2 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:22:17 -0600 Subject: [PATCH 03/13] Add test for cart update_quantity instance method --- spec/models/cart_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb index a07436ce..b867e672 100644 --- a/spec/models/cart_spec.rb +++ b/spec/models/cart_spec.rb @@ -17,4 +17,14 @@ expect(cart.contents).to eq(expected) end + it "#update_quantity(item_id, quantity), updates the correct item quantity, deleting item if appropriate" do + cart = Cart.new({"5"=> 3, "3" => 4}) + cart.update_quantity("5", 6) + expected = {"5"=>6, "3"=>4} + expect(cart.contents).to eq(expected) + + cart.update_quantity("3",0) + expected = {"5"=>6} + expect(cart.contents.to eq(expected)) + end end From e82bb6aed63f7e7a3d9aa0966e70f0b889141d67 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:25:42 -0600 Subject: [PATCH 04/13] Passing update_quantities test --- app/models/cart.rb | 8 ++++++++ spec/models/cart_spec.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/cart.rb b/app/models/cart.rb index db793dd8..53757cdb 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -16,4 +16,12 @@ def add_item(item_id) def total_count @contents.values.sum end + + def update_quantity(item_id, quantity) + if quantity == 0 + @contents.delete(item_id) + else + @contents[item_id] = quantity + end + end end diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb index b867e672..8c3f499d 100644 --- a/spec/models/cart_spec.rb +++ b/spec/models/cart_spec.rb @@ -25,6 +25,6 @@ cart.update_quantity("3",0) expected = {"5"=>6} - expect(cart.contents.to eq(expected)) + expect(cart.contents).to eq(expected) end end From 51af938773c413eb7437bfa034224e442687f5c2 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:30:11 -0600 Subject: [PATCH 05/13] Passing tests for updating quantities in cart --- app/controllers/carts_controller.rb | 2 +- spec/features/cart/show_cart_spec.rb | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb index 7856a6da..fb5fe32e 100644 --- a/app/controllers/carts_controller.rb +++ b/app/controllers/carts_controller.rb @@ -2,7 +2,7 @@ class CartsController < ApplicationController before_action :require_customer def update - @cart.update_quantity(params[:item_id], params[:Quantity]) + @cart.update_quantity(params[:item_id], params[:Quantity].to_i) redirect_to cart_path end diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index f1da5508..df61f02b 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -60,11 +60,9 @@ it 'can update quantities of items in the cart' do visit item_path(@item_1) click_button "Add to Cart" - # binding.pry visit cart_path fill_in "Quantity", with: 3 - save_and_open_page click_button "Update Quantity" expect(current_path).to eq(cart_path) @@ -74,11 +72,10 @@ it 'upon updating a quantity to 0, that item is removed from the cart' do visit item_path(@item_1) click_button "Add to Cart" - # binding.pry visit cart_path fill_in "Quantity", with: 0 - click_button "Update quantity" + click_button "Update Quantity" expect(current_path).to eq(cart_path) expect(page).to have_content(@empty_cart_message) From a308d372b65ed9229556a1fee93fc03a38f070ef Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:38:49 -0600 Subject: [PATCH 06/13] Passing revoe item button test --- app/views/carts/_cart_item.html.erb | 1 + spec/features/cart/show_cart_spec.rb | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/views/carts/_cart_item.html.erb b/app/views/carts/_cart_item.html.erb index b2f078f3..0ef5de10 100644 --- a/app/views/carts/_cart_item.html.erb +++ b/app/views/carts/_cart_item.html.erb @@ -7,6 +7,7 @@ <%=number_field_tag("Quantity", value = quantity, min:0, max:item.quantity, step:1)%> <%= submit_tag "Update Quantity"%> <%end%> + <%= button_to "Remove Item", cart_path(item_id: item.id, Quantity:0), method: :patch %>
<%=(quantity*item.current_price).round(2)%>
diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index df61f02b..abdba86c 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -73,7 +73,6 @@ visit item_path(@item_1) click_button "Add to Cart" visit cart_path - fill_in "Quantity", with: 0 click_button "Update Quantity" @@ -81,6 +80,16 @@ expect(page).to have_content(@empty_cart_message) end + it 'upon clicking Remove Item, that item is removed from the cart' do + visit item_path(@item_1) + click_button "Add to Cart" + visit cart_path + click_button "Remove Item" + + expect(current_path).to eq(cart_path) + expect(page).to have_content(@empty_cart_message) + end + end RSpec.describe 'partial for items in cart' ,type: :view do @@ -94,6 +103,8 @@ expect(rendered).to have_selector('div', id:"item-merchant", text:item.user.name) expect(rendered).to have_selector('div', id:"item-price", text:item.current_price) expect(rendered).to have_field('Quantity', with:quantity) + expect(rendered).to have_button("Update Quantity") + expect(rendered).to have_button("Remove Item") expect(rendered).to have_selector('div', id:"subtotal", text:"#{item.current_price * quantity}") end end From 9a1e983667f5cb8874269ac6f62b5c82485ab162 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:40:32 -0600 Subject: [PATCH 07/13] Lowercase quantity field name --- app/controllers/carts_controller.rb | 2 +- app/views/carts/_cart_item.html.erb | 4 ++-- spec/features/cart/show_cart_spec.rb | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb index fb5fe32e..cdd04908 100644 --- a/app/controllers/carts_controller.rb +++ b/app/controllers/carts_controller.rb @@ -2,7 +2,7 @@ class CartsController < ApplicationController before_action :require_customer def update - @cart.update_quantity(params[:item_id], params[:Quantity].to_i) + @cart.update_quantity(params[:item_id], params[:quantity].to_i) redirect_to cart_path end diff --git a/app/views/carts/_cart_item.html.erb b/app/views/carts/_cart_item.html.erb index 0ef5de10..6d9e2780 100644 --- a/app/views/carts/_cart_item.html.erb +++ b/app/views/carts/_cart_item.html.erb @@ -4,10 +4,10 @@
<%=item.current_price%>
<%= form_tag cart_path(item_id: item.id), method: :patch do %> - <%=number_field_tag("Quantity", value = quantity, min:0, max:item.quantity, step:1)%> + <%=number_field_tag("quantity", value = quantity, min:0, max:item.quantity, step:1)%> <%= submit_tag "Update Quantity"%> <%end%> - <%= button_to "Remove Item", cart_path(item_id: item.id, Quantity:0), method: :patch %> + <%= button_to "Remove Item", cart_path(item_id: item.id, quantity:0), method: :patch %>
<%=(quantity*item.current_price).round(2)%>
diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index abdba86c..eef957df 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -62,18 +62,18 @@ click_button "Add to Cart" visit cart_path - fill_in "Quantity", with: 3 + fill_in "quantity", with: 3 click_button "Update Quantity" expect(current_path).to eq(cart_path) - expect(page).to have_field('Quantity', with:3 ) + expect(page).to have_field('quantity', with:3 ) end it 'upon updating a quantity to 0, that item is removed from the cart' do visit item_path(@item_1) click_button "Add to Cart" visit cart_path - fill_in "Quantity", with: 0 + fill_in "quantity", with: 0 click_button "Update Quantity" expect(current_path).to eq(cart_path) @@ -102,7 +102,7 @@ expect(rendered).to have_selector('div', id:"item-name", text:item.name) expect(rendered).to have_selector('div', id:"item-merchant", text:item.user.name) expect(rendered).to have_selector('div', id:"item-price", text:item.current_price) - expect(rendered).to have_field('Quantity', with:quantity) + expect(rendered).to have_field('quantity', with:quantity) expect(rendered).to have_button("Update Quantity") expect(rendered).to have_button("Remove Item") expect(rendered).to have_selector('div', id:"subtotal", text:"#{item.current_price * quantity}") From 41411eba6dce18c25f6a4714fb94eccf1b575aca Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 06:44:18 -0600 Subject: [PATCH 08/13] Tested only removing one item with 'remove item' button --- spec/features/cart/show_cart_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index eef957df..9c36af61 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -90,6 +90,20 @@ expect(page).to have_content(@empty_cart_message) end + it 'clicking remove item does not remove the other items' do + visit item_path(@item_1) + click_button "Add to Cart" + visit item_path(@item_2) + click_button "Add to Cart" + visit cart_path + + within "#cart-item-#{@item_1.id}" do + click_button "Remove Item" + end + + expect(page).to have_content(@item_2.name) + expect(page).not_to have_content(@item_1.name) + end end RSpec.describe 'partial for items in cart' ,type: :view do From aa5cd23173e93f8b626c46e1b5bd1d0715a81e2e Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 07:10:40 -0600 Subject: [PATCH 09/13] Change quantity to dropdown --- app/views/carts/_cart_item.html.erb | 3 ++- spec/features/cart/show_cart_spec.rb | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/views/carts/_cart_item.html.erb b/app/views/carts/_cart_item.html.erb index 6d9e2780..5f38b3de 100644 --- a/app/views/carts/_cart_item.html.erb +++ b/app/views/carts/_cart_item.html.erb @@ -4,7 +4,8 @@
<%=item.current_price%>
<%= form_tag cart_path(item_id: item.id), method: :patch do %> - <%=number_field_tag("quantity", value = quantity, min:0, max:item.quantity, step:1)%> + <%= select_tag "quantity", options_for_select((0..item.quantity).to_a, selected: quantity) %> + <%= submit_tag "Update Quantity"%> <%end%> <%= button_to "Remove Item", cart_path(item_id: item.id, quantity:0), method: :patch %> diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index 9c36af61..9468fc00 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -62,9 +62,9 @@ click_button "Add to Cart" visit cart_path - fill_in "quantity", with: 3 + select 3, from: "quantity" click_button "Update Quantity" - + expect(current_path).to eq(cart_path) expect(page).to have_field('quantity', with:3 ) end @@ -73,7 +73,7 @@ visit item_path(@item_1) click_button "Add to Cart" visit cart_path - fill_in "quantity", with: 0 + select 0, from: "quantity" click_button "Update Quantity" expect(current_path).to eq(cart_path) @@ -96,7 +96,7 @@ visit item_path(@item_2) click_button "Add to Cart" visit cart_path - + within "#cart-item-#{@item_1.id}" do click_button "Remove Item" end @@ -104,6 +104,7 @@ expect(page).to have_content(@item_2.name) expect(page).not_to have_content(@item_1.name) end + end RSpec.describe 'partial for items in cart' ,type: :view do From aee3f97590aa4833e781b65253f40c6178dd7b4a Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 07:17:07 -0600 Subject: [PATCH 10/13] Add test for checking out from cart as visitor/ user --- spec/features/cart/show_cart_spec.rb | 34 +++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index 9468fc00..26e7feb2 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -64,7 +64,7 @@ select 3, from: "quantity" click_button "Update Quantity" - + expect(current_path).to eq(cart_path) expect(page).to have_field('quantity', with:3 ) end @@ -105,6 +105,38 @@ expect(page).not_to have_content(@item_1.name) end + it 'tests for sad path of quantity being entered maliciously greater than qty available or less than 0' + + it 'says you must register / log in if you are browsing as a user' do + visit item_path(@item_1) + click_button "Add to Cart" + visit cart_path + + within '#checkout' do + expect(page).to have_content("You must register or log in to checkout") + expect(page).to have_link("Log In", href: login_path) + expect(page).to have_link("Register", href: register_path) + + expect(page).not_to have_button("Checkout") + end + end + + it 'gives the option to checkout as a logged in user' do + user = create(:user) + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) + visit item_path(@item_1) + click_button "Add to Cart" + visit cart_path + + within '#checkout' do + expect(page).not_to have_content("You must register or log in to checkout") + expect(page).not_to have_link("Log In", href: login_path) + expect(page).not_to have_link("Register", href: register_path) + + expect(page).to have_button("Checkout") + end + end + end RSpec.describe 'partial for items in cart' ,type: :view do From 537197dd83fdaede5772d375dda9c337c80324b4 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 07:22:10 -0600 Subject: [PATCH 11/13] Passing test for checkout options as user/visitor --- app/views/carts/show.html.erb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/views/carts/show.html.erb b/app/views/carts/show.html.erb index fa8d27c0..963a169e 100644 --- a/app/views/carts/show.html.erb +++ b/app/views/carts/show.html.erb @@ -6,4 +6,13 @@ Your cart is empty. <%end%>
<%=@total%>
<%= button_to "Empty Cart", cart_path, method: :delete%> + +
+ <% if current_user %> + <%= button_to "Checkout" %> + <% else %> + You must register or log in to checkout + <%= link_to "Log In", login_path %> <%= link_to "Register", register_path %> + <%end%> +
<%end%> From 7a87b315ac27371d59cf0e3f02fff3e02463df61 Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 08:10:23 -0600 Subject: [PATCH 12/13] Add test for image in cart --- spec/features/cart/show_cart_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index 26e7feb2..341b5d64 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -149,6 +149,7 @@ expect(rendered).to have_selector('div', id:"item-name", text:item.name) expect(rendered).to have_selector('div', id:"item-merchant", text:item.user.name) expect(rendered).to have_selector('div', id:"item-price", text:item.current_price) + expect(rendered).to have_xpath("//img[@src='#{item.image_url}']") expect(rendered).to have_field('quantity', with:quantity) expect(rendered).to have_button("Update Quantity") expect(rendered).to have_button("Remove Item") From 264e369855a51aab84bafc0377186881baa0754c Mon Sep 17 00:00:00 2001 From: wipegup Date: Fri, 5 Apr 2019 08:12:15 -0600 Subject: [PATCH 13/13] Passing test for image in cart_items --- app/views/carts/_cart_item.html.erb | 1 + spec/features/cart/show_cart_spec.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/carts/_cart_item.html.erb b/app/views/carts/_cart_item.html.erb index 5f38b3de..8c891990 100644 --- a/app/views/carts/_cart_item.html.erb +++ b/app/views/carts/_cart_item.html.erb @@ -2,6 +2,7 @@
Item Name: <%=item.name%>
<%=item.user.name%>
<%=item.current_price%>
+ height=20px width=20px/>
<%= form_tag cart_path(item_id: item.id), method: :patch do %> <%= select_tag "quantity", options_for_select((0..item.quantity).to_a, selected: quantity) %> diff --git a/spec/features/cart/show_cart_spec.rb b/spec/features/cart/show_cart_spec.rb index 341b5d64..bbd9f9d4 100644 --- a/spec/features/cart/show_cart_spec.rb +++ b/spec/features/cart/show_cart_spec.rb @@ -127,7 +127,7 @@ visit item_path(@item_1) click_button "Add to Cart" visit cart_path - + save_and_open_page within '#checkout' do expect(page).not_to have_content("You must register or log in to checkout") expect(page).not_to have_link("Log In", href: login_path)