Sha256: 35cca81c0f0db99b2a8ca798714dbf54bda778b6b0a61635288c8a18ef3f2f88

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

# coding: utf-8
require 'spec_helper'

describe PowerShop::CartController, :type => :controller do
  let(:cart) { create :shopping_cart }
  let(:product) { create :product, price: 500 }

  before { controller.stub(:cart).and_return(cart) }

  describe '#POST add_product' do
    before { request.env["HTTP_REFERER"] = "/" }

    context 'when request is not xhr' do
      before { post :add_product, product_id: product.id, use_route: 'power_shop' }

      it { expect(response).to redirect_to '/' }
      it { expect(cart.shopping_cart_items.first.price).to eq 500 }
      it { expect(cart.shopping_cart_items.first.quantity).to eq 1 }
      it { expect(cart.shopping_cart_items.size).to eq 1 }
    end

    context 'when request is xhr' do
      before { xhr :post, :add_product, product_id: product.id, use_route: 'power_shop' }

      subject(:expected_json) { JSON.parse(response.body) }

      it { expect(response).to be_success }
      it { expect(expected_json['total_items']).to eq 1 }
      it { expect(expected_json['subtotal']).to eq 500 }
    end

    context 'when exists quantity param' do
      before { post :add_product, product_id: product.id, quantity: 2, use_route: 'power_shop' }

      it { expect(cart.shopping_cart_items.first.quantity).to eq 2 }
    end
  end

  describe '#DELETE delete_product' do
    before do
      request.env["HTTP_REFERER"] = "/cart"
      cart.add(product, product.price, 2)
      delete :delete_product, product_id: product.id, use_route: 'power_shop'
    end

    it { expect(cart.reload).to be_empty }
    it { expect(response).to redirect_to '/cart' }
  end

  describe '#GET show' do
    before { get :show, use_route: 'power_shop' }

    it { expect(response).to be_success }
    it {expect(assigns(:cart)).to eq cart }
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
power_shop-0.2.3 spec/controllers/cart_controller_spec.rb
power_shop-0.2.2 spec/controllers/cart_controller_spec.rb
power_shop-0.2.1 spec/controllers/cart_controller_spec.rb
power_shop-0.2.0 spec/controllers/cart_controller_spec.rb
power_shop-0.1.1 spec/controllers/cart_controller_spec.rb
power_shop-0.1.0 spec/controllers/cart_controller_spec.rb