status_relationships_presenter_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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).to have_attributes(
  21. reblogs_map: eq(default_map),
  22. favourites_map: eq(default_map),
  23. bookmarks_map: eq(default_map),
  24. mutes_map: eq(default_map),
  25. pins_map: eq(default_map)
  26. )
  27. end
  28. end
  29. context 'when options[:reblogs_map] is set' do
  30. let(:options) { { reblogs_map: { 2 => true } } }
  31. it 'sets @reblogs_map merged with default_map and options[:reblogs_map]' do
  32. expect(presenter.reblogs_map).to eq default_map.merge(options[:reblogs_map])
  33. end
  34. end
  35. context 'when options[:favourites_map] is set' do
  36. let(:options) { { favourites_map: { 3 => true } } }
  37. it 'sets @favourites_map merged with default_map and options[:favourites_map]' do
  38. expect(presenter.favourites_map).to eq default_map.merge(options[:favourites_map])
  39. end
  40. end
  41. context 'when options[:bookmarks_map] is set' do
  42. let(:options) { { bookmarks_map: { 4 => true } } }
  43. it 'sets @bookmarks_map merged with default_map and options[:bookmarks_map]' do
  44. expect(presenter.bookmarks_map).to eq default_map.merge(options[:bookmarks_map])
  45. end
  46. end
  47. context 'when options[:mutes_map] is set' do
  48. let(:options) { { mutes_map: { 5 => true } } }
  49. it 'sets @mutes_map merged with default_map and options[:mutes_map]' do
  50. expect(presenter.mutes_map).to eq default_map.merge(options[:mutes_map])
  51. end
  52. end
  53. context 'when options[:pins_map] is set' do
  54. let(:options) { { pins_map: { 6 => true } } }
  55. it 'sets @pins_map merged with default_map and options[:pins_map]' do
  56. expect(presenter.pins_map).to eq default_map.merge(options[:pins_map])
  57. end
  58. end
  59. context 'when post includes filtered terms' do
  60. 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'))] }
  61. let(:options) { {} }
  62. before do
  63. Account.find(current_account_id).custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
  64. end
  65. it 'sets @filters_map to filter top-level status' do
  66. matched_filters = presenter.filters_map[statuses[0].id]
  67. expect(matched_filters)
  68. .to be_an(Array)
  69. .and have_attributes(size: 1)
  70. .and contain_exactly(
  71. have_attributes(
  72. filter: have_attributes(title: 'filter1'),
  73. keyword_matches: contain_exactly('banned')
  74. )
  75. )
  76. end
  77. it 'sets @filters_map to filter reblogged status' do
  78. matched_filters = presenter.filters_map[statuses[1].reblog_of_id]
  79. expect(matched_filters)
  80. .to be_an(Array)
  81. .and have_attributes(size: 1)
  82. .and contain_exactly(
  83. have_attributes(
  84. filter: have_attributes(title: 'filter1'),
  85. keyword_matches: contain_exactly('irrelevant')
  86. )
  87. )
  88. end
  89. end
  90. context 'when post includes filtered individual statuses' do
  91. let(:statuses) { [Fabricate(:status, text: 'hello world'), Fabricate(:status, reblog: Fabricate(:status, text: 'this toot is about an irrelevant word'))] }
  92. let(:options) { {} }
  93. before do
  94. filter = Account.find(current_account_id).custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide)
  95. filter.statuses.create!(status_id: statuses[0].id)
  96. filter.statuses.create!(status_id: statuses[1].reblog_of_id)
  97. end
  98. it 'sets @filters_map to filter top-level status' do
  99. matched_filters = presenter.filters_map[statuses[0].id]
  100. expect(matched_filters)
  101. .to be_an(Array)
  102. .and have_attributes(size: 1)
  103. .and contain_exactly(
  104. have_attributes(
  105. filter: have_attributes(title: 'filter1'),
  106. status_matches: contain_exactly(statuses.first.id)
  107. )
  108. )
  109. end
  110. it 'sets @filters_map to filter reblogged status' do
  111. matched_filters = presenter.filters_map[statuses[1].reblog_of_id]
  112. expect(matched_filters)
  113. .to be_an(Array)
  114. .and have_attributes(size: 1)
  115. .and contain_exactly(
  116. have_attributes(
  117. filter: have_attributes(title: 'filter1'),
  118. status_matches: contain_exactly(statuses.second.reblog_of_id)
  119. )
  120. )
  121. end
  122. end
  123. end
  124. end