agent_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::Agent, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for agents' do
  5. let(:agent) { create(:agent) }
  6. let(:agents) { create_list(:agent, 3, lastname: 'AutocompleteSearch') }
  7. let(:customers) { create_list(:customer, 3, lastname: 'AutocompleteSearch') } # must not be found
  8. let(:query) do
  9. <<~QUERY
  10. query autocompleteSearchAgent($input: AutocompleteSearchUserInput!) {
  11. autocompleteSearchAgent(input: $input) {
  12. value
  13. label
  14. labelPlaceholder
  15. heading
  16. headingPlaceholder
  17. disabled
  18. icon
  19. }
  20. }
  21. QUERY
  22. end
  23. let(:variables) { { input: { query: query_string, limit: limit, exceptInternalId: except } } }
  24. let(:query_string) { agents.last.lastname }
  25. let(:limit) { nil }
  26. let(:except) { nil }
  27. before do
  28. agents && customers
  29. gql.execute(query, variables: variables)
  30. end
  31. context 'without limit' do
  32. it 'finds all agents' do
  33. expect(gql.result.data.length).to eq(agents.length)
  34. end
  35. end
  36. context 'with limit' do
  37. let(:limit) { 1 }
  38. it 'respects the limit' do
  39. expect(gql.result.data.length).to eq(limit)
  40. end
  41. end
  42. context 'with exact search' do
  43. let(:first_user_payload) do
  44. {
  45. 'value' => agents.first.id,
  46. 'label' => agents.first.fullname,
  47. 'labelPlaceholder' => nil,
  48. 'heading' => nil,
  49. 'headingPlaceholder' => nil,
  50. 'icon' => nil,
  51. 'disabled' => nil,
  52. }
  53. end
  54. let(:query_string) { agents.first.login }
  55. it 'has data' do
  56. expect(gql.result.data).to eq([first_user_payload])
  57. end
  58. end
  59. context 'when sending an empty search string' do
  60. let(:query_string) { ' ' }
  61. it 'returns nothing' do
  62. expect(gql.result.data.length).to eq(0)
  63. end
  64. end
  65. context 'when a specific agent is excepted' do
  66. let(:except) { agents.first.id }
  67. let(:second_user_payload) do
  68. {
  69. 'value' => agents.second.id,
  70. 'label' => agents.second.fullname,
  71. 'labelPlaceholder' => nil,
  72. 'heading' => nil,
  73. 'headingPlaceholder' => nil,
  74. 'icon' => nil,
  75. 'disabled' => nil,
  76. }
  77. end
  78. let(:third_user_payload) do
  79. {
  80. 'value' => agents.third.id,
  81. 'label' => agents.third.fullname,
  82. 'labelPlaceholder' => nil,
  83. 'heading' => nil,
  84. 'headingPlaceholder' => nil,
  85. 'icon' => nil,
  86. 'disabled' => nil,
  87. }
  88. end
  89. it 'filters out provided agent' do
  90. expect(gql.result.data).to eq([third_user_payload, second_user_payload])
  91. end
  92. end
  93. it_behaves_like 'graphql responds with error if unauthenticated'
  94. end
  95. end