docbrown/spec/queries/articles/active_threads_query_spec.rb
Julianna Tetreault 849bb846ac
[15-Minute Fix] Sidebar Tags Show Relevant Posts on Homepage (#13326)
* Revert change to options hash within Articles::ActiveThreadsQuery#call
  - Reverts double splat change back to options hash
  - Reverts changes to active_threads_query_spec.rb options

* Adds another check for tags to Articles::ActiveThreadsQuery#call
  - Adds a .tags.present? check to #call
  - Removes redundant and broken relation from #call

* Reverts change to options hash within _sidebar_additional.html.erb

* Removes before block from active_threads_query_spec

* Moves tag filter before conditional in Articles::ActiveThreadsQuery#call
  - Adds before block back to active_threads_query_spec for proper
  testing of filtering of tags within spec

* Adjust options to use new kwargs in Articles::ActiveThreadsQuery
  - Adjust active_threads_query_spec to use new args
  - Remove useless code from Articles::ActiveThreadsQuery

* Adjust published_at in else block
2021-04-09 10:40:09 -06:00

76 lines
3.6 KiB
Ruby

require "rails_helper"
RSpec.describe Articles::ActiveThreadsQuery, type: :query do
before do
create(:article, score: described_class::MINIMUM_SCORE - 1, tags: "watercooler")
end
describe "::call" do
context "when time_ago is latest" do
it "returns the latest article with a good score", :aggregate_failures do
article = create(:article, tags: "discuss", score: described_class::MINIMUM_SCORE + 1)
create(:article, published_at: 2.days.ago, tags: "discuss", score: described_class::MINIMUM_SCORE + 1)
result = described_class.call(tags: "discuss", time_ago: "latest", count: 10)
expect(result.length).to eq(2)
expect(result.first.first).to eq(article.path)
end
it "returns any article if no higher-quality article is found", :aggregate_failures do
article = create(:article, tags: "discuss", score: described_class::MINIMUM_SCORE - 10)
result = described_class.call(tags: "discuss", time_ago: "latest", count: 10)
expect(result.length).to eq(1)
expect(result.first.first).to eq(article.path)
end
end
context "when given a precise time" do
it "returns article ordered_by comment_count based on time", :aggregate_failures do
time = 2.days.ago
article = create(:article, comments_count: 20, published_at: time, tags: "discuss",
score: described_class::MINIMUM_SCORE)
create(:article, comments_count: 10, tags: "discuss", score: described_class::MINIMUM_SCORE)
create(:article, published_at: time - 2.days, comments_count: 30, tags: "discuss",
score: described_class::MINIMUM_SCORE)
result = described_class.call(tags: "discuss", time_ago: time, count: 10)
expect(result.length).to eq(2)
expect(result.first.first).to eq(article.path)
end
it "returns any article when no higher-quality article could be found", :aggregate_failures do
time = 2.days.ago
article = create(:article, comments_count: 20, published_at: time - 5.days, tags: "discuss",
score: described_class::MINIMUM_SCORE)
create(:article, comments_count: 10, tags: "discuss", score: described_class::MINIMUM_SCORE - 10)
result = described_class.call(tags: "discuss", time_ago: time, count: 10)
expect(result.length).to eq(2)
expect(result.first.first).to eq(article.path)
end
end
context "when time_ago is not given" do
it "returns articles ordered by last_comment_at, not based on time", :aggregate_failures do
article = create(:article, last_comment_at: Time.zone.now, tags: "discuss",
score: described_class::MINIMUM_SCORE)
create(:article, last_comment_at: nil, tags: "discuss", score: described_class::MINIMUM_SCORE)
result = described_class.call(tags: "discuss", time_ago: nil, count: 10)
expect(result.length).to eq(2)
expect(result.first.first).to eq(article.path)
end
it "returns any article, with nil articles last, if no higher-quality articles exist", :aggregate_failures do
article = create(:article, last_comment_at: Time.zone.now, tags: "discuss",
score: described_class::MINIMUM_SCORE - 10)
create(:article, last_comment_at: 2.days.ago, tags: "discuss", score: described_class::MINIMUM_SCORE - 10)
result = described_class.call(tags: "discuss", time_ago: nil, count: 10)
expect(result.length).to eq(2)
expect(result.first.first).to eq(article.path)
end
end
end
end