require 'spec_helper'

describe TextMessage do

  before do
    TextMessage.delete_all
    User.delete_all
    @target_user = FactoryGirl.create(:user)
    @source_user = FactoryGirl.create(:user)

    @msg = TextMessage.new(:target_user_id => @target_user.id, :source_user_id => @source_user.id)
  end

  describe "index" do

    it "should retrieve conversation for both users" do
      @msg.message = "Test message"
      @msg.save!

      messages = TextMessage.index(@target_user.id, @source_user.id)
      messages.count.should == 1

      messages = TextMessage.index(@source_user.id, @target_user.id)
      messages.count.should == 1
    end

    it "should page records" do
      11.times do |n|
        message = TextMessage.new(:target_user_id => @target_user.id, :source_user_id => @source_user.id)
        message.message = "Message #{n}"
        message.save!
      end

      messages = TextMessage.index(@target_user.id, @source_user.id, {:offset => 0})
      messages.count.should == 10

      messages = TextMessage.index(@target_user.id, @source_user.id, {:offset => 10})
      messages.count.should == 1
    end

    it "should not allow empty message" do
      expect { @msg.save! }.to raise_error(ActiveRecord::RecordInvalid)
    end

  end
end