summaryrefslogtreecommitdiff
path: root/spec/models/customer_relations/contact_state_counts_spec.rb
blob: a19f6f0848951b9aad333d4c6221ada1d7f9b1d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CustomerRelations::ContactStateCounts do
  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group, :crm_enabled) }

  let(:counter) { described_class.new(user, group, params) }
  let(:params) { {} }

  before_all do
    group.add_reporter(user)
    create(:contact, group: group, first_name: 'filter')
    create(:contact, group: group, last_name: 'filter')
    create(:contact, group: group)
    create(:contact, group: group, state: 'inactive', email: 'filter@example.com')
    create(:contact, group: group, state: 'inactive')
  end

  describe '.declarative_policy_class' do
    subject { described_class.declarative_policy_class }

    it { is_expected.to eq('CustomerRelations::ContactPolicy') }
  end

  describe '#all' do
    it 'returns the total number of contacts' do
      expect(counter.all).to be(5)
    end
  end

  describe '#active' do
    it 'returns the number of active contacts' do
      expect(counter.active).to be(3)
    end
  end

  describe '#inactive' do
    it 'returns the number of inactive contacts' do
      expect(counter.inactive).to be(2)
    end
  end

  describe 'when filtered' do
    let(:params) { { search: 'filter' } }

    it '#all returns the number of contacts with a filter' do
      expect(counter.all).to be(3)
    end

    it '#active returns the number of active contacts with a filter' do
      expect(counter.active).to be(2)
    end

    it '#inactive returns the number of inactive contacts with a filter' do
      expect(counter.inactive).to be(1)
    end
  end
end