Files
mozo-backend/spec/models/list_spec.rb
T
2025-08-28 09:07:47 -05:00

238 lines
8.7 KiB
Ruby

require 'spec_helper'
describe List do
let(:employee_password){'secret1'}
let(:employee) { create :employee, email: 'supplier@mozo.bar', password: employee_password, confirmation_token: 'abc', confirmed_at: Time.now.utc}
let(:supplier) { create :supplier, open: true }
let(:user) { create :user }
let(:section) { create :section, supplier: supplier}
let(:table) { create :table, supplier: supplier}
let(:list_options){ {supplier: supplier, table: table, section: section, user_ids: [user.id]} }
let(:list){ create :list, list_options}
let(:product){ create :product, price: 2.22, supplier: supplier }
let(:order) { create :order, user: user, list: list, supplier: supplier, section: section }
let(:product_order ){ create :product_order, order: order, product: product, quantity: 3, price: 2.11 }
subject { list }
describe :mark_as_paid do
before do
expect_broadcast channel: "/user/#{list.users.first.id}", data: {data: {id: list.id}, event: 'list_is_paid'}
expect_broadcast channel: "/supplier/#{list.supplier.id}", data: {data: {id: list.id}, event: 'list_is_paid'}
end
it "should set paid_at to a time" do
list.paid_at.should be_nil
list.is_paid!
list.paid_at.should be_kind_of Time
end
it "should set is_paid to true" do
list.is_paid.should be false
list.is_paid!
list.is_paid.should be true
end
end
describe '#set_price' do
it 'takes the product_order price in stead of the product price' do
product_order and list.set_price.should == 6.33
end
it 'does not include the price of cancelled orders' do
product_order
cancelled = create :order, :cancelled, user: user, list: list, supplier: supplier, section: section
create :product_order, order: cancelled, product: product, quantity: 2, price: 7.99
list.set_price
list.price.should == 6.33
end
end
describe '#move_to_table!' do
let(:new_section) { create :section, supplier: supplier}
let(:new_table) { create :table, supplier: supplier, section: new_section}
before do
@order1 = create :order, supplier: supplier, list: list, section: section
expect_broadcast channel: "/user/#{list.users.first.id}", data: {data: {
from_table_id: table.id,
list_id: list.id,
to_table_id: new_table.id
},
event: 'list_changed_table'
}
expect_broadcast channel: "/supplier/#{list.supplier.id}", data: {data: {
from_table_id: table.id,
list_id: list.id,
to_table_id: new_table.id,
payload: anything # out of scope
},
event: 'list_changed_table'
}
end
it 'adds a model to keep track of the movement' do
expect{ list.move_to_table! new_table }.to change{ UserTableMove.count }.by(1)
end
it "changes the table" do
expect{ list.move_to_table! new_table }.to change{ list.table_id }.to( new_table.id )
end
end
describe '#has_active_orders?' do
its(:has_active_orders?) { should be false }
it 'returns true when there are placed orders for the list' do
create :order, supplier: supplier, list: list, section: section, state: 'placed'
list.has_active_orders?.should be true
end
it 'returns true when there are active orders for the list' do
create :order, supplier: supplier, list: list, section: section, state: 'active'
list.has_active_orders?.should be true
end
it 'returns false when there are only delivered orders' do
create :order, supplier: supplier, list: list, section: section, state: 'delivered'
list.has_active_orders?.should be false
end
it 'returns false when there are only closed orders' do
create :order, supplier: supplier, list: list, section: section, state: 'closed'
list.has_active_orders?.should be false
end
it 'returns false when there are only cancelled orders' do
create :order, supplier: supplier, list: list, section: section, state: 'cancelled'
list.has_active_orders?.should be false
end
end
describe '#active_orders' do
its(:active_orders) { should be_empty }
it 'returns placed orders' do
order = create :order, supplier: supplier, list: list, section: section, state: 'placed'
list.active_orders.should eq [order]
end
it 'returns active orders' do
order = create :order, supplier: supplier, list: list, section: section, state: 'active'
list.active_orders.should eq [order]
end
it 'does not return delivered orders' do
order = create :order, supplier: supplier, list: list, section: section, state: 'delivered'
list.active_orders.should be_empty
end
it 'does not return closed orders' do
order = create :order, supplier: supplier, list: list, section: section, state: 'closed'
list.active_orders.should be_empty
end
it 'does not return cancelled orders' do
order = create :order, supplier: supplier, list: list, section: section, state: 'cancelled'
list.active_orders.should be_empty
end
end
describe '#place_order' do
subject{ list.place_order **order_options }
describe "first order" do
let(:order_options){ {user: user, first_order: true} }
before do
expect_broadcast channel: "/supplier/#{list.supplier.id}", data: {data: anything, event: 'list_update'}
expect_broadcast channel: "/supplier/#{list.supplier.id}", data: {data: {
list_id: list.id,
payload: anything,
supplier_orders_placed_count: 1
},
event: 'new_list'
}
end
it 'returns an order object' do
order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}]
subject.should be_a Order
end
it 'creates an order' do
order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}]
expect{ subject }.to change{ Order.count }.by(1)
end
# describe 'broadcasting' do
# it 'broadcasts to the user and the supplier the active order counter' do
# # create existing order to test with counts higher than 2
# list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user, first_order: true)
#
# # expect{
# # list.place_order(product_orders: [{product_id: product.id, quantity: 5}], user: user)
# # }.to broadcast_to_user(user.id).message('orders_placed_count').with(count: 2)
#
# order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}]
# expect{ subject }.to broadcast_to_supplier(supplier.id).message('new_list').with hash_including(supplier_orders_placed_count: 2)
# end
# end
it 'sets the list price as kind of caching' do
product.price.should eq 2.22
list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 7}], user: user)
list.reload
list.price.should == 15.54
end
describe 'product order creation'
end
describe "followup order" do
let(:order_options){ {user: user, first_order: false} }
describe 'broadcasting' do
it 'broadcasts to the user and the supplier the active order counter' do
order_options[:product_orders] = [{'product_id' => product.id, 'quantity' => 7}]
expect{ subject }.to broadcast_to_supplier(supplier.id).message('new_order').with hash_including(supplier_orders_placed_count: 1)
end
end
end
end
describe '#close!' do
it 'removes the helped mark' do
list_options[:needs_help] = true
expect_broadcast channel: "/user/#{list.users.first.id}", data: {data: {id: list.id}, event: 'list_helped'}
expect_broadcast channel: "/user/#{list.users.first.id}", data: {data: {
id: list.id,
supplier_orders_in_process_count: 0,
supplier_orders_placed_count: 0
},
event: 'list_closed'
}
expect_broadcast channel: "/supplier/#{list.supplier.id}", data: {data: {
id: list.id,
},
event: 'list_helped'
}
expect_broadcast channel: "/supplier/#{list.supplier.id}", data: {data: {
id: list.id,
supplier_orders_in_process_count: 0,
supplier_orders_placed_count: 0
},
event: 'list_closed'
}
list.close!
expect( list.needs_help? ).not_to be true
end
end
context 'class_methods' do
describe 'active_for_supplier' do
it 'only returns the lists for the supplier' do
other_supplier = create :supplier, open: true
other_list = create :list, :active, supplier: other_supplier
described_class.active_for_supplier( list.supplier ).should eq [list]
end
end
end
end