discourse/spec/serializers/category_detailed_serializer_spec.rb
David Taylor c9dab6fd08
DEV: Automatically require 'rails_helper' in all specs (#16077)
It's very easy to forget to add `require 'rails_helper'` at the top of every core/plugin spec file, and omissions can cause some very confusing/sporadic errors.

By setting this flag in `.rspec`, we can remove the need for `require 'rails_helper'` entirely.
2022-03-01 17:50:50 +00:00

25 lines
964 B
Ruby

# frozen_string_literal: true
describe CategoryDetailedSerializer do
describe "counts" do
it "works for categories with no subcategories" do
no_subcats = Fabricate(:category, topics_year: 10, topics_month: 5, topics_day: 2)
json = CategoryDetailedSerializer.new(no_subcats, scope: Guardian.new, root: false).as_json
expect(json[:topics_year]).to eq(10)
expect(json[:topics_month]).to eq(5)
expect(json[:topics_day]).to eq(2)
end
it "includes counts from subcategories" do
parent = Fabricate(:category, topics_year: 10, topics_month: 5, topics_day: 2)
subcategory = Fabricate(:category, parent_category_id: parent.id, topics_year: 1, topics_month: 1, topics_day: 1)
json = CategoryDetailedSerializer.new(parent, scope: Guardian.new, root: false).as_json
expect(json[:topics_year]).to eq(11)
expect(json[:topics_month]).to eq(6)
expect(json[:topics_day]).to eq(3)
end
end
end