docbrown/spec/labor/article_analytics_fetcher_spec.rb
Mac Siri 73127aca01 Remove controller specs (#2224)
* Create more request specs

* Update wording

* Update guard to work with requests

* Fix broken spec

* Remove rails-controller-testing gem

* Move article_specs

* Move last controller spec out

* Remove controller spec config

* Fix broken spec

* Remove all use of render_template

* Refactor spec

* Tweak User#remember_me

* Create ArticleAnalyticFetcher spec

* Undo User#remember_me

* Use srand in rspec

* Create FeedbackMessagesHelper spec

* Add DigestMailer spec

* Fix broken test
2019-03-29 19:05:02 -04:00

47 lines
1.8 KiB
Ruby

require "rails_helper"
RSpec.describe ArticleAnalyticsFetcher do
let(:stubbed_ga) { double }
before do
srand(2) # disabling #occasionally_force_fetch
allow(Notification).to receive(:send_milestone_notification)
allow(GoogleAnalytics).to receive(:new).and_return(stubbed_ga)
end
describe "#update_analytics" do
context "when positive_reactions_count is LOWER than previous_positive_reactions_count" do
it "does nothing " do
article = build_stubbed(:article, positive_reactions_count: 2, previous_positive_reactions_count: 3)
allow(Article).to receive(:where).and_return([article])
described_class.new.update_analytics(1)
expect(Notification).not_to have_received(:send_milestone_notification)
end
end
context "when positive_reactions_count is HIGHER than previous_positive_reactions_count" do
let(:article) { build_stubbed(:article, positive_reactions_count: 5, previous_positive_reactions_count: 3) }
let(:pageview) { {} }
let(:counts) { 1000 }
before do
pageview[article.id] = counts
allow(stubbed_ga).to receive(:get_pageviews).and_return(pageview)
allow(article).to receive(:update_columns)
allow(Article).to receive(:where).and_return([article])
described_class.new.update_analytics(1)
end
it "sends send_milestone_notification for Reaction and View" do
%w[Reaction View].each do |type|
expect(Notification).to have_received(:send_milestone_notification).with(type: type, article: article)
end
end
it "updates appropriate column" do
expect(article).to have_received(:update_columns).with(previous_positive_reactions_count: article.positive_reactions_count)
expect(article).to have_received(:update_columns).with(page_views_count: counts)
end
end
end
end