master
1require 'rails_helper'
2
3module Admin
4 describe ProductsController do
5 let(:admin) { create(:admin)}
6
7 before :each do
8 http_login(admin)
9 end
10
11 describe "#show" do
12 let(:product_api) { double(find: true) }
13
14 before :each do
15 allow(controller).to receive(:product_api).and_return(product_api)
16 end
17
18 it 'loads the product details from amazon' do
19 asin = 'asin'
20 product = "product"
21 allow(product_api).to receive(:find).with(asin).and_return(product)
22
23 get :show, params: { id: asin }
24
25 expect(assigns(:product)).to eql(product)
26 end
27
28 context "when the tool has been added to the toolbox" do
29 let(:tool) { create(:tool) }
30
31 it "loads the tool" do
32 get :show, params: { id: tool.asin }
33 expect(assigns(:tool)).to eql(tool)
34 end
35 end
36
37 context "when the tool is not in the toolbox" do
38 it "does not load a tool" do
39 get :show, params: { id: "not_added" }
40 expect(assigns(:tool)).to be_nil
41 end
42 end
43 end
44
45 describe "#create" do
46 it "creates new tool" do
47 post :create, params: { name: "pan", asin: "34234" }
48
49 expect(Tool.count).to eql(1)
50 expect(Tool.first.name).to eql("pan")
51 expect(Tool.first.asin).to eql("34234")
52 end
53
54 it "redirects back to the detail page" do
55 post :create, params: { name: 'blah', asin: 'blah' }
56 expect(response).to redirect_to(admin_product_path('blah'))
57 end
58 end
59 end
60end