user_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::User, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for users' do
  5. let(:agent) { create(:agent) }
  6. let(:users) { create_list(:agent, 3, lastname: 'AutocompleteSearch') }
  7. let(:query) do
  8. <<~QUERY
  9. query autocompleteSearchUser($input: AutocompleteSearchInput!) {
  10. autocompleteSearchUser(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) { users.last.lastname }
  24. let(:limit) { nil }
  25. before do
  26. gql.execute(query, variables: variables)
  27. end
  28. context 'without limit' do
  29. it 'finds all users' do
  30. expect(gql.result.data.length).to eq(users.length)
  31. end
  32. end
  33. context 'with limit' do
  34. let(:limit) { 1 }
  35. it 'respects the limit' do
  36. expect(gql.result.data.length).to eq(limit)
  37. end
  38. end
  39. context 'with exact search' do
  40. let(:first_user_payload) do
  41. {
  42. 'value' => users.first.id,
  43. 'label' => users.first.fullname,
  44. 'labelPlaceholder' => nil,
  45. 'heading' => nil,
  46. 'headingPlaceholder' => nil,
  47. 'icon' => nil,
  48. 'disabled' => nil,
  49. }
  50. end
  51. let(:query_string) { users.first.login }
  52. it 'has data' do
  53. expect(gql.result.data).to eq([first_user_payload])
  54. end
  55. end
  56. context 'when sending an empty search string' do
  57. let(:query_string) { ' ' }
  58. it 'returns nothing' do
  59. expect(gql.result.data.length).to eq(0)
  60. end
  61. end
  62. it_behaves_like 'graphql responds with error if unauthenticated'
  63. end
  64. end