discourse/spec/serializers/theme_serializer_spec.rb
Martin Brennan 57fea290ef
DEV: Use themeable site settings for Horizon (#33645)
This commit removes the value transformers introduced
to Horizon back in
274e5f7a1f
and
897d34132e
in favor of the new themeable site settings introduced in
19af83d39e , as this is what they
are for.

No migration for existing sites...they will already have
ThemeSiteSetting values from a previous migration to ensure
site setting values were preserved in theme site settings.

We do delete the ThemeField storing the Horizon custom theme
setting definition though, otherwise the UI still shows the old
settings.
2025-07-24 12:20:34 +10:00

97 lines
3.3 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe ThemeSerializer do
describe ".settings" do
fab!(:theme)
it "should add error message when settings format is invalid" do
Theme
.any_instance
.stubs(:settings)
.raises(ThemeSettingsParser::InvalidYaml, I18n.t("themes.settings_errors.invalid_yaml"))
serialized = ThemeSerializer.new(theme).as_json[:theme]
expect(serialized[:settings]).to be_nil
expect(serialized[:errors].count).to eq(1)
expect(serialized[:errors][0]).to eq(I18n.t("themes.settings_errors.invalid_yaml"))
end
it "should add errors messages from theme fields" do
error = "error when compiling theme field"
theme_field = Fabricate(:theme_field, error: error, theme: theme)
serialized = ThemeSerializer.new(theme.reload).as_json[:theme]
expect(serialized[:errors].count).to eq(1)
expect(serialized[:errors][0]).to eq(error)
end
end
describe "screenshot_url" do
fab!(:theme)
let(:serialized) { ThemeSerializer.new(theme.reload).as_json[:theme] }
it "should include screenshot_url when there is a theme field with screenshot upload type" do
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:theme_screenshot_upload_var],
name: "theme_screenshot_1",
upload: Fabricate(:upload),
)
expect(serialized[:screenshot_url]).to be_present
end
it "should not include screenshot_url when there is no theme field with screenshot upload type" do
expect(serialized[:screenshot_url]).to be_nil
end
it "should handle multiple screenshot fields and use the first one" do
first_upload = Fabricate(:upload)
second_upload = Fabricate(:upload)
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:theme_screenshot_upload_var],
name: "theme_screenshot_1",
upload: first_upload,
)
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:theme_screenshot_upload_var],
name: "theme_screenshot_2",
upload: second_upload,
)
expect(serialized[:screenshot_url]).to eq(first_upload.url)
end
end
describe ".themeable_site_settings" do
fab!(:theme)
it "returns the themeable_site_settings with defaults and values converted to string" do
serialized = ThemeSerializer.new(theme).as_json[:theme]
SiteSetting.themeable_site_settings.each do |setting|
found_tss = serialized[:themeable_site_settings].find { |tss| tss[:setting] == setting }
expect(found_tss[:value]).to eq(SiteSetting.defaults[setting.name].to_s)
expect(found_tss[:default]).to eq(SiteSetting.defaults[setting.name].to_s)
end
end
it "returns the correct value for overridden theme site settings" do
Fabricate(
:theme_site_setting_with_service,
name: "enable_welcome_banner",
value: false,
theme: theme,
)
serialized = ThemeSerializer.new(theme).as_json[:theme]
found_tss =
serialized[:themeable_site_settings].find { |tss| tss[:setting] == :enable_welcome_banner }
expect(found_tss[:value]).to eq("false")
expect(found_tss[:default]).to eq(SiteSetting.defaults[:enable_welcome_banner].to_s)
end
end
end