foodsoft/spec/integration/receive_spec.rb

97 lines
3.2 KiB
Ruby
Raw Normal View History

2014-01-08 11:52:39 +01:00
require_relative '../spec_helper'
describe 'receiving an order', :type => :feature do
let(:admin) { create :user, groups:[create(:workgroup, role_orders: true)] }
let(:supplier) { create :supplier }
let(:article) { create :article, supplier: supplier, unit_quantity: 3 }
let(:order) { create :order, supplier: supplier, article_ids: [article.id] } # need to ref article
let(:go1) { create :group_order, order: order }
let(:go2) { create :group_order, order: order }
let(:oa) { order.order_articles.find_by_article_id(article.id) }
let(:goa1) { create :group_order_article, group_order: go1, order_article: oa }
let(:goa2) { create :group_order_article, group_order: go2, order_article: oa }
# set quantities of group_order_articles
def set_quantities(q1, q2)
goa1.update_quantities(*q1)
goa2.update_quantities(*q2)
oa.update_results!
order.finish!(admin)
reload_articles
end
# reload all group_order_articles
def reload_articles
[goa1, goa2].map(&:reload)
oa.reload
end
describe :type => :feature, :js => true do
before { login admin }
it 'has product ordered visible' do
set_quantities [3,0], [0,0]
visit receive_order_path(order)
expect(page).to have_content(article.name)
expect(page).to have_selector("#order_article_#{oa.id}")
end
it 'has product not ordered invisible' do
set_quantities [0,0], [0,0]
visit receive_order_path(order)
expect(page).to_not have_selector("#order_article_#{oa.id}")
end
it 'is not received by default' do
set_quantities [3,0], [0,0]
visit receive_order_path(order)
expect(find("#order_articles_#{oa.id}_units_received").value).to eq ''
end
it 'does not change anything when received is ordered' do
set_quantities [2,0], [3,2]
visit receive_order_path(order)
expect {
fill_in "order_articles_#{oa.id}_units_received", :with => oa.units_to_order
find('input[type="submit"]').click
expect(page).to have_selector('body')
reload_articles
}.to_not change{[oa.units, goa1.result, goa2.result]}
end
it 'redistributes properly when received is more' do
set_quantities [2,0], [3,2]
visit receive_order_path(order)
fill_in "order_articles_#{oa.id}_units_received", :with => 3
find('input[type="submit"]').click
expect(page).to have_selector('body')
reload_articles
expect(oa.units).to eq 3
expect(goa1.result).to be_within(1e-3).of 2
expect(goa2.result).to be_within(1e-3).of 5
end
it 'redistributes properly when received is less' do
set_quantities [2,0], [3,2]
visit receive_order_path(order)
fill_in "order_articles_#{oa.id}_units_received", :with => 1
find('input[type="submit"]').click
expect(page).to have_selector('body')
reload_articles
expect(oa.units).to eq 1
expect(goa1.result).to be_within(1e-3).of 2
expect(goa2.result).to be_within(1e-3).of 1
end
it 'has a locked field when edited elsewhere' do
set_quantities [2,0], [3,2]
goa1.result = goa1.result + 1
goa1.save!
visit receive_order_path(order)
expect(find("#order_articles_#{oa.id}_units_received")).to be_disabled
end
end
end