2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/system/admin_email_templates_spec.rb
Régis Hanol 5f6e69f64f
UX: Display interpolation keys as interactive pills in admin editors (#37254)
When editing site texts or email templates, admins need to know which
interpolation keys are available and whether they're being used
correctly.
Previously there was no UI for this.

Show interpolation keys as clickable pill buttons below the editor:
- Unused keys are dimmed — clicking inserts %{key} at the last cursor
position
- Used keys are highlighted green to confirm they're present in the text
- Invalid keys (typos or unknown keys) appear in red as non-clickable
pills

Implementation:
- New `<AdminInterpolationKeys>` template-only component renders the
pills
- New `interpolationKeysWithStatus()` utility in admin/lib centralizes
the
logic for computing key statuses (used/unused/invalid) across both
editors
- Controllers track textarea focus and cursor position via private
fields
  (no @Tracked — these are only read imperatively on pill click)
- Uses document.execCommand("insertText") for native undo/redo support
- Fix duplicate keys in backend by using Array#| (set union) instead of
Array#+

<img width="1662" height="1355" alt="2026-02-26 @ 17 09 32"
src="https://github.com/user-attachments/assets/05497832-4e17-4eb2-b4c1-e9e0e036304a"
/>

<img width="1662" height="1355" alt="2026-02-26 @ 17 09 19"
src="https://github.com/user-attachments/assets/f13271ad-511e-4fa1-9bec-eb6fbc7a66d8"
/>


Ref - t/172375
2026-02-27 21:21:26 +01:00

66 lines
2.5 KiB
Ruby

# frozen_string_literal: true
describe "Admin Email Templates", type: :system do
fab!(:admin)
let(:email_templates_page) { PageObjects::Pages::AdminEmailTemplates.new }
let(:composer) { PageObjects::Components::Composer.new(".email-template__body") }
before { sign_in(admin) }
it "forces markdown mode and doesn't allow the user to toggle the rich text editor" do
email_templates_page.visit_template("user_notifications.account_exists")
expect(composer).to have_markdown_editor_active
expect(composer).to have_no_toggle_switch
end
it "can edit an email template" do
email_templates_page.visit_template("user_notifications.account_exists")
subject_text = "Modified test subject #{SecureRandom.hex(8)}"
email_templates_page.edit_subject(subject_text)
body_text =
"This is a modified test body with some **markdown** formatting #{SecureRandom.hex(8)}"
email_templates_page.edit_body(body_text)
expect(email_templates_page).to have_preview_content(
"This is a modified test body with some markdown formatting",
)
expect(page).to have_css(".d-editor-preview strong", text: "markdown")
email_templates_page.save_changes
expect(page).to have_css(".save-button .saved")
email_templates_page.visit_template("user_notifications.account_exists")
expect(email_templates_page).to have_subject_value(subject_text)
expect(composer).to have_value(body_text)
end
it "shows link to site texts for template with multiple subjects" do
email_templates_page.visit_template("system_messages.pending_users_reminder")
expect(email_templates_page).to have_multiple_subjects_link(
"#{Discourse.base_url}/admin/customize/site_texts?q=system_messages.pending_users_reminder",
)
end
it "shows link to site texts for template with multiple bodies" do
email_templates_page.visit_template("system_messages.reviewables_reminder")
expect(email_templates_page).to have_multiple_bodies_link(
"#{Discourse.base_url}/admin/customize/site_texts?q=system_messages.reviewables_reminder",
)
end
it "shows interpolation keys for templates that have them" do
email_templates_page.visit_template("user_notifications.admin_login")
expect(email_templates_page).to have_interpolation_keys(
%w[base_url email_prefix email_token site_name],
)
end
it "does not show interpolation keys for templates without any" do
email_templates_page.visit_template("system_messages.download_remote_images_disabled")
expect(email_templates_page).to have_no_interpolation_keys
end
end