status_relationships_presenter_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) { described_class.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.filter_map(&:reblog_of_id) }
  16. let(:default_map) { { 1 => true } }
  17. context 'when 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 'when 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 'when 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 'when 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 'when 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 'when 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. context 'when post includes filtered individual statuses' do
  77. let(:statuses) { [Fabricate(:status, text: 'hello world'), Fabricate(:status, reblog: Fabricate(:status, text: 'this toot is about an irrelevant word'))] }
  78. let(:options) { {} }
  79. before do
  80. filter = Account.find(current_account_id).custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide)
  81. filter.statuses.create!(status_id: statuses[0].id)
  82. filter.statuses.create!(status_id: statuses[1].reblog_of_id)
  83. end
  84. it 'sets @filters_map to filter top-level status' do
  85. matched_filters = presenter.filters_map[statuses[0].id]
  86. expect(matched_filters.size).to eq 1
  87. expect(matched_filters[0].filter.title).to eq 'filter1'
  88. expect(matched_filters[0].status_matches).to eq [statuses[0].id]
  89. end
  90. it 'sets @filters_map to filter reblogged status' do
  91. matched_filters = presenter.filters_map[statuses[1].reblog_of_id]
  92. expect(matched_filters.size).to eq 1
  93. expect(matched_filters[0].filter.title).to eq 'filter1'
  94. expect(matched_filters[0].status_matches).to eq [statuses[1].reblog_of_id]
  95. end
  96. end
  97. end
  98. end