Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/customer_relations')
-rw-r--r--spec/models/customer_relations/contact_spec.rb53
-rw-r--r--spec/models/customer_relations/contact_state_counts_spec.rb60
2 files changed, 108 insertions, 5 deletions
diff --git a/spec/models/customer_relations/contact_spec.rb b/spec/models/customer_relations/contact_spec.rb
index f91546f5240..487af404a7c 100644
--- a/spec/models/customer_relations/contact_spec.rb
+++ b/spec/models/customer_relations/contact_spec.rb
@@ -226,15 +226,58 @@ RSpec.describe CustomerRelations::Contact, type: :model do
end
end
- describe '.sort_by_name' do
+ describe '.counts_by_state' do
+ before do
+ create_list(:contact, 3, group: group)
+ create_list(:contact, 2, group: group, state: 'inactive')
+ end
+
+ it 'returns correct contact counts' do
+ counts = group.contacts.counts_by_state
+
+ expect(counts['active']).to be(3)
+ expect(counts['inactive']).to be(2)
+ end
+ end
+
+ describe 'sorting' do
+ let_it_be(:organization_a) { create(:organization, name: 'a') }
+ let_it_be(:organization_b) { create(:organization, name: 'b') }
let_it_be(:contact_a) { create(:contact, group: group, first_name: "c", last_name: "d") }
- let_it_be(:contact_b) { create(:contact, group: group, first_name: "a", last_name: "b") }
- let_it_be(:contact_c) { create(:contact, group: group, first_name: "e", last_name: "d") }
+ let_it_be(:contact_b) do
+ create(:contact,
+ group: group,
+ first_name: "a",
+ last_name: "b",
+ phone: "123",
+ organization: organization_a)
+ end
- context 'when sorting the contacts' do
- it 'sorts them by last name then first name in ascendent order' do
+ let_it_be(:contact_c) do
+ create(:contact,
+ group: group,
+ first_name: "e",
+ last_name: "d",
+ phone: "456",
+ organization: organization_b)
+ end
+
+ describe '.sort_by_name' do
+ it 'sorts them by last name then first name in ascending order' do
expect(group.contacts.sort_by_name).to eq([contact_b, contact_a, contact_c])
end
end
+
+ describe '.sort_by_organization' do
+ it 'sorts them by organization in descending order' do
+ expect(group.contacts.sort_by_organization(:desc)).to eq([contact_c, contact_b, contact_a])
+ end
+ end
+
+ describe '.sort_by_field' do
+ it 'sorts them by phone in ascending order' do
+ expect(group.contacts.sort_by_field('phone', :asc)).to eq([contact_b, contact_c, contact_a])
+ end
+ end
end
end
diff --git a/spec/models/customer_relations/contact_state_counts_spec.rb b/spec/models/customer_relations/contact_state_counts_spec.rb
new file mode 100644
index 00000000000..a19f6f08489
--- /dev/null
+++ b/spec/models/customer_relations/contact_state_counts_spec.rb
@@ -0,0 +1,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