Skip to content

Commit ce4e6bc

Browse files
committed
Allow updates of CompositeMenuNode properties (eclipse-theia#12948)
- refactor updateOptions function to allow updates - add a unit test cases for updateOptions Fixes eclipse-theia#12948
1 parent 9348727 commit ce4e6bc

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// *****************************************************************************
2+
// Copyright (C) 2024 EclipseSource and others.
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License v. 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0.
7+
//
8+
// This Source Code may also be made available under the following Secondary
9+
// Licenses when the conditions for such availability set forth in the Eclipse
10+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
// with the GNU Classpath Exception which is available at
12+
// https://www.gnu.org/software/classpath/license.html.
13+
//
14+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15+
// *****************************************************************************
16+
import * as chai from 'chai';
17+
import { CompositeMenuNode } from './composite-menu-node';
18+
import { CompoundMenuNodeRole } from './menu-types';
19+
20+
const expect = chai.expect;
21+
22+
describe('composite-menu-node', () => {
23+
describe('updateOptions', () => {
24+
it('should update undefined node properties', () => {
25+
const node = new CompositeMenuNode('test-id');
26+
node.updateOptions({ label: 'node-label', icon: 'icon', order: 'a', role: CompoundMenuNodeRole.Flat, when: 'node-condition' });
27+
expect(node.label).to.equal('node-label');
28+
expect(node.icon).to.equal('icon');
29+
expect(node.order).to.equal('a');
30+
expect(node.role).to.equal(CompoundMenuNodeRole.Flat);
31+
expect(node.when).to.equal('node-condition');
32+
});
33+
it('should update existing node properties', () => {
34+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a1', role: CompoundMenuNodeRole.Submenu, when: 'test-condition' });
35+
node.updateOptions({ label: 'NEW-label', icon: 'NEW-icon', order: 'a2', role: CompoundMenuNodeRole.Flat, when: 'NEW-condition' });
36+
expect(node.label).to.equal('NEW-label');
37+
expect(node.icon).to.equal('NEW-icon');
38+
expect(node.order).to.equal('a2');
39+
expect(node.role).to.equal(CompoundMenuNodeRole.Flat);
40+
expect(node.when).to.equal('NEW-condition');
41+
});
42+
it('should update only the icon without affecting other properties', () => {
43+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a' });
44+
node.updateOptions({ icon: 'NEW-icon' });
45+
expect(node.label).to.equal('test-label');
46+
expect(node.icon).to.equal('NEW-icon');
47+
expect(node.order).to.equal('a');
48+
});
49+
it('should not allow to unset properties', () => {
50+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a' });
51+
node.updateOptions({ icon: undefined });
52+
expect(node.label).to.equal('test-label');
53+
expect(node.icon).to.equal('test-icon');
54+
expect(node.order).to.equal('a');
55+
});
56+
it('should allow to set empty strings in properties', () => {
57+
const node = new CompositeMenuNode('test-id', 'test-label');
58+
node.updateOptions({ label: '' });
59+
expect(node.label).to.equal('');
60+
});
61+
it('should not cause side effects when updating a property to its existing value', () => {
62+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a' });
63+
node.updateOptions({ icon: 'test-icon' });
64+
expect(node.label).to.equal('test-label');
65+
expect(node.icon).to.equal('test-icon');
66+
expect(node.order).to.equal('a');
67+
});
68+
});
69+
});

packages/core/src/common/menu/composite-menu-node.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ export class CompositeMenuNode implements MutableCompoundMenuNode {
6363

6464
updateOptions(options?: SubMenuOptions): void {
6565
if (options) {
66-
this.iconClass ??= options.icon ?? options.iconClass;
67-
this.label ??= options.label;
68-
this.order ??= options.order;
69-
this._role ??= options.role;
70-
this._when ??= options.when;
66+
this.iconClass = options.icon ?? options.iconClass ?? this.iconClass;
67+
this.label = options.label ?? this.label;
68+
this.order = options.order ?? this.order;
69+
this._role = options.role ?? this._role;
70+
this._when = options.when ?? this._when;
7171
}
7272
}
7373

0 commit comments

Comments
 (0)