A platform for community discussion. Free, open, simple.
PERF: Turn common test prefixes into contexts in topics_controller_spec (#15439)
Daniel Waterworth4a0f73a3377c
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 6653f9a18d4..0ecd4d6e0c5 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -3308,72 +3308,65 @@ RSpec.describe TopicsController doDismissedTopicUser.where(user_id: user.id, topic_id: tracked_topic.id).count}.by(1)end+ end- it "creates dismissed topic user records if there are > 30 (default pagination) topics" do- topic_ids = []- 5.times do- topic_ids << create_post(category: tracked_category).topic.id- end-- expect do- stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 2) do- put "/topics/reset-new.json?tracked=true"- end- end.to change {- DismissedTopicUser.where(user_id: user.id, topic_id: topic_ids).count- }.by(5)+ context "when 5 tracked topics exist" do+ before_all do+ @tracked_topic_ids = 5.times.map { create_post(category: tracked_category).topic.id }+ @tracked_topic_ids.freezeend- it "creates dismissed topic user records if there are > 30 (default pagination) topics and topic_ids are provided" do- topic_ids = []- 5.times do- topic_ids << create_post(category: tracked_category).topic.id+ describe "when tracked param is true" do+ it "creates dismissed topic user records if there are > 30 (default pagination) topics" do+ expect do+ stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 2) do+ put "/topics/reset-new.json?tracked=true"+ end+ end.to change {+ DismissedTopicUser.where(user_id: user.id, topic_id: @tracked_topic_ids).count+ }.by(5)end- dismissing_topic_ids = topic_ids.sample(4)- expect do- stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 2) do- put "/topics/reset-new.json?tracked=true", params: { topic_ids: dismissing_topic_ids }- end- end.to change {- DismissedTopicUser.where(user_id: user.id, topic_id: topic_ids).count- }.by(4)- end- end+ it "creates dismissed topic user records if there are > 30 (default pagination) topics and topic_ids are provided" do+ dismissing_topic_ids = @tracked_topic_ids.sample(4)- context "when tracked=false" do- it "updates the user_stat new_since column and dismisses all the new topics" do- topic_ids = []- 5.times do- topic_ids << create_post(category: tracked_category).topic.id+ expect do+ stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 2) do+ put "/topics/reset-new.json?tracked=true", params: { topic_ids: dismissing_topic_ids }+ end+ end.to change {+ DismissedTopicUser.where(user_id: user.id, topic_id: @tracked_topic_ids).count+ }.by(4)end- topic_ids << Fabricate(:topic).id- topic_ids << Fabricate(:topic).id- old_new_since = user.user_stat.new_since-- put "/topics/reset-new.json?tracked=false"- expect(DismissedTopicUser.where(user_id: user.id, topic_id: topic_ids).count).to eq(7)- expect(user.reload.user_stat.new_since > old_new_since).to eq(true)end- it "does not pass topic ids that are not new for the user to the bulk action, limit the scope to new topics" do-- topic_ids = []- 5.times do- topic_ids << create_post(category: tracked_category).topic.id+ context "when two extra topics exist" do+ before_all do+ @topic_ids = @tracked_topic_ids + [Fabricate(:topic).id, Fabricate(:topic).id]+ @topic_ids.freezeend- topic_ids << Fabricate(:topic).id- topic_ids << Fabricate(:topic).id- dismiss_ids = topic_ids[0..1]- other_ids = topic_ids[2..-1].sort.reverse+ context "when tracked=false" do+ it "updates the user_stat new_since column and dismisses all the new topics" do+ old_new_since = user.user_stat.new_since- DismissedTopicUser.create(user_id: user.id, topic_id: dismiss_ids.first)- DismissedTopicUser.create(user_id: user.id, topic_id: dismiss_ids.second)+ put "/topics/reset-new.json?tracked=false"+ expect(DismissedTopicUser.where(user_id: user.id, topic_id: @topic_ids).count).to eq(7)+ expect(user.reload.user_stat.new_since > old_new_since).to eq(true)+ end++ it "does not pass topic ids that are not new for the user to the bulk action, limit the scope to new topics" do+ dismiss_ids = @topic_ids[0..1]+ other_ids = @topic_ids[2..-1].sort.reverse++ DismissedTopicUser.create(user_id: user.id, topic_id: dismiss_ids.first)+ DismissedTopicUser.create(user_id: user.id, topic_id: dismiss_ids.second)- expect { put "/topics/reset-new.json?tracked=false" }.to change {- DismissedTopicUser.where(user_id: user.id).count- }.by(5)+ expect { put "/topics/reset-new.json?tracked=false" }.to change {+ DismissedTopicUser.where(user_id: user.id).count+ }.by(5)+ end+ endendend