search_service_spec.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe SearchService, type: :service do
  4. subject { described_class.new }
  5. describe '#call' do
  6. describe 'with a blank query' do
  7. it 'returns empty results without searching' do
  8. allow(AccountSearchService).to receive(:new)
  9. allow(Tag).to receive(:search_for)
  10. results = subject.call('', nil, 10)
  11. expect(results).to eq(empty_results)
  12. expect(AccountSearchService).to_not have_received(:new)
  13. expect(Tag).to_not have_received(:search_for)
  14. end
  15. end
  16. describe 'with an url query' do
  17. let(:query) { 'http://test.host/query' }
  18. context 'when it does not find anything' do
  19. it 'returns the empty results' do
  20. service = instance_double(ResolveURLService, call: nil)
  21. allow(ResolveURLService).to receive(:new).and_return(service)
  22. results = subject.call(query, nil, 10, resolve: true)
  23. expect(service).to have_received(:call).with(query, on_behalf_of: nil)
  24. expect(results).to eq empty_results
  25. end
  26. end
  27. context 'when it finds an account' do
  28. it 'includes the account in the results' do
  29. account = Account.new
  30. service = instance_double(ResolveURLService, call: account)
  31. allow(ResolveURLService).to receive(:new).and_return(service)
  32. results = subject.call(query, nil, 10, resolve: true)
  33. expect(service).to have_received(:call).with(query, on_behalf_of: nil)
  34. expect(results).to eq empty_results.merge(accounts: [account])
  35. end
  36. end
  37. context 'when it finds a status' do
  38. it 'includes the status in the results' do
  39. status = Status.new
  40. service = instance_double(ResolveURLService, call: status)
  41. allow(ResolveURLService).to receive(:new).and_return(service)
  42. results = subject.call(query, nil, 10, resolve: true)
  43. expect(service).to have_received(:call).with(query, on_behalf_of: nil)
  44. expect(results).to eq empty_results.merge(statuses: [status])
  45. end
  46. end
  47. end
  48. describe 'with a non-url query' do
  49. context 'when it matches an account' do
  50. it 'includes the account in the results' do
  51. query = 'username'
  52. account = Account.new
  53. service = instance_double(AccountSearchService, call: [account])
  54. allow(AccountSearchService).to receive(:new).and_return(service)
  55. results = subject.call(query, nil, 10)
  56. expect(service).to have_received(:call).with(query, nil, limit: 10, offset: 0, resolve: false, start_with_hashtag: false, use_searchable_text: true, following: false)
  57. expect(results).to eq empty_results.merge(accounts: [account])
  58. end
  59. end
  60. context 'when it matches a tag' do
  61. it 'includes the tag in the results' do
  62. query = '#tag'
  63. tag = Tag.new
  64. allow(Tag).to receive(:search_for).with('tag', 10, 0, { exclude_unreviewed: nil }).and_return([tag])
  65. results = subject.call(query, nil, 10)
  66. expect(Tag).to have_received(:search_for).with('tag', 10, 0, exclude_unreviewed: nil)
  67. expect(results).to eq empty_results.merge(hashtags: [tag])
  68. end
  69. end
  70. end
  71. end
  72. def empty_results
  73. { accounts: [], hashtags: [], statuses: [] }
  74. end
  75. end