email_address_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require 'rails_helper'
  2. RSpec.describe EmailAddress, type: :model do
  3. subject(:email_address) { create(:email_address) }
  4. describe 'Attributes:' do
  5. describe '#active' do
  6. subject(:email_address) do
  7. create(:email_address, channel: channel, active: active)
  8. end
  9. context 'without a Channel association' do
  10. let(:channel) { nil }
  11. let(:active) { true }
  12. it 'always returns false' do
  13. expect(email_address.active).not_to eq(active)
  14. end
  15. end
  16. context 'with a Channel association' do
  17. let(:channel) { create(:email_channel) }
  18. let(:active) { true }
  19. it 'returns the value it was set to' do
  20. expect(email_address.active).to eq(active)
  21. end
  22. end
  23. end
  24. end
  25. describe 'Associations:' do
  26. describe '#groups' do
  27. let(:group) { create(:group, email_address: email_address) }
  28. context 'when an EmailAddress is destroyed' do
  29. it 'removes the #email_address_id from all associated Groups' do
  30. expect { email_address.destroy }
  31. .to change { group.reload.email_address_id }.to(nil)
  32. end
  33. end
  34. end
  35. describe '#channel' do
  36. subject(:email_addresses) { create_list(:email_address, 2, channel: channel) }
  37. let(:channel) { create(:channel) }
  38. context 'when a Channel is destroyed' do
  39. it 'removes the #channel_id from all its associated EmailAddresses' do
  40. expect { channel.destroy }
  41. .to change { email_addresses.map(&:reload).map(&:channel_id) }
  42. .to([nil, nil])
  43. end
  44. context 'and then an identical Channel is created' do
  45. it 'removes the #channel_id from all its associated EmailAddresses' do
  46. channel.destroy
  47. expect { create(:channel) }
  48. .not_to change { email_addresses.map(&:reload).map(&:channel_id) }
  49. end
  50. end
  51. end
  52. end
  53. end
  54. end