Add some basic test coverage to exchange_products_controller and exchange_products_renderer

This commit is contained in:
luisramos0
2019-11-24 19:17:33 +00:00
parent 7e3b6e2b5d
commit 3264355f12
2 changed files with 58 additions and 14 deletions

View File

@@ -4,31 +4,46 @@ module Api
describe ExchangeProductsController, type: :controller do
include AuthenticationWorkflow
let!(:order_cycle) { create(:order_cycle) }
let!(:coordinator) { order_cycle.coordinator }
let(:order_cycle) { create(:order_cycle) }
let(:exchange) { order_cycle.exchanges.incoming.first }
let(:coordinator) { order_cycle.coordinator }
let!(:renderer) { ExchangeProductsRenderer.new(order_cycle, coordinator.owner) }
before do
allow(controller).to receive_messages spree_current_user: coordinator.owner
allow(ExchangeProductsRenderer).to receive(:new) { renderer }
allow(renderer).
to receive(:exchange_products).
with(exchange.incoming, exchange.sender).
and_return(products_relation)
end
describe "#index" do
describe "for incoming exchanges" do
it "loads data" do
exchange = order_cycle.exchanges.incoming.first
spree_get :index, exchange_id: exchange.id
describe "when the product list is empty" do
let(:products_relation) { Spree::Product.where("1=0") }
expect(json_response["products"].first["supplier_name"]).to eq exchange.variants.first.product.supplier.name
it "handles it gracefully" do
spree_get :index, exchange_id: exchange.id
expect(json_response["products"].length).to eq 0
end
end
describe "for outgoing exchanges" do
it "loads data" do
exchange = order_cycle.exchanges.outgoing.first
spree_get :index, exchange_id: exchange.id
describe "when a product is returned" do
let(:products_relation) { Spree::Product.where(id: exchange.variants.first.product.id) }
suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
expect(suppliers).to include json_response["products"].first["supplier_name"]
expect(suppliers).to include json_response["products"].second["supplier_name"]
describe "when an exchange id param is provided" do
it "uses exchange order_cycle, incoming and enterprise to fetch products" do
spree_get :index, exchange_id: exchange.id, order_cycle_id: 666, enterprise_id: 666, incoming: false
expect(json_response["products"].first["supplier_name"]).to eq exchange.variants.first.product.supplier.name
end
end
describe "when an exchange id param is not provided" do
it "uses params order_cycle, incoming and enterprise to fetch products" do
spree_get :index, order_cycle_id: order_cycle.id, enterprise_id: exchange.sender_id, incoming: true
expect(json_response["products"].first["supplier_name"]).to eq exchange.variants.first.product.supplier.name
end
end
end
end

View File

@@ -0,0 +1,29 @@
require "spec_helper"
describe ExchangeProductsRenderer do
let(:order_cycle) { create(:order_cycle) }
let(:coordinator) { order_cycle.coordinator }
let(:renderer) { described_class.new(order_cycle, coordinator.owner) }
describe "#exchange_products" do
describe "for an incoming exchange" do
it "loads products" do
exchange = order_cycle.exchanges.incoming.first
products = renderer.exchange_products(true, exchange.sender)
expect(products.first.supplier.name).to eq exchange.variants.first.product.supplier.name
end
end
describe "for an outgoing exchange" do
it "loads products" do
exchange = order_cycle.exchanges.outgoing.first
products = renderer.exchange_products(false, exchange.receiver)
suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
expect(suppliers).to include products.first.supplier.name
expect(suppliers).to include products.second.supplier.name
end
end
end
end