discourse/spec/serializers/theme_objects_setting_metadata_serializer_spec.rb
Alan Guo Xiang Tan 6dac187785
DEV: Support translations for property labels in objects schema editor (#26362)
Why this change?

In cdba86459877987f52def4bcfeba5c7bef7de407, we added support for adding
a description which will be displayed under the input of each property
on the client side.

Currently this convention in the locale file is followed:

```
en:
  theme_metadata:
    settings:
      objects_setting:
        description: <description> for the setting
        schema:
          properties:
            name: <description for the name property>
            links:
              name: <description for the name property in link>
              url: <description for the url property in link>
```

Since we now want to allow the label to be translated as well, we will
be changing the convention to the following:

```
en:
  theme_metadata:
    settings:
      objects_setting:
        description: <description> for the setting
        schema:
          properties:
            name:
              label: <label for the name property>
              description: <description for the name property>
            links:
              name:
                label: <label for the name property>
                description: <description for the name property in link>
              url:
		label: <label for the url property>
                description: <description for the url property in link>
```

If the locale file does not provide a `label` key under the property's
name, the client side will just display the property's name as the
label for the input field.
2024-03-28 10:53:51 +08:00

98 lines
2.9 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
before { SiteSetting.experimental_objects_type_for_theme_settings = true }
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" do
objects_setting_locale
payload = described_class.new(theme_setting[:objects_setting], root: false).as_json
expect(payload[:property_descriptions]).to eq(
{
"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",
},
)
end
end
describe "#categories" do
fab!(:category_1) { Fabricate(:category) }
fab!(:category_2) { Fabricate(: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