summaryrefslogtreecommitdiff
path: root/spec/graphql/resolvers/crm/contacts_resolver_spec.rb
blob: 98da4aeac28377bab5aa67823bd264a32c497a7d (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::Crm::ContactsResolver do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group, :crm_enabled) }

  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

  describe '#resolve' do
    context 'with unauthorized user' do
      it 'does not rise an error and returns no contacts' do
        expect { resolve_contacts(group) }.not_to raise_error
        expect(resolve_contacts(group)).to be_empty
      end
    end

    context 'with authorized user' do
      it 'does not rise an error and returns all contacts in the correct order' do
        group.add_reporter(user)

        expect { resolve_contacts(group) }.not_to raise_error
        expect(resolve_contacts(group)).to eq([contact_a, contact_b])
      end
    end

    context 'without parent' do
      it 'returns no contacts' do
        expect(resolve_contacts(nil)).to be_empty
      end
    end

    context 'with a group parent' do
      before do
        group.add_developer(user)
      end

      context 'when no filter is provided' do
        it 'returns all the contacts in the correct order' do
          expect(resolve_contacts(group)).to eq([contact_a, contact_b])
        end
      end

      context 'when search term is provided' do
        it 'returns the correct contacts' do
          expect(resolve_contacts(group, { search: "x@test.com" })).to contain_exactly(contact_b)
        end
      end

      context 'when state is provided' do
        it 'returns the correct contacts' do
          expect(resolve_contacts(group, { state: :inactive })).to contain_exactly(contact_a)
        end
      end

      context 'when ids are provided' do
        it 'returns the correct contacts' do
          expect(resolve_contacts(group, { ids: [contact_a.to_global_id] })).to contain_exactly(contact_a)
        end
      end
    end
  end

  def resolve_contacts(parent, args = {}, context = { current_user: user })
    resolve(described_class, obj: parent, args: args, ctx: context)
  end
end