organization_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::Organization, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for organizations' do
  5. let(:agent) { create(:agent) }
  6. let(:organizations) { create_list(:organization, 3, note: 'AutocompleteSearch') }
  7. let(:query) do
  8. <<~QUERY
  9. query autocompleteSearchOrganization($input: AutocompleteSearchOrganizationInput!) {
  10. autocompleteSearchOrganization(input: $input) {
  11. value
  12. label
  13. labelPlaceholder
  14. heading
  15. headingPlaceholder
  16. disabled
  17. icon
  18. }
  19. }
  20. QUERY
  21. end
  22. let(:variables) { { input: { query: query_string, limit: limit } } }
  23. let(:query_string) { organizations.last.note }
  24. let(:limit) { nil }
  25. context 'without limit' do
  26. it 'finds all organizations' do
  27. gql.execute(query, variables: variables)
  28. expect(gql.result.data.length).to eq(organizations.length)
  29. end
  30. end
  31. context 'with limit' do
  32. let(:limit) { 1 }
  33. it 'respects the limit' do
  34. gql.execute(query, variables: variables)
  35. expect(gql.result.data.length).to eq(limit)
  36. end
  37. end
  38. context 'with exact search' do
  39. let(:first_organization_payload) do
  40. {
  41. 'value' => organizations.first.id,
  42. 'label' => organizations.first.name,
  43. 'labelPlaceholder' => nil,
  44. 'heading' => nil,
  45. 'headingPlaceholder' => nil,
  46. 'icon' => nil,
  47. 'disabled' => nil,
  48. }
  49. end
  50. let(:query_string) { organizations.first.name }
  51. it 'has data' do
  52. gql.execute(query, variables: variables)
  53. expect(gql.result.data).to eq([first_organization_payload])
  54. end
  55. end
  56. context 'when sending an empty search string' do
  57. let(:query_string) { ' ' }
  58. it 'returns nothing' do
  59. gql.execute(query, variables: variables)
  60. expect(gql.result.data.length).to eq(0)
  61. end
  62. end
  63. context 'when customer is set' do
  64. let(:organizations) { create_list(:organization, 5, note: 'AutocompleteSearch') }
  65. let(:customer) { create(:customer, organization: organizations[0], organizations: [organizations[1], organizations[2]]) }
  66. let(:variables) { { input: { query: query_string, limit: limit, customerId: gql.id(customer) } } }
  67. let(:query_string) { 'dummy' }
  68. context 'with primary organization' do
  69. before do
  70. organizations.first.update!(note: query_string)
  71. end
  72. let(:primary_organization_payload) do
  73. {
  74. 'value' => organizations.first.id,
  75. 'label' => organizations.first.name,
  76. 'labelPlaceholder' => nil,
  77. 'heading' => nil,
  78. 'headingPlaceholder' => nil,
  79. 'icon' => nil,
  80. 'disabled' => nil,
  81. }
  82. end
  83. it 'finds the primary organization' do
  84. gql.execute(query, variables: variables)
  85. expect(gql.result.data).to eq([primary_organization_payload])
  86. end
  87. end
  88. context 'with secondary organization' do
  89. let(:secondary_organization) { organizations[1] }
  90. let(:secondary_organization_payload) do
  91. {
  92. 'value' => secondary_organization.id,
  93. 'label' => secondary_organization.name,
  94. 'labelPlaceholder' => nil,
  95. 'heading' => nil,
  96. 'headingPlaceholder' => nil,
  97. 'icon' => nil,
  98. 'disabled' => nil,
  99. }
  100. end
  101. before do
  102. secondary_organization.update!(note: query_string)
  103. end
  104. it 'finds the secondary organization' do
  105. gql.execute(query, variables: variables)
  106. expect(gql.result.data).to eq([secondary_organization_payload])
  107. end
  108. end
  109. context 'with unassigned organization' do
  110. let(:unassigned_organization) { organizations[3] }
  111. before do
  112. unassigned_organization.update!(note: query_string)
  113. end
  114. it 'returns nothing' do
  115. gql.execute(query, variables: variables)
  116. expect(gql.result.data.length).to eq(0)
  117. end
  118. end
  119. end
  120. context 'when unauthenticated' do
  121. before do
  122. gql.execute(query, variables: variables)
  123. end
  124. it_behaves_like 'graphql responds with error if unauthenticated'
  125. end
  126. end
  127. end