Repository: exadel-inc/esl
Pull request #2118 (2023-12-14 02:14:13)

feat(esl-share): add ability to update config items (single one or in batch)


Patch:
diff --git a/src/modules/esl-share/core/esl-share-config.ts b/src/modules/esl-share/core/esl-share-config.ts
index 7a6945a2c..626d2fd2f 100644
--- a/src/modules/esl-share/core/esl-share-config.ts
+++ b/src/modules/esl-share/core/esl-share-config.ts
@@ -77,6 +77,11 @@ export class ESLShareConfig extends SyntheticEventTarget {
     return ESLShareConfig.instance;
   }
 
+  /** Updates items by the name and passed changes */
+  public static update(name: string, changes: Partial<ESLShareButtonConfig>): ESLShareConfig {
+    return ESLShareConfig.instance.update(name, changes);
+  }
+
   /** Appends single button or group to current configuration */
   public static append(cfg: ESLShareButtonConfig | ESLShareGroupConfig | ESLShareButtonConfig[] | ESLShareGroupConfig[]): ESLShareConfig {
     return ESLShareConfig.instance.append(cfg);
@@ -168,6 +173,14 @@ export class ESLShareConfig extends SyntheticEventTarget {
     return this;
   }
 
+  /** Updates items by the name and passed changes */
+  public update(name: string, changes: Partial<ESLShareButtonConfig>): ESLShareConfig {
+    for (const btn of this.get(name)) {
+      this.append(Object.assign({}, btn, changes));
+    }
+    return this;
+  }
+
   @decorate(microtask)
   protected _onUpdate(): void {
     this.dispatchEvent(new CustomEvent('change'));


Test patch:
diff --git a/src/modules/esl-share/test/esl-share-config.test.ts b/src/modules/esl-share/test/esl-share-config.test.ts
index 92ee879ec..b2d92172b 100644
--- a/src/modules/esl-share/test/esl-share-config.test.ts
+++ b/src/modules/esl-share/test/esl-share-config.test.ts
@@ -155,7 +155,6 @@ describe('ESLShareConfig tests', () => {
     });
   });
 
-
   describe('ESLShareConfig notify about changes', () => {
     const instance: ESLShareConfig = new (ESLShareConfig as any)();
     const callback = jest.fn();
@@ -228,7 +227,7 @@ describe('ESLShareConfig tests', () => {
     });
   });
 
-  describe('ESLShareConfig static API', () => {
+  describe('ESLShareConfig.set static API', () => {
     test('ESLShareConfig instance is a singleton', () => {
       expect(ESLShareConfig.instance).toBeInstanceOf(ESLShareConfig);
       expect(ESLShareConfig.instance).toBe(ESLShareConfig.instance);
@@ -266,4 +265,40 @@ describe('ESLShareConfig tests', () => {
       expect(ESLShareConfig.instance.groups.length).toBe(1);
     });
   });
+
+  describe('ESLShareConfig.update', () => {
+    const instance: ESLShareConfig = new (ESLShareConfig as any)();
+
+    beforeEach(async () => {
+      instance.clear();
+      await Promise.resolve();
+    });
+
+    test('ESLShareConfig.update does not fail if no items with passed name', () => {
+      instance.append([SAMPLE_BUTTON_1, SAMPLE_BUTTON_2]);
+      instance.update('sn3', {title: 'SN3'});
+      expect(instance.buttons).toEqual([SAMPLE_BUTTON_1, SAMPLE_BUTTON_2]);
+    });
+
+    test('ESLShareConfig.update change a single button with the passed name', () => {
+      instance.append([SAMPLE_BUTTON_1, SAMPLE_BUTTON_2]);
+      instance.update('sn2', {title: 'SN2Updated', link: '#'});
+      expect(instance.get('sn2')).toEqual([expect.objectContaining({...SAMPLE_BUTTON_2, title: 'SN2Updated', link: '#'})]);
+    });
+
+    test('ESLShareConfig.update updates a group of items', () => {
+      instance.append([SAMPLE_BUTTON_1, SAMPLE_BUTTON_2]);
+      instance.append(SAMPLE_GROUP_1);
+      instance.update(SAMPLE_GROUP_1.name, {link: '#'});
+      for (const btn of instance.get(SAMPLE_GROUP_1.name)) {
+        expect(btn.link).toBe('#');
+      }
+    });
+
+    test('ESLShareConfig.update does not introduce new items', () => {
+      instance.append([SAMPLE_BUTTON_1, SAMPLE_BUTTON_2]);
+      instance.update('sn1', {title: 'SN3'});
+      expect(instance.buttons.length).toEqual(2);
+    });
+  });
 });