summaryrefslogtreecommitdiff
path: root/spec/controllers/admin/products_controller_spec.rb
blob: 89de9b17c13114fc3b3faa4bb2a2e7a893afd71a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'rails_helper'

module Admin
  describe ProductsController do 
    let(:admin) { create(:admin)}

    before :each do
      http_login(admin)
    end

    describe "#show" do
      let(:product_api) { double(find: true) }

      before :each do
        allow(controller).to receive(:product_api).and_return(product_api)
      end

      it 'loads the product details from amazon' do
        asin = 'asin'
        product = "product"
        allow(product_api).to receive(:find).with(asin).and_return(product)

        get :show, params: { id: asin }

        expect(assigns(:product)).to eql(product)
      end

      context "when the tool has been added to the toolbox" do
        let(:tool) { create(:tool) }

        it "loads the tool" do
          get :show, params: { id: tool.asin }
          expect(assigns(:tool)).to eql(tool)
        end
      end

      context "when the tool is not in the toolbox" do
        it "does not load a tool" do
          get :show, params: { id: "not_added" }
          expect(assigns(:tool)).to be_nil
        end
      end
    end

    describe "#create" do
      it "creates new tool" do
        post :create, params: { name: "pan", asin: "34234" }

        expect(Tool.count).to eql(1)
        expect(Tool.first.name).to eql("pan")
        expect(Tool.first.asin).to eql("34234")
      end

      it "redirects back to the detail page" do
        post :create, params: { name: 'blah', asin: 'blah' }
        expect(response).to redirect_to(admin_product_path('blah'))
      end
    end
  end
end