recipient_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::Recipient, authenticated_as: :agent, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:recipient) { create(:customer) }
  6. let(:query) do
  7. <<~QUERY
  8. query autocompleteSearchRecipient($input: AutocompleteSearchRecipientInput!) {
  9. autocompleteSearchRecipient(input: $input) {
  10. value
  11. label
  12. labelPlaceholder
  13. heading
  14. headingPlaceholder
  15. disabled
  16. icon
  17. }
  18. }
  19. QUERY
  20. end
  21. before do
  22. gql.execute(query, variables: variables)
  23. end
  24. shared_examples 'returning expected recipient payload' do |contact:|
  25. let(:recipient_payload) do
  26. value = case contact
  27. when 'phone'
  28. recipient.phone
  29. when 'mobile'
  30. recipient.mobile
  31. else
  32. recipient.email
  33. end
  34. {
  35. 'value' => value,
  36. 'label' => value,
  37. 'labelPlaceholder' => nil,
  38. 'heading' => recipient.fullname,
  39. 'headingPlaceholder' => nil,
  40. 'icon' => nil,
  41. 'disabled' => nil,
  42. }
  43. end
  44. it 'returns expected recipient payload' do
  45. expect(gql.result.data).to eq([recipient_payload])
  46. end
  47. end
  48. shared_examples 'returning empty data set' do
  49. it 'returns empty data set' do
  50. expect(gql.result.data).to eq([])
  51. end
  52. end
  53. context 'when searching for recipients' do
  54. let(:variables) { { input: { query: query_string } } }
  55. context 'with implicit contact' do
  56. let(:query_string) { recipient.email }
  57. it_behaves_like 'returning expected recipient payload', contact: 'email'
  58. context 'when a specific recipient is excepted' do
  59. let(:variables) { { input: { query: query_string, exceptInternalId: recipient.id } } }
  60. it_behaves_like 'returning empty data set'
  61. end
  62. end
  63. context 'with explicit contact' do
  64. let(:variables) { { input: { query: query_string, contact: user_contact } } }
  65. let(:query_string) { recipient.login }
  66. context 'with email address' do
  67. let(:user_contact) { 'email' }
  68. it_behaves_like 'returning expected recipient payload', contact: 'email'
  69. end
  70. context 'with phone number' do
  71. let(:phone_number) do
  72. Faker::Config.locale = 'de'
  73. Faker::PhoneNumber.cell_phone_in_e164
  74. end
  75. let(:recipient) { create(:customer, phone: phone_number) }
  76. let(:user_contact) { 'phone' }
  77. it_behaves_like 'returning expected recipient payload', contact: 'phone'
  78. context 'with mobile number' do
  79. let(:mobile) do
  80. Faker::Config.locale = 'de'
  81. Faker::PhoneNumber.cell_phone_in_e164
  82. end
  83. let(:recipient) { create(:customer, mobile: phone_number) }
  84. it_behaves_like 'returning expected recipient payload', contact: 'mobile'
  85. end
  86. end
  87. context 'with multiple phone numbers' do
  88. let(:phone_number) do
  89. Faker::Config.locale = 'de'
  90. Faker::PhoneNumber.cell_phone_in_e164
  91. end
  92. let(:mobile_number) do
  93. Faker::Config.locale = 'de'
  94. Faker::PhoneNumber.cell_phone_in_e164
  95. end
  96. let(:recipient) { create(:customer, phone: phone_number, mobile: mobile_number) }
  97. let(:user_contact) { 'phone' }
  98. let(:recipient_payload) do
  99. [
  100. {
  101. 'value' => mobile_number,
  102. 'label' => mobile_number,
  103. 'labelPlaceholder' => nil,
  104. 'heading' => recipient.fullname,
  105. 'headingPlaceholder' => nil,
  106. 'icon' => nil,
  107. 'disabled' => nil,
  108. },
  109. {
  110. 'value' => phone_number,
  111. 'label' => phone_number,
  112. 'labelPlaceholder' => nil,
  113. 'heading' => recipient.fullname,
  114. 'headingPlaceholder' => nil,
  115. 'icon' => nil,
  116. 'disabled' => nil,
  117. },
  118. ]
  119. end
  120. it 'returns expected recipient payload' do
  121. expect(gql.result.data).to eq(recipient_payload)
  122. end
  123. end
  124. context 'with empty value' do
  125. let(:user_contact) { 'phone' }
  126. it_behaves_like 'returning empty data set'
  127. end
  128. context 'when a specific recipient is excepted' do
  129. let(:variables) { { input: { query: query_string, contact: user_contact, exceptInternalId: recipient.id } } }
  130. let(:user_contact) { 'email' }
  131. it_behaves_like 'returning empty data set'
  132. end
  133. end
  134. end
  135. end