agent_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. require 'rails_helper'
  2. RSpec.describe Chat::Agent, type: :model do
  3. describe '.state' do
  4. let(:user) { create(:agent) }
  5. context 'when no record exists for User' do
  6. it 'returns false' do
  7. expect(described_class.state(1337)).to be(false)
  8. end
  9. end
  10. context 'when active flag is set to true' do
  11. before do
  12. create(:'chat/agent', active: true, updated_by: user)
  13. end
  14. it 'returns true' do
  15. expect(described_class.state(user.id)).to be(true)
  16. end
  17. end
  18. context 'when active flag is set to false' do
  19. before do
  20. create(:'chat/agent', active: false, updated_by: user)
  21. end
  22. it 'returns false' do
  23. expect(described_class.state(user.id)).to be(false)
  24. end
  25. end
  26. context 'when setting state for not existing record' do
  27. it 'creates a record' do
  28. expect { described_class.state(user.id, true) }.to change { described_class.exists?(updated_by: user) }.from(false).to(true)
  29. end
  30. end
  31. context 'when setting same state for record' do
  32. let(:record) { create(:'chat/agent', active: true, updated_by: user) }
  33. before do
  34. # avoid race condition with same updated_at time
  35. record
  36. travel_to 5.minutes.from_now
  37. end
  38. it 'updates updated_at timestamp' do
  39. expect { described_class.state(record.updated_by_id, record.active) }.to change { record.reload.updated_at }
  40. end
  41. it 'returns false' do
  42. expect(described_class.state(record.updated_by_id, record.active)).to eq(false)
  43. end
  44. end
  45. context 'when setting different state for record' do
  46. let(:record) { create(:'chat/agent', active: true, updated_by: user) }
  47. before do
  48. # avoid race condition with same updated_at time
  49. record
  50. travel_to 5.minutes.from_now
  51. end
  52. it 'updates updated_at timestamp' do
  53. expect { described_class.state(record.updated_by_id, !record.active) }.to change { record.reload.updated_at }
  54. end
  55. it 'returns true' do
  56. expect(described_class.state(record.updated_by_id, !record.active)).to eq(true)
  57. end
  58. end
  59. end
  60. end