status_relationships_presenter_spec.rb 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe StatusRelationshipsPresenter do
  4. describe '.initialize' do
  5. before do
  6. allow(Status).to receive(:reblogs_map).with(match_array(status_ids), current_account_id).and_return(default_map)
  7. allow(Status).to receive(:favourites_map).with(status_ids, current_account_id).and_return(default_map)
  8. allow(Status).to receive(:bookmarks_map).with(status_ids, current_account_id).and_return(default_map)
  9. allow(Status).to receive(:mutes_map).with(anything, current_account_id).and_return(default_map)
  10. allow(Status).to receive(:pins_map).with(anything, current_account_id).and_return(default_map)
  11. end
  12. let(:presenter) { StatusRelationshipsPresenter.new(statuses, current_account_id, **options) }
  13. let(:current_account_id) { Fabricate(:account).id }
  14. let(:statuses) { [Fabricate(:status)] }
  15. let(:status_ids) { statuses.map(&:id) + statuses.map(&:reblog_of_id).compact }
  16. let(:default_map) { { 1 => true } }
  17. context 'options are not set' do
  18. let(:options) { {} }
  19. it 'sets default maps' do
  20. expect(presenter.reblogs_map).to eq default_map
  21. expect(presenter.favourites_map).to eq default_map
  22. expect(presenter.bookmarks_map).to eq default_map
  23. expect(presenter.mutes_map).to eq default_map
  24. expect(presenter.pins_map).to eq default_map
  25. end
  26. end
  27. context 'options[:reblogs_map] is set' do
  28. let(:options) { { reblogs_map: { 2 => true } } }
  29. it 'sets @reblogs_map merged with default_map and options[:reblogs_map]' do
  30. expect(presenter.reblogs_map).to eq default_map.merge(options[:reblogs_map])
  31. end
  32. end
  33. context 'options[:favourites_map] is set' do
  34. let(:options) { { favourites_map: { 3 => true } } }
  35. it 'sets @favourites_map merged with default_map and options[:favourites_map]' do
  36. expect(presenter.favourites_map).to eq default_map.merge(options[:favourites_map])
  37. end
  38. end
  39. context 'options[:bookmarks_map] is set' do
  40. let(:options) { { bookmarks_map: { 4 => true } } }
  41. it 'sets @bookmarks_map merged with default_map and options[:bookmarks_map]' do
  42. expect(presenter.bookmarks_map).to eq default_map.merge(options[:bookmarks_map])
  43. end
  44. end
  45. context 'options[:mutes_map] is set' do
  46. let(:options) { { mutes_map: { 5 => true } } }
  47. it 'sets @mutes_map merged with default_map and options[:mutes_map]' do
  48. expect(presenter.mutes_map).to eq default_map.merge(options[:mutes_map])
  49. end
  50. end
  51. context 'options[:pins_map] is set' do
  52. let(:options) { { pins_map: { 6 => true } } }
  53. it 'sets @pins_map merged with default_map and options[:pins_map]' do
  54. expect(presenter.pins_map).to eq default_map.merge(options[:pins_map])
  55. end
  56. end
  57. context 'when post includes filtered terms' do
  58. let(:statuses) { [Fabricate(:status, text: 'this toot is about that banned word'), Fabricate(:status, reblog: Fabricate(:status, text: 'this toot is about an irrelevant word'))] }
  59. let(:options) { {} }
  60. before do
  61. Account.find(current_account_id).custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
  62. end
  63. it 'sets @filters_map to filter top-level status' do
  64. matched_filters = presenter.filters_map[statuses[0].id]
  65. expect(matched_filters.size).to eq 1
  66. expect(matched_filters[0].filter.title).to eq 'filter1'
  67. expect(matched_filters[0].keyword_matches).to eq ['banned']
  68. end
  69. it 'sets @filters_map to filter reblogged status' do
  70. matched_filters = presenter.filters_map[statuses[1].reblog_of_id]
  71. expect(matched_filters.size).to eq 1
  72. expect(matched_filters[0].filter.title).to eq 'filter1'
  73. expect(matched_filters[0].keyword_matches).to eq ['irrelevant']
  74. end
  75. end
  76. end
  77. end