Skip to content

Commit

Permalink
feat(esl-share): add ability to update config items (single one or in…
Browse files Browse the repository at this point in the history
… batch)
  • Loading branch information
ala-n committed Dec 14, 2023
1 parent 27bd588 commit 96eac6b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/modules/esl-share/core/esl-share-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export class ESLShareConfig extends SyntheticEventTarget {
return ESLShareConfig.instance;
}

/** Update items by the name and passed changes */
public static update(name: string, cfg: Partial<ESLShareButtonConfig>): ESLShareConfig {
return ESLShareConfig.instance.update(name, cfg);
}

/** Appends single button or group to current configuration */
public static append(cfg: ESLShareButtonConfig | ESLShareGroupConfig | ESLShareButtonConfig[] | ESLShareGroupConfig[]): ESLShareConfig {
return ESLShareConfig.instance.append(cfg);
Expand Down Expand Up @@ -168,6 +173,14 @@ export class ESLShareConfig extends SyntheticEventTarget {
return this;
}

/** Update items by the name and passed changes */
public update(name: string, cfg: Partial<ESLShareButtonConfig>): ESLShareConfig {
for (const btn of this.get(name)) {
this.append(Object.assign({}, btn, cfg));
}
return this;
}

@decorate(microtask)
protected _onUpdate(): void {
this.dispatchEvent(new CustomEvent('change'));
Expand Down
39 changes: 37 additions & 2 deletions src/modules/esl-share/test/esl-share-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ describe('ESLShareConfig tests', () => {
});
});


describe('ESLShareConfig notify about changes', () => {
const instance: ESLShareConfig = new (ESLShareConfig as any)();
const callback = jest.fn();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 96eac6b

Please sign in to comment.