Skip to content

Commit 53ee5e3

Browse files
authoredFeb 28, 2024··
Allow updates of CompositeMenuNode properties (#12948) (#13425)
- refactor updateOptions function to allow updates - add unit test cases for updateOptions Fixes #12948
1 parent b96a84a commit 53ee5e3

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 { expect } from 'chai';
17+
import { CompositeMenuNode } from './composite-menu-node';
18+
import { CompoundMenuNodeRole } from './menu-types';
19+
20+
describe('composite-menu-node', () => {
21+
describe('updateOptions', () => {
22+
it('should update undefined node properties', () => {
23+
const node = new CompositeMenuNode('test-id');
24+
node.updateOptions({ label: 'node-label', icon: 'icon', order: 'a', role: CompoundMenuNodeRole.Flat, when: 'node-condition' });
25+
expect(node.label).to.equal('node-label');
26+
expect(node.icon).to.equal('icon');
27+
expect(node.order).to.equal('a');
28+
expect(node.role).to.equal(CompoundMenuNodeRole.Flat);
29+
expect(node.when).to.equal('node-condition');
30+
});
31+
it('should update existing node properties', () => {
32+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a1', role: CompoundMenuNodeRole.Submenu, when: 'test-condition' });
33+
node.updateOptions({ label: 'NEW-label', icon: 'NEW-icon', order: 'a2', role: CompoundMenuNodeRole.Flat, when: 'NEW-condition' });
34+
expect(node.label).to.equal('NEW-label');
35+
expect(node.icon).to.equal('NEW-icon');
36+
expect(node.order).to.equal('a2');
37+
expect(node.role).to.equal(CompoundMenuNodeRole.Flat);
38+
expect(node.when).to.equal('NEW-condition');
39+
});
40+
it('should update only the icon without affecting other properties', () => {
41+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a' });
42+
node.updateOptions({ icon: 'NEW-icon' });
43+
expect(node.label).to.equal('test-label');
44+
expect(node.icon).to.equal('NEW-icon');
45+
expect(node.order).to.equal('a');
46+
});
47+
it('should not allow to unset properties', () => {
48+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a' });
49+
node.updateOptions({ icon: undefined });
50+
expect(node.label).to.equal('test-label');
51+
expect(node.icon).to.equal('test-icon');
52+
expect(node.order).to.equal('a');
53+
});
54+
it('should allow to set empty strings in properties', () => {
55+
const node = new CompositeMenuNode('test-id', 'test-label');
56+
node.updateOptions({ label: '' });
57+
expect(node.label).to.equal('');
58+
});
59+
it('should not cause side effects when updating a property to its existing value', () => {
60+
const node = new CompositeMenuNode('test-id', 'test-label', { icon: 'test-icon', order: 'a' });
61+
node.updateOptions({ icon: 'test-icon' });
62+
expect(node.label).to.equal('test-label');
63+
expect(node.icon).to.equal('test-icon');
64+
expect(node.order).to.equal('a');
65+
});
66+
});
67+
});

‎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)
Please sign in to comment.