email_address_spec.rb 2.0 KB

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