From a36b59be8ad7656b7ceab9751c9ec5b3563e3a30 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 13 Nov 2023 09:32:36 -0500 Subject: [PATCH] Spec coverage for `api/v1/trends` controllers (#27837) --- .../api/v1/trends/links_controller_spec.rb | 38 +++++++++++++++-- .../api/v1/trends/statuses_controller_spec.rb | 38 +++++++++++++++-- .../api/v1/trends/tags_controller_spec.rb | 41 +++++++++++++++---- 3 files changed, 101 insertions(+), 16 deletions(-) diff --git a/spec/controllers/api/v1/trends/links_controller_spec.rb b/spec/controllers/api/v1/trends/links_controller_spec.rb index 71a7e2e477..12d4198aaf 100644 --- a/spec/controllers/api/v1/trends/links_controller_spec.rb +++ b/spec/controllers/api/v1/trends/links_controller_spec.rb @@ -2,14 +2,44 @@ require 'rails_helper' -describe Api::V1::Trends::LinksController do +RSpec.describe Api::V1::Trends::LinksController do render_views describe 'GET #index' do - it 'returns http success' do - get :index + around do |example| + previous = Setting.trends + example.run + Setting.trends = previous + end - expect(response).to have_http_status(200) + context 'when trends are disabled' do + before { Setting.trends = false } + + it 'returns http success' do + get :index + + expect(response).to have_http_status(200) + end + end + + context 'when trends are enabled' do + before { Setting.trends = true } + + it 'returns http success' do + prepare_trends + stub_const('Api::V1::Trends::LinksController::DEFAULT_LINKS_LIMIT', 2) + get :index + + expect(response).to have_http_status(200) + expect(response.headers).to include('Link') + end + + def prepare_trends + Fabricate.times(3, :preview_card, trendable: true, language: 'en').each do |link| + 2.times { |i| Trends.links.add(link, i) } + end + Trends::Links.new(threshold: 1).refresh + end end end end diff --git a/spec/controllers/api/v1/trends/statuses_controller_spec.rb b/spec/controllers/api/v1/trends/statuses_controller_spec.rb index e9892bb140..69fdb270d5 100644 --- a/spec/controllers/api/v1/trends/statuses_controller_spec.rb +++ b/spec/controllers/api/v1/trends/statuses_controller_spec.rb @@ -2,14 +2,44 @@ require 'rails_helper' -describe Api::V1::Trends::StatusesController do +RSpec.describe Api::V1::Trends::StatusesController do render_views describe 'GET #index' do - it 'returns http success' do - get :index + around do |example| + previous = Setting.trends + example.run + Setting.trends = previous + end - expect(response).to have_http_status(200) + context 'when trends are disabled' do + before { Setting.trends = false } + + it 'returns http success' do + get :index + + expect(response).to have_http_status(200) + end + end + + context 'when trends are enabled' do + before { Setting.trends = true } + + it 'returns http success' do + prepare_trends + stub_const('Api::BaseController::DEFAULT_STATUSES_LIMIT', 2) + get :index + + expect(response).to have_http_status(200) + expect(response.headers).to include('Link') + end + + def prepare_trends + Fabricate.times(3, :status, trendable: true, language: 'en').each do |status| + 2.times { |i| Trends.statuses.add(status, i) } + end + Trends::Statuses.new(threshold: 1, decay_threshold: -1).refresh + end end end end diff --git a/spec/controllers/api/v1/trends/tags_controller_spec.rb b/spec/controllers/api/v1/trends/tags_controller_spec.rb index 84370d8412..9311392cd9 100644 --- a/spec/controllers/api/v1/trends/tags_controller_spec.rb +++ b/spec/controllers/api/v1/trends/tags_controller_spec.rb @@ -6,16 +6,41 @@ RSpec.describe Api::V1::Trends::TagsController do render_views describe 'GET #index' do - before do - Fabricate.times(10, :tag).each do |tag| - 10.times { |i| Trends.tags.add(tag, i) } - end - - get :index + around do |example| + previous = Setting.trends + example.run + Setting.trends = previous end - it 'returns http success' do - expect(response).to have_http_status(200) + context 'when trends are disabled' do + before { Setting.trends = false } + + it 'returns http success' do + get :index + + expect(response).to have_http_status(200) + expect(response.headers).to_not include('Link') + end + end + + context 'when trends are enabled' do + before { Setting.trends = true } + + it 'returns http success' do + prepare_trends + stub_const('Api::V1::Trends::TagsController::DEFAULT_TAGS_LIMIT', 2) + get :index + + expect(response).to have_http_status(200) + expect(response.headers).to include('Link') + end + + def prepare_trends + Fabricate.times(3, :tag, trendable: true).each do |tag| + 2.times { |i| Trends.tags.add(tag, i) } + end + Trends::Tags.new(threshold: 1).refresh + end end end end