2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/spec/requests/exceptions_controller_spec.rb
Régis Hanol 3dfa25bc41
FIX: Respect forced color mode for logo on server-rendered pages (#37032)
When visiting pages that use the server-rendered header (404, password
reset, invites, etc.), the logo was always using the OS/browser
`prefers-color-scheme` preference instead of respecting the user's
explicit choice made via the interface color selector.

This happened because the `<source>` media query was hardcoded to
`(prefers-color-scheme: dark)`, ignoring the `forced_color_mode` cookie.

Now we use the existing `dark_elements_media_query` helper which returns
the appropriate value based on the user's preference:
- "none" when light mode is forced (hides dark logo)
- "all" when dark mode is forced (shows dark logo)
- "(prefers-color-scheme: dark)" for auto mode (respects OS)

This matches the behavior already implemented for the splash screen.

Ref - t/172056

**BEFORE**

<img width="1529" height="1220" alt="2026-01-09 @ 09 32 28"
src="https://github.com/user-attachments/assets/89c04e53-1360-442f-af27-af0ddc1f526c"
/>

**AFTER**

<img width="1529" height="1220" alt="2026-01-09 @ 09 32 19"
src="https://github.com/user-attachments/assets/75825cd1-9374-4e2f-b98b-e5ba5e4d72f0"
/>
2026-01-09 11:33:06 +01:00

64 lines
1.8 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ExceptionsController do
describe "#not_found" do
it "should return the right response" do
get "/404"
expect(response.status).to eq(404)
expect(response.body).to have_tag(
"title",
text: "#{I18n.t("page_not_found.page_title")} - #{SiteSetting.title}",
)
expect(response.body).to have_tag("img", with: { src: SiteSetting.site_logo_url })
end
describe "text site logo" do
before { SiteSetting.logo = "" }
it "should return the right response" do
get "/404"
expect(response.status).to eq(404)
expect(response.body).to have_tag("h2", text: SiteSetting.title)
end
end
describe "logo dark mode media query" do
fab!(:upload_light, :upload)
fab!(:upload_dark, :upload)
let(:light_scheme_id) { ColorScheme::NAMES_TO_ID_MAP["Solarized Light"] }
let(:dark_scheme_id) { ColorScheme::NAMES_TO_ID_MAP["Dark"] }
before do
SiteSetting.logo = upload_light
SiteSetting.logo_dark = upload_dark
SiteSetting.interface_color_selector = "sidebar_footer"
color_scheme_id = ColorScheme.where(base_scheme_id: light_scheme_id).pick(:id)
dark_color_scheme_id = ColorScheme.where(base_scheme_id: dark_scheme_id).pick(:id)
Theme.find_default.update!(color_scheme_id:, dark_color_scheme_id:)
end
{
nil => "(prefers-color-scheme: dark)",
"auto" => "(prefers-color-scheme: dark)",
"light" => "none",
"dark" => "all",
}.each do |cookie, media|
it "uses media=#{media.inspect} when forced_color_mode is #{cookie.inspect}" do
cookies[:forced_color_mode] = cookie
get "/404"
expect(response.body).to have_tag("source", with: { media: })
end
end
end
end
end