Commit eda24c21 by vulehuan

rspec: card - update quantity

parent 89d66f64
......@@ -28,11 +28,13 @@ class CardsController < ApplicationController
elsif !params[:card_action].nil? && params[:card_action] == 'update'
# Nothing to update
if @card_infos.empty?
flash[:error] = "Invalid request"
redirect_to cards_path
else
quantity = params[:quantity]
# Invalid request
if quantity.nil? || quantity.to_i <= 0
flash[:error] = "Invalid quantity"
redirect_to cards_path
else
card_items = @card_infos[:card_items]
......
......@@ -29,12 +29,12 @@
<td><%= $i %></td>
<td><%= link_to(product.name, product_url(product)) %></td>
<% if can_edit %>
<td><input type="text" class="form-control txt-quantity" value="<%= card_item[:quantity] %>" /></td>
<td><input id="cart-quantity-<%= card_item[:product_id] %>" type="text" class="form-control txt-quantity" value="<%= card_item[:quantity] %>" /></td>
<% else %>
<td><div class="text-right"><%= card_item[:quantity] %></div></td>
<% end %>
<td><div class="text-right"><%= number_with_delimiter(product.price) %></div></td>
<td><div class="text-right"><%= number_with_delimiter(product.price * card_item[:quantity]) %></div></td>
<td><div class="text-right cart-unit-price-<%= card_item[:product_id] %>"><%= number_with_delimiter(product.price) %></div></td>
<td><div class="text-right cart-total-price-<%= card_item[:product_id] %>"><%= number_with_delimiter(product.price * card_item[:quantity]) %></div></td>
<% if can_edit %>
<td>
<div class="text-center">
......
......@@ -3,6 +3,7 @@
<h2 class="sprite-2">
Cards<span class="sprite-2"></span>
</h2>
<div id="block-message-visible" class="hidden"></div>
<%= render partial: 'cards/item_list', locals: { card_infos: @card_infos, can_edit: true } %>
<div class="clearfix"></div>
</div>
require 'spec_helper'
describe "Cards Page" do
describe "CardPages" do
subject { page }
describe "step 1" do
before(:all) {
@product_category = FactoryGirl.create(:product_category)
@user = FactoryGirl.create(:user)
5.times { FactoryGirl.create(:product, product_category_id: @product_category.id, user_id: @user.id) }
Product.all.each do |product|
visit product_path(product)
page.find('.product-detail').click_link('Order')
end
}
after(:all) do
Product.delete_all
User.delete_all
ProductCategory.delete_all
end
describe "when update quantity" do
describe "with invalid information" do
describe "when quantity is not a number" do
end
describe "when product not exist" do
before do
visit cards_path(card_action: 'update', product_id: 0, quantity: "abc")
end
describe "when quantity < 0" do
it { should have_selector('.alert.alert-error', text: 'Invalid') }
end
describe "when quantity = 0" do
describe "when product exist" do
describe "when quantity is not a number" do
before do
product = Product.first
visit cards_path(card_action: 'update', product_id: product.id , quantity: "abc")
end
it { should have_selector('.alert.alert-error', text: 'Invalid') }
end
describe "when quantity < 0" do
before do
product = Product.first
visit cards_path(card_action: 'update', product_id: product.id , quantity: -1)
end
it { should have_selector('.alert.alert-error', text: 'Invalid') }
end
describe "when quantity = 0" do
before do
product = Product.first
visit cards_path(card_action: 'update', product_id: product.id , quantity: 0)
end
it { should have_selector('.alert.alert-error', text: 'Invalid') }
end
end
end
describe "with valid information" do
it "with valid information" do
amount = 99
product = Product.first
visit cards_path(card_action: 'update', product_id: product.id , quantity: amount)
page.should have_xpath("//input[@id='cart-quantity-#{product.id}'][@value='#{amount}']")
page.should have_selector(".cart-unit-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price))
page.should have_selector(".cart-total-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price * amount))
end
end
......@@ -23,6 +75,8 @@ describe "Cards Page" do
end
describe "when product exist in card" do
describe "calculate price" do
end
end
end
......@@ -31,9 +85,6 @@ describe "Cards Page" do
describe "when click product name" do
end
describe "calculate price" do
end
end
describe "step 2" do
......@@ -90,5 +141,8 @@ describe "Cards Page" do
end
describe "step 4" do
before { visit url_for(controller: 'cards', action: 'thankyou') }
it { should have_title('Thank you') }
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