require 'spec_helper' describe List do let(:supplier) { create :supplier } let(:supplier_password){'secret1'} let(:supplier) { create :supplier, email: 'supplier@qwaiter.com', password: supplier_password, confirmation_token: 'abc', confirmed_at: Time.now.utc, open: true } let(:user) { create :user } let(:section) { create :section, supplier: supplier} let(:table) { create :table, supplier: supplier} let(:list){ create :list, supplier: supplier, table: table, section: section, user_ids: [user.id] } 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 :as_json do it 'should include _id in as_json serialization' do list.as_json.keys.map(&:to_sym).should include :_id end it 'should include table_number in as_json serialization' do list.as_json.keys.should include :table_number end end describe :mark_as_paid do 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 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 it "changes the section" do expect{ list.move_to_table! new_table }.to change{ list.section_id }.to( new_section.id ) end it "changes the section of the orders" do expect{ list.move_to_table! new_table }.to change{ @order1.reload; @order1.section_id }.to( new_section.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 it 'returns an order object' do list.place_order(products: {product.id => 7}, user: user).should be_a Order end it 'creates an order' do expect{ list.place_order(products: {product.id => 7}, user: user) }.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 list.place_order(products: {product.id => 7}, user: user) expect{ list.place_order(products: {product.id => 3}, user: user) }.to broadcast_to_user(user.id).message('orders_placed_count').with(count: 2) expect{ list.place_order(products: {product.id => 5}, user: user) }.to broadcast_to_supplier(supplier.id).message('orders_placed_count').with(count: 3) end end it 'sets the list price as kind of caching' do list.place_order(products: {product.id => 7}, user: user) list.reload list.price.should == 15.54 end describe 'product order creation' end describe 'join requests' do describe '#send_table_join_request_for_user' do it "does not add an existing user to join_request_user_ids" do list.send_table_join_request_for_user! user expect(list.join_request_user_ids).not_to include user.id end it "adds a user to join_request_user_ids" do other_user = create :user list.send_table_join_request_for_user! other_user # test through persistance list.reload expect(list.join_request_user_ids).to eq [other_user.id] end it "does not add a user multiple times" do other_user = create :user 2.times { list.send_table_join_request_for_user! other_user } expect(list.join_request_user_ids).to eq [other_user.id] end it "broadcasts it to the user" do other_user = create :user expect{ list.send_table_join_request_for_user! other_user }.to broadcast_to_user(user.id).message('user_join_request').with( hash_including(:users, :join_request) ) end end end end