discourse-rewind/spec/services/fetch_reports_spec.rb
Martin Brennan 26a77d4c69
DEV: Adding specs and refactors (#21)
Add specs for the following reports:

* Reading time
* Activity calendar
* Best posts
* Best topics
* Top words
* Most viewed tags
* Most viewed categories

Did some minor UI and ruby refactors for related components.

Also made a minor change to the Activity calendar, to show a title based
on the number of posts or if the user was active on hover.

Still missing specs for:

* Reactions
* FBFF

And the newly added reports that don't yet have UI components.
2025-12-01 10:08:30 +10:00

72 lines
2.0 KiB
Ruby

# frozen_string_literal: true
RSpec.describe(DiscourseRewind::FetchReports) do
describe ".call" do
subject(:result) { described_class.call(**dependencies) }
fab!(:current_user, :user)
let(:guardian) { Guardian.new(current_user) }
let(:dependencies) { { guardian: } }
context "when in january" do
before { freeze_time DateTime.parse("2021-01-22") }
it "computes the correct previous year" do
expect(result.year).to eq(2020)
end
end
context "when in december" do
before { freeze_time DateTime.parse("2021-12-22") }
it "computes the correct previous year" do
expect(result.year).to eq(2021)
end
end
context "when out of valid months december" do
before { freeze_time DateTime.parse("2021-02-22") }
it { is_expected.to fail_to_find_a_model(:year) }
end
context "in development mode" do
before do
Rails.env.stubs(:development?).returns(true)
freeze_time DateTime.parse("2021-06-22")
end
it "finds the year no matter what month" do
expect(result.year).to eq(2021)
end
end
context "when reports is cached" do
before { freeze_time DateTime.parse("2021-12-22") }
it "returns the cached reports" do
initial_count = result.reports.length
expect(initial_count).to be > 0
allow(DiscourseRewind::Action::TopWords).to receive(:call)
expect(result.reports.length).to eq(initial_count)
expect(DiscourseRewind::Action::TopWords).to_not have_received(:call)
end
end
context "when reports is not cached" do
before do
freeze_time DateTime.parse("2021-01-22")
Discourse.redis.del("rewind:#{current_user.username}:2020")
end
it "returns the reports" do
allow(DiscourseRewind::Action::TopWords).to receive(:call)
expect(result.reports.length).to be > 0
expect(DiscourseRewind::Action::TopWords).to have_received(:call)
end
end
end
end