2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/spec/serializers/theme_objects_setting_metadata_serializer_spec.rb
Kris fb26fc66a4
FIX: support nested descriptions in object settings (#37538)
Reported here:
https://meta.discourse.org/t/labels-and-descriptions-missing-from-nested-object-settings/394685

Descriptions for nested object settings were not being displayed due to
a mismatch in locale key formatting. Stripping `.schema.properties.` so
that the locale keys from the serializer match the keys expected in the
template fixes it

Before: 
<img width="600" alt="image"
src="https://github.com/user-attachments/assets/1e0ab4c6-e6eb-478d-97bc-76f16739135b"
/>


After: 
<img width="600" alt="image"
src="https://github.com/user-attachments/assets/a9a9e64c-4efa-4807-94a1-dd853ffd9f04"
/>
2026-02-05 14:01:58 -05:00

99 lines
3.1 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ThemeObjectsSettingMetadataSerializer do
fab!(:theme)
let(:theme_setting) do
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/objects_settings.yaml")
theme.set_field(target: :settings, name: "yaml", value: yaml)
theme.save!
theme.settings
end
describe "#property_descriptions" do
let(:objects_setting_locale) do
theme.set_field(
target: :translations,
name: "en",
value: File.read("#{Rails.root}/spec/fixtures/theme_locales/objects_settings/en.yaml"),
)
theme.save!
end
it "should return a hash of the settings property descriptions with schema.properties segments stripped" do
objects_setting_locale
payload = described_class.new(theme_setting[:objects_setting], root: false).as_json
expect(payload[:property_descriptions]).to eq(
{
"links.child_links.title.description" => "Title of the child link",
"links.child_links.title.label" => "Title",
"links.name.description" => "Name of the link",
"links.name.label" => "Name",
"links.url.description" => "URL of the link",
"links.url.label" => "URL",
"name.description" => "Section Name",
"name.label" => "Name",
},
)
expect(payload[:property_descriptions]).not_to have_key("links.schema.properties.name.label")
end
end
describe "#categories" do
fab!(:category_1, :category)
fab!(:category_2, :category)
fab!(:category_3) { Fabricate(:private_category, group: Fabricate(:group)) }
fab!(:admin)
it "should return a hash of serialized categories" do
theme_setting[:objects_with_categories].value = [
{
"category_ids" => [category_1.id, category_2.id],
"child_categories" => [{ "category_ids" => [category_3.id] }],
},
]
scope = Guardian.new
payload =
described_class.new(theme_setting[:objects_with_categories], scope:, root: false).as_json
categories = payload[:categories]
expect(categories.keys).to contain_exactly(category_1.id, category_2.id)
expect(categories[category_1.id]).to eq(
BasicCategorySerializer.new(category_1, scope:, root: false).as_json,
)
expect(categories[category_2.id]).to eq(
BasicCategorySerializer.new(category_2, scope:, root: false).as_json,
)
scope = Guardian.new(admin)
payload =
described_class.new(theme_setting[:objects_with_categories], scope:, root: false).as_json
categories = payload[:categories]
expect(categories.keys).to contain_exactly(category_1.id, category_2.id, category_3.id)
expect(categories[category_1.id]).to eq(
BasicCategorySerializer.new(category_1, scope:, root: false).as_json,
)
expect(categories[category_2.id]).to eq(
BasicCategorySerializer.new(category_2, scope:, root: false).as_json,
)
expect(categories[category_3.id]).to eq(
BasicCategorySerializer.new(category_3, scope:, root: false).as_json,
)
end
end
end