Bläddra i källkod

merge only relevant settings when calling api.settings.extend (#2187)

* merge only relevant settings when calling api.settings.extend
* test behaviour is the one expected and change doesn't break previous tests
Xavi 4 år sedan
förälder
incheckning
7cdc592ed9
3 ändrade filer med 24 tillägg och 1 borttagningar
  1. 1 0
      CHANGES.md
  2. 22 0
      spec/converse.js
  3. 1 1
      src/headless/converse-core.js

+ 1 - 0
CHANGES.md

@@ -26,6 +26,7 @@ Soon we'll deprecate the latter, so prepare now.
 - #2006: fix rendering of emojis in case `use_system_emojis == false`
 - #2028: Implement XEP-0333 `displayed` chat marker
 - #2101: Improve contrast of text in control box
+- #2187: Avoid merging initial settings with themselves every time settings are extended.
 - Removed the mockups from the project. Recommended to use tests instead.
 - The API method `api.settings.update` has been deprecated in favor of `api.settings.extend`.
 - Filter roster contacts via all available information (JID, nickname and VCard full name).

+ 22 - 0
spec/converse.js

@@ -353,6 +353,28 @@ describe("Converse", function() {
             expect(_converse.api.settings.get('emoji_categories')?.food).toBe(undefined);
             done();
         }));
+        
+        it("only overrides the passed in properties",
+                mock.initConverse([],
+                {
+                    'root': document.createElement('div').attachShadow({ 'mode': 'open' }),
+                    'emoji_categories': { 'travel': ':rocket:' },
+                },
+                (done, _converse) => {
+                    expect(_converse.api.settings.get('emoji_categories')?.travel).toBe(':rocket:');
+
+                    // Test that the extend command doesn't override user-provided site
+                    // settings (i.e. settings passed in via converse.initialize).
+                    _converse.api.settings.extend({
+                        'emoji_categories': { 'travel': ':motorcycle:', 'food': ':burger:' },
+                    });
+
+                    expect(_converse.api.settings.get('emoji_categories').travel).toBe(':rocket:');
+                    expect(_converse.api.settings.get('emoji_categories').food).toBe(undefined);
+                    done();
+                }
+            )
+        );
 
     });
 

+ 1 - 1
src/headless/converse-core.js

@@ -659,7 +659,7 @@ export const api = _converse.api = {
             u.merge(DEFAULT_SETTINGS, settings);
             // When updating the settings, we need to avoid overwriting the
             // initialization_settings (i.e. the settings passed in via converse.initialize).
-            const allowed_keys = Object.keys(DEFAULT_SETTINGS);
+            const allowed_keys = Object.keys(pick(settings,Object.keys(DEFAULT_SETTINGS)));
             const allowed_site_settings = pick(initialization_settings, allowed_keys);
             const updated_settings = assignIn(pick(settings, allowed_keys), allowed_site_settings);
             u.merge(_converse.settings, updated_settings);