discourse/plugins/chat/test/javascripts/acceptance/chat-email-preferences-test.js
Régis Hanol b66fca70d0
DEV: Add page-aware plugin APIs for saving user preferences (#36757)
Previously, plugins that needed to save user preferences on specific
preference pages had to either:

1. Manually register value transformers for
`preferences-save-attributes`
2. Use `modifyClass` to push fields to `saveAttrNames`

The second approach was broken because `saveAttrNames` is now a getter
that returns a fresh array each time, so pushed values were lost.

This commit introduces three new plugin APIs that handle the transformer
registration automatically:

- `addSaveableUserOption(name, { page })` - for `user_options` table
fields
- `addSaveableUserField(name, { page })` - for `user_fields` table
fields
- `addSaveableCustomFields(page)` - ensures `custom_fields` object is
saved on a page (auto-deduplicates across plugins)

The `{ page }` option specifies which preferences page triggers saving:
"account", "emails", "interface", "notifications", "profile",
"tracking", etc.

Also updates bundled plugins to use the new APIs:
- chat: `addSaveableUserOption` with `{ page: "emails" }` for email
frequency
- discourse-ai: `addSaveableUserOption` with `{ page: "interface" }`
- discourse-assign: `addSaveableUserOption` with `{ page: "tracking" }`,
`addSaveableCustomFields` with `"notifications"`
- discourse-policy: `addSaveableUserOption` with `{ page: "emails" }`
- discourse-rewind: `addSaveableUserOption` (no page, custom prefs page)

Deprecates `addSaveableUserOptionField` in favor of
`addSaveableUserOption`.

Ref - https://meta.discourse.org/t/391509

Follow up to ee1a1c7219
2025-12-18 10:58:16 +01:00

44 lines
1.1 KiB
JavaScript

import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import {
acceptance,
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
import selectKit from "discourse/tests/helpers/select-kit-helper";
acceptance("Chat | Email Preferences", function (needs) {
needs.user();
needs.settings({
chat_enabled: true,
});
let savedData;
needs.pretender((server, helper) => {
server.put("/u/eviltrout.json", (request) => {
savedData = helper.parsePostData(request.requestBody);
return helper.response({ user: {} });
});
});
test("saves chat_email_frequency when saving email preferences", async function (assert) {
updateCurrentUser({
user_option: {
chat_email_frequency: "when_away",
},
});
await visit("/u/eviltrout/preferences/emails");
const dropdown = selectKit("#user_chat_email_frequency");
await dropdown.expand();
await dropdown.selectRowByValue("never");
await click(".save-changes");
assert.strictEqual(
savedData.chat_email_frequency,
"never",
"chat_email_frequency is included in saved data"
);
});
});