The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 lines
3.3 KiB

  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)
  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)
  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)
  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).and_return([tag])
  67. results = subject.call(query, nil, 10)
  68. expect(Tag).to have_received(:search_for).with('tag', 10, 0)
  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