generic_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::Generic, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for users' do
  5. let(:agent) { create(:agent, groups: [ticket.group]) }
  6. let!(:users) { create_list(:customer, 3, lastname: 'AutocompleteSearch') }
  7. let!(:organization) { create(:organization, name: 'AutocompleteSearch') }
  8. let!(:ticket) do
  9. create(:ticket, title: 'AutocompleteSearch').tap do |ticket|
  10. # Article required to find ticket via SQL
  11. create(:ticket_article, ticket: ticket)
  12. end
  13. end
  14. let(:query) do
  15. <<~QUERY
  16. query autocompleteSearchGeneric($input: AutocompleteSearchGenericInput!) {
  17. autocompleteSearchGeneric(input: $input) {
  18. value
  19. label
  20. object {
  21. ... on User { id }
  22. ... on Organization { id }
  23. ... on Ticket { id }
  24. }
  25. }
  26. }
  27. QUERY
  28. end
  29. let(:variables) { { input: { query: query_string, limit:, onlyIn: only_in } } }
  30. let(:query_string) { 'AutocompleteSearch' }
  31. let(:limit) { nil }
  32. let(:only_in) { nil }
  33. before do
  34. gql.execute(query, variables: variables)
  35. end
  36. context 'without limit' do
  37. it 'finds all objects' do
  38. expect(gql.result.data.length).to eq(5)
  39. end
  40. end
  41. context 'with limit' do
  42. let(:limit) { 1 }
  43. let(:expected_data) do
  44. [
  45. {
  46. 'label' => "##{ticket.number} - #{ticket.title}",
  47. 'value' => ticket.id,
  48. 'object' => { 'id' => gql.id(ticket) }
  49. },
  50. {
  51. 'label' => users.last.fullname,
  52. 'value' => users.last.id,
  53. 'object' => { 'id' => gql.id(users.last) }
  54. },
  55. {
  56. 'label' => organization.name,
  57. 'value' => organization.id,
  58. 'object' => { 'id' => gql.id(organization) }
  59. },
  60. ]
  61. end
  62. it 'respects the limit' do
  63. expect(gql.result.data).to eq(expected_data)
  64. end
  65. context 'with onlyIn' do
  66. let(:only_in) { ['Ticket'] }
  67. let(:expected_data) do
  68. [
  69. {
  70. 'label' => "##{ticket.number} - #{ticket.title}",
  71. 'value' => ticket.id,
  72. 'object' => { 'id' => gql.id(ticket) }
  73. },
  74. ]
  75. end
  76. it 'filters objects' do
  77. expect(gql.result.data).to eq(expected_data)
  78. end
  79. end
  80. end
  81. context 'when sending an empty search string' do
  82. let(:query_string) { ' ' }
  83. it 'returns nothing' do
  84. expect(gql.result.data.length).to eq(0)
  85. end
  86. end
  87. it_behaves_like 'graphql responds with error if unauthenticated'
  88. end
  89. end