Commit cad828e0 by vulehuan

rspec: product - add product to card

parent d17f3bf9
......@@ -7,6 +7,7 @@ class CardsController < ApplicationController
@card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end
if !params[:product_id].nil?
if Product.exists?(params[:product_id])
# if remove a product from card
if !params[:card_action].nil? && params[:card_action] == 'remove'
# Nothing to delete
......@@ -51,14 +52,12 @@ class CardsController < ApplicationController
# if add a product to card
# first time add to card
if @card_infos.empty?
if Product.exists?(params[:product_id])
product = Product.find(params[:product_id])
card_items = Array.new
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = Hash.new
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end
redirect_to cards_path
else
card_items = @card_infos[:card_items]
......@@ -80,6 +79,10 @@ class CardsController < ApplicationController
redirect_to cards_path
end
end
else
flash[:error] = "Invalid product"
redirect_to products_path
end
end
end
......
......@@ -101,4 +101,37 @@ describe "Products Page" do
end
end
end
describe "when add product to card" do
describe "when product is invalid" do
describe "when product id is not a number" do
before { visit cards_path(product_id: "abc") }
it { should have_selector('.alert.alert-error', text: 'Invalid') }
end
describe "when product id is not exist" do
before { visit cards_path(product_id: 0) }
it { should have_selector('.alert.alert-error', text: 'Invalid') }
end
end
describe "when product is valid" do
before do
@product_category = FactoryGirl.create(:product_category)
@user = FactoryGirl.create(:user)
@product = FactoryGirl.create(:product, product_category_id: @product_category.id, user_id: @user.id)
visit product_path(@product)
page.find('.product-detail').click_link('Order')
end
it { should have_selector('table a', text: @product.name) }
it { should have_selector('.btn-update-card-quantity') }
it { should have_selector('a.btn', text: 'Remove') }
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment