diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-16 12:09:26 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-16 12:09:26 +0000 |
commit | b019dc959ec16b15fe42a680dbd729542ff61537 (patch) | |
tree | cde5c3d0582bd7542e1e195d83ad7a50079f4e49 /spec/models/customer_relations | |
parent | b7b44de429911864686599ef1643baf525bf75ec (diff) | |
download | gitlab-ce-b019dc959ec16b15fe42a680dbd729542ff61537.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/customer_relations')
-rw-r--r-- | spec/models/customer_relations/contact_spec.rb | 95 | ||||
-rw-r--r-- | spec/models/customer_relations/organization_spec.rb | 79 |
2 files changed, 174 insertions, 0 deletions
diff --git a/spec/models/customer_relations/contact_spec.rb b/spec/models/customer_relations/contact_spec.rb index 86f868b269e..f91546f5240 100644 --- a/spec/models/customer_relations/contact_spec.rb +++ b/spec/models/customer_relations/contact_spec.rb @@ -142,4 +142,99 @@ RSpec.describe CustomerRelations::Contact, type: :model do expect(issue_contact2.reload.contact_id).to eq(dupe_contact1.id) end end + + describe '.search' do + let_it_be(:contact_a) do + create( + :contact, + group: group, + first_name: "ABC", + last_name: "DEF", + email: "ghi@test.com", + description: "LMNO", + state: "inactive" + ) + end + + let_it_be(:contact_b) do + create( + :contact, + group: group, + first_name: "PQR", + last_name: "STU", + email: "vwx@test.com", + description: "YZ", + state: "active" + ) + end + + subject(:found_contacts) { group.contacts.search(search_term) } + + context 'when search term is empty' do + let(:search_term) { "" } + + it 'returns all group contacts' do + expect(found_contacts).to contain_exactly(contact_a, contact_b) + end + end + + context 'when search term is not empty' do + context 'when searching for first name ignoring casing' do + let(:search_term) { "aBc" } + + it { is_expected.to contain_exactly(contact_a) } + end + + context 'when searching for last name ignoring casing' do + let(:search_term) { "StU" } + + it { is_expected.to contain_exactly(contact_b) } + end + + context 'when searching for email' do + let(:search_term) { "ghi" } + + it { is_expected.to contain_exactly(contact_a) } + end + + context 'when searching description ignoring casing' do + let(:search_term) { "Yz" } + + it { is_expected.to contain_exactly(contact_b) } + end + + context 'when fuzzy searching for email and last name' do + let(:search_term) { "s" } + + it { is_expected.to contain_exactly(contact_a, contact_b) } + end + end + end + + describe '.search_by_state' do + let_it_be(:contact_a) { create(:contact, group: group, state: "inactive") } + let_it_be(:contact_b) { create(:contact, group: group, state: "active") } + + context 'when searching for contacts state' do + it 'returns only inactive contacts' do + expect(group.contacts.search_by_state(:inactive)).to contain_exactly(contact_a) + end + + it 'returns only active contacts' do + expect(group.contacts.search_by_state(:active)).to contain_exactly(contact_b) + end + end + end + + describe '.sort_by_name' do + 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") } + + context 'when sorting the contacts' do + it 'sorts them by last name then first name in ascendent order' do + expect(group.contacts.sort_by_name).to eq([contact_b, contact_a, contact_c]) + end + end + end end diff --git a/spec/models/customer_relations/organization_spec.rb b/spec/models/customer_relations/organization_spec.rb index 06ba9c5b7ad..1833fcf5385 100644 --- a/spec/models/customer_relations/organization_spec.rb +++ b/spec/models/customer_relations/organization_spec.rb @@ -78,4 +78,83 @@ RSpec.describe CustomerRelations::Organization, type: :model do expect(contact2.reload.organization_id).to eq(dupe_organization1.id) end end + + describe '.search' do + let_it_be(:organization_a) do + create( + :organization, + group: group, + name: "DEF", + description: "ghi_st", + state: "inactive" + ) + end + + let_it_be(:organization_b) do + create( + :organization, + group: group, + name: "ABC_st", + description: "JKL", + state: "active" + ) + end + + subject(:found_organizations) { group.organizations.search(search_term) } + + context 'when search term is empty' do + let(:search_term) { "" } + + it 'returns all group organizations' do + expect(found_organizations).to contain_exactly(organization_a, organization_b) + end + end + + context 'when search term is not empty' do + context 'when searching for name' do + let(:search_term) { "aBc" } + + it { is_expected.to contain_exactly(organization_b) } + end + + context 'when searching for description' do + let(:search_term) { "ghI" } + + it { is_expected.to contain_exactly(organization_a) } + end + + context 'when searching for name and description' do + let(:search_term) { "_st" } + + it { is_expected.to contain_exactly(organization_a, organization_b) } + end + end + end + + describe '.search_by_state' do + let_it_be(:organization_a) { create(:organization, group: group, state: "inactive") } + let_it_be(:organization_b) { create(:organization, group: group, state: "active") } + + context 'when searching for organizations state' do + it 'returns only inactive organizations' do + expect(group.organizations.search_by_state(:inactive)).to contain_exactly(organization_a) + end + + it 'returns only active organizations' do + expect(group.organizations.search_by_state(:active)).to contain_exactly(organization_b) + end + end + end + + describe '.sort_by_name' do + let_it_be(:organization_a) { create(:organization, group: group, name: "c") } + let_it_be(:organization_b) { create(:organization, group: group, name: "a") } + let_it_be(:organization_c) { create(:organization, group: group, name: "b") } + + context 'when sorting the organizations' do + it 'sorts them by name in ascendent order' do + expect(group.organizations.sort_by_name).to eq([organization_b, organization_c, organization_a]) + end + end + end end |