search_service_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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).not_to have_received(:new)
  13. expect(Tag).not_to have_received(:search_for)
  14. end
  15. end
  16. describe 'with an url query' do
  17. before do
  18. @query = 'http://test.host/query'
  19. end
  20. context 'that does not find anything' do
  21. it 'returns the empty results' do
  22. service = double(call: nil)
  23. allow(ResolveURLService).to receive(:new).and_return(service)
  24. results = subject.call(@query, nil, 10, resolve: true)
  25. expect(service).to have_received(:call).with(@query, on_behalf_of: nil)
  26. expect(results).to eq empty_results
  27. end
  28. end
  29. context 'that finds an account' do
  30. it 'includes the account in the results' do
  31. account = Account.new
  32. service = double(call: account)
  33. allow(ResolveURLService).to receive(:new).and_return(service)
  34. results = subject.call(@query, nil, 10, resolve: true)
  35. expect(service).to have_received(:call).with(@query, on_behalf_of: nil)
  36. expect(results).to eq empty_results.merge(accounts: [account])
  37. end
  38. end
  39. context 'that finds a status' do
  40. it 'includes the status in the results' do
  41. status = Status.new
  42. service = double(call: status)
  43. allow(ResolveURLService).to receive(:new).and_return(service)
  44. results = subject.call(@query, nil, 10, resolve: true)
  45. expect(service).to have_received(:call).with(@query, on_behalf_of: nil)
  46. expect(results).to eq empty_results.merge(statuses: [status])
  47. end
  48. end
  49. end
  50. describe 'with a non-url query' do
  51. context 'that matches an account' do
  52. it 'includes the account in the results' do
  53. query = 'username'
  54. account = Account.new
  55. service = double(call: [account])
  56. allow(AccountSearchService).to receive(:new).and_return(service)
  57. results = subject.call(query, nil, 10)
  58. expect(service).to have_received(:call).with(query, nil, limit: 10, offset: 0, resolve: false)
  59. expect(results).to eq empty_results.merge(accounts: [account])
  60. end
  61. end
  62. context 'that matches a tag' do
  63. it 'includes the tag in the results' do
  64. query = '#tag'
  65. tag = Tag.new
  66. allow(Tag).to receive(:search_for).with('tag', 10, 0, exclude_unreviewed: nil).and_return([tag])
  67. results = subject.call(query, nil, 10)
  68. expect(Tag).to have_received(:search_for).with('tag', 10, 0, exclude_unreviewed: nil)
  69. expect(results).to eq empty_results.merge(hashtags: [tag])
  70. end
  71. it 'does not include tag when starts with @ character' do
  72. query = '@username'
  73. allow(Tag).to receive(:search_for)
  74. results = subject.call(query, nil, 10)
  75. expect(Tag).not_to have_received(:search_for)
  76. expect(results).to eq empty_results
  77. end
  78. end
  79. end
  80. end
  81. def empty_results
  82. { accounts: [], hashtags: [], statuses: [] }
  83. end
  84. end