From bfa4121f410c0a46a96013b1aab5f08542b3e16a Mon Sep 17 00:00:00 2001 From: Lauren Date: Wed, 30 Oct 2019 01:19:23 -0400 Subject: [PATCH] Feature/add inbox specs (#192) * Add base Inbox association and validation specs * `#facebook?`` * `#add_member` and `#remove_member` * cleanup * Rubocop * Rubocop but again really this time --- app/models/inbox.rb | 2 ++ spec/models/inbox_spec.rb | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 spec/models/inbox_spec.rb diff --git a/app/models/inbox.rb b/app/models/inbox.rb index cdb12e3c3..13ffb2cb4 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -4,6 +4,8 @@ class Inbox < ApplicationRecord validates :account_id, presence: true belongs_to :account + + # TODO: should add associations for the channel types belongs_to :channel, polymorphic: true, dependent: :destroy has_many :contact_inboxes, dependent: :destroy diff --git a/spec/models/inbox_spec.rb b/spec/models/inbox_spec.rb new file mode 100644 index 000000000..7773a73d3 --- /dev/null +++ b/spec/models/inbox_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Inbox do + describe 'validations' do + it { is_expected.to validate_presence_of(:account_id) } + end + + describe 'associations' do + it { is_expected.to belong_to(:account) } + + it { is_expected.to belong_to(:channel) } + + it { is_expected.to have_many(:contact_inboxes).dependent(:destroy) } + + it { is_expected.to have_many(:contacts).through(:contact_inboxes) } + + it { is_expected.to have_many(:inbox_members).dependent(:destroy) } + + it { is_expected.to have_many(:members).through(:inbox_members).source(:user) } + + it { is_expected.to have_many(:conversations).dependent(:destroy) } + + it { is_expected.to have_many(:messages).through(:conversations) } + end + + describe '#add_member' do + let(:inbox) { FactoryBot.create(:inbox) } + let(:user) { FactoryBot.create(:user) } + + it do + expect(inbox.inbox_members.size).to eq(0) + + inbox.add_member(user.id) + expect(inbox.reload.inbox_members.size).to eq(1) + end + end + + describe '#remove_member' do + let(:inbox) { FactoryBot.create(:inbox) } + let(:user) { FactoryBot.create(:user) } + + before { inbox.add_member(user.id) } + + it do + expect(inbox.inbox_members.size).to eq(1) + + inbox.remove_member(user.id) + expect(inbox.reload.inbox_members.size).to eq(0) + end + end + + describe '#facebook?' do + let(:inbox) do + FactoryBot.build(:inbox, channel: channel_val) + end + + context 'when the channel type is Channel::FacebookPage' do + let(:channel_val) { Channel::FacebookPage.new } + + it do + expect(inbox.facebook?).to eq(true) + end + end + + context 'when the channel type is not Channel::FacebookPage' do + let(:channel_val) { Channel::WebWidget.new } + + it { expect(inbox.facebook?).to eq(false) } + end + end +end