recipient_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. end
  59. context 'with explicit contact' do
  60. let(:variables) { { input: { query: query_string, contact: user_contact } } }
  61. let(:query_string) { recipient.login }
  62. context 'with email address' do
  63. let(:user_contact) { 'email' }
  64. it_behaves_like 'returning expected recipient payload', contact: 'email'
  65. end
  66. context 'with phone number' do
  67. let(:phone_number) do
  68. Faker::Config.locale = 'de'
  69. Faker::PhoneNumber.cell_phone_in_e164
  70. end
  71. let(:recipient) { create(:customer, phone: phone_number) }
  72. let(:user_contact) { 'phone' }
  73. it_behaves_like 'returning expected recipient payload', contact: 'phone'
  74. context 'with mobile number' do
  75. let(:mobile) do
  76. Faker::Config.locale = 'de'
  77. Faker::PhoneNumber.cell_phone_in_e164
  78. end
  79. let(:recipient) { create(:customer, mobile: phone_number) }
  80. it_behaves_like 'returning expected recipient payload', contact: 'mobile'
  81. end
  82. end
  83. context 'with multiple phone numbers' do
  84. let(:phone_number) do
  85. Faker::Config.locale = 'de'
  86. Faker::PhoneNumber.cell_phone_in_e164
  87. end
  88. let(:mobile_number) do
  89. Faker::Config.locale = 'de'
  90. Faker::PhoneNumber.cell_phone_in_e164
  91. end
  92. let(:recipient) { create(:customer, phone: phone_number, mobile: mobile_number) }
  93. let(:user_contact) { 'phone' }
  94. let(:recipient_payload) do
  95. [
  96. {
  97. 'value' => mobile_number,
  98. 'label' => mobile_number,
  99. 'labelPlaceholder' => nil,
  100. 'heading' => recipient.fullname,
  101. 'headingPlaceholder' => nil,
  102. 'icon' => nil,
  103. 'disabled' => nil,
  104. },
  105. {
  106. 'value' => phone_number,
  107. 'label' => phone_number,
  108. 'labelPlaceholder' => nil,
  109. 'heading' => recipient.fullname,
  110. 'headingPlaceholder' => nil,
  111. 'icon' => nil,
  112. 'disabled' => nil,
  113. },
  114. ]
  115. end
  116. it 'returns expected recipient payload' do
  117. expect(gql.result.data).to eq(recipient_payload)
  118. end
  119. end
  120. context 'with empty value' do
  121. let(:user_contact) { 'phone' }
  122. it_behaves_like 'returning empty data set'
  123. end
  124. end
  125. end
  126. end