From 3629b2de1b731fe90ffbc3648cb25744e46fbdbf Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Thu, 6 Oct 2022 15:58:55 +0800 Subject: [PATCH] DEV: Add tests for `SiteSerializer#top_tags` (#18498) --- spec/serializers/site_serializer_spec.rb | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rb index 2839ac774ef..02d39afc690 100644 --- a/spec/serializers/site_serializer_spec.rb +++ b/spec/serializers/site_serializer_spec.rb @@ -135,4 +135,52 @@ RSpec.describe SiteSerializer do serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json expect(serialized[:anonymous_default_sidebar_tags]).to eq(["dev", "random"]) end + + describe '#top_tags' do + fab!(:tag) { Fabricate(:tag) } + + describe 'when tagging is not enabled' do + before do + SiteSetting.tagging_enabled = false + end + + it 'is not included in the serialised object' do + serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json + + expect(serialized[:top_tags]).to eq(nil) + end + end + + describe 'when tagging is enabled' do + fab!(:tag2) { Fabricate(:tag) } + fab!(:tag3) { Fabricate(:tag) } + + before do + SiteSetting.tagging_enabled = true + end + + it 'is not included in the serialised object when there are no tags' do + tag.destroy! + + serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json + + expect(serialized[:top_tags]).to eq([]) + end + + it 'is included in the serialised object containing the top tags' do + tag2 = Fabricate(:tag) + tag2 = Fabricate(:tag) + + SiteSetting.max_tags_in_filter_list = 1 + + CategoryTagStat.create!(category_id: SiteSetting.uncategorized_category_id, tag_id: tag2.id, topic_count: 2) + CategoryTagStat.create!(category_id: SiteSetting.uncategorized_category_id, tag_id: tag.id, topic_count: 1) + CategoryTagStat.create!(category_id: SiteSetting.uncategorized_category_id, tag_id: tag3.id, topic_count: 5) + + serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json + + expect(serialized[:top_tags]).to eq([tag3.name, tag2.name]) + end + end + end end