|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (C) 2026 Posit Software, PBC. All rights reserved. |
| 3 | + * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +/* eslint-disable local/code-no-dangerous-type-assertions */ |
| 7 | + |
| 8 | +import assert from 'assert'; |
| 9 | +import sinon from 'sinon'; |
| 10 | +import { Emitter } from '../../../../../base/common/event.js'; |
| 11 | +import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js'; |
| 12 | +import { setupReactRenderer } from '../../../../../base/test/browser/react.js'; |
| 13 | +import { IMenu, IMenuChangeEvent, IMenuService, MenuId, MenuItemAction, SubmenuItemAction } from '../../../../../platform/actions/common/actions.js'; |
| 14 | +import { IVersionedMenu, useMenu } from '../../browser/useMenu.js'; |
| 15 | +import { useMenuActions } from '../../browser/useMenuActions.js'; |
| 16 | +import { MockContextKeyService } from '../../../../../platform/keybinding/test/common/mockKeybindingService.js'; |
| 17 | +import { PositronReactServicesContext } from '../../../../../base/browser/positronReactRendererContext.js'; |
| 18 | +import { PositronReactServices } from '../../../../../base/browser/positronReactServices.js'; |
| 19 | + |
| 20 | +type Actions = [string, (MenuItemAction | SubmenuItemAction)[]][]; |
| 21 | + |
| 22 | +/** Harness that renders useMenuActions with a directly controlled IVersionedMenu. */ |
| 23 | +function UseMenuActionsHarness({ menu, onActions }: { |
| 24 | + menu: IVersionedMenu; |
| 25 | + onActions: (a: Actions) => void; |
| 26 | +}) { |
| 27 | + const actions = useMenuActions(menu); |
| 28 | + onActions(actions); |
| 29 | + return null; |
| 30 | +} |
| 31 | + |
| 32 | +/** Harness that composes useMenu + useMenuActions the way real components do. */ |
| 33 | +function ComposedHarness({ contextKeyService, onActions }: { |
| 34 | + contextKeyService: MockContextKeyService | undefined; |
| 35 | + onActions: (a: Actions) => void; |
| 36 | +}) { |
| 37 | + const menu = useMenu(MenuId.CommandPalette, contextKeyService); |
| 38 | + const actions = useMenuActions(menu); |
| 39 | + onActions(actions); |
| 40 | + return null; |
| 41 | +} |
| 42 | + |
| 43 | +suite('useMenuActions', () => { |
| 44 | + const { render } = setupReactRenderer(); |
| 45 | + const disposables = ensureNoDisposablesAreLeakedInTestSuite(); |
| 46 | + |
| 47 | + const action = (id: string) => ({ id }) as MenuItemAction; |
| 48 | + |
| 49 | + suite('standalone', () => { |
| 50 | + test('returns empty array when menu.current is undefined', () => { |
| 51 | + let captured: Actions = []; |
| 52 | + const menu: IVersionedMenu = { current: undefined, version: 0 }; |
| 53 | + |
| 54 | + render(<UseMenuActionsHarness menu={menu} onActions={a => { captured = a; }} />); |
| 55 | + assert.deepStrictEqual(captured, []); |
| 56 | + }); |
| 57 | + |
| 58 | + test('returns actions when menu.current is present', () => { |
| 59 | + let captured: Actions = []; |
| 60 | + const expected: Actions = [['group', [action('a1'), action('a2')]]]; |
| 61 | + const menu: IVersionedMenu = { |
| 62 | + current: { getActions: () => expected } as unknown as IMenu, |
| 63 | + version: 0, |
| 64 | + }; |
| 65 | + |
| 66 | + render(<UseMenuActionsHarness menu={menu} onActions={a => { captured = a; }} />); |
| 67 | + assert.deepStrictEqual(captured, expected); |
| 68 | + }); |
| 69 | + |
| 70 | + test('updates when version changes', () => { |
| 71 | + let captured: Actions = []; |
| 72 | + const actionsV0: Actions = [['g', [action('old')]]]; |
| 73 | + const actionsV1: Actions = [['g', [action('new')]]]; |
| 74 | + let currentActions = actionsV0; |
| 75 | + |
| 76 | + const mockMenu = { getActions: () => currentActions } as unknown as IMenu; |
| 77 | + |
| 78 | + render(<UseMenuActionsHarness |
| 79 | + menu={{ current: mockMenu, version: 0 }} |
| 80 | + onActions={a => { captured = a; }} |
| 81 | + />); |
| 82 | + assert.deepStrictEqual(captured, actionsV0); |
| 83 | + |
| 84 | + currentActions = actionsV1; |
| 85 | + render(<UseMenuActionsHarness |
| 86 | + menu={{ current: mockMenu, version: 1 }} |
| 87 | + onActions={a => { captured = a; }} |
| 88 | + />); |
| 89 | + assert.deepStrictEqual(captured, actionsV1); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + suite('composed with useMenu', () => { |
| 94 | + let contextKeyService: MockContextKeyService; |
| 95 | + let menuActions: Actions; |
| 96 | + let onDidChange: Emitter<IMenuChangeEvent>; |
| 97 | + |
| 98 | + setup(() => { |
| 99 | + contextKeyService = disposables.add(new MockContextKeyService()); |
| 100 | + menuActions = []; |
| 101 | + onDidChange = disposables.add(new Emitter<IMenuChangeEvent>()); |
| 102 | + }); |
| 103 | + |
| 104 | + /** Tracks all menus created by the mock service. */ |
| 105 | + let createdMenus: { menu: IMenu; dispose: sinon.SinonSpy }[]; |
| 106 | + |
| 107 | + function createServices(): PositronReactServices { |
| 108 | + createdMenus = []; |
| 109 | + const menuService: IMenuService = { |
| 110 | + _serviceBrand: undefined, |
| 111 | + createMenu: () => { |
| 112 | + const dispose = sinon.spy(); |
| 113 | + const menu: IMenu = { |
| 114 | + onDidChange: onDidChange.event, |
| 115 | + dispose, |
| 116 | + getActions: () => menuActions, |
| 117 | + }; |
| 118 | + createdMenus.push({ menu, dispose }); |
| 119 | + return menu; |
| 120 | + }, |
| 121 | + getMenuActions: () => [], |
| 122 | + getMenuContexts: () => new Set(), |
| 123 | + resetHiddenStates: () => { }, |
| 124 | + }; |
| 125 | + return { |
| 126 | + get: (id: any) => { |
| 127 | + if (id === IMenuService) { return menuService; } |
| 128 | + throw new Error(`Unexpected service: ${id}`); |
| 129 | + }, |
| 130 | + } as unknown as PositronReactServices; |
| 131 | + } |
| 132 | + |
| 133 | + test('resolves actions in the same render as useMenu', () => { |
| 134 | + menuActions = [['g', [action('a1')]]]; |
| 135 | + let captured: Actions = []; |
| 136 | + const services = createServices(); |
| 137 | + |
| 138 | + const element = ( |
| 139 | + <PositronReactServicesContext.Provider value={services}> |
| 140 | + <ComposedHarness |
| 141 | + contextKeyService={contextKeyService} |
| 142 | + onActions={a => { captured = a; }} |
| 143 | + /> |
| 144 | + </PositronReactServicesContext.Provider> |
| 145 | + ); |
| 146 | + |
| 147 | + // Render 1: initial (effect queued, menu undefined) |
| 148 | + render(element); |
| 149 | + assert.deepStrictEqual(captured, [], 'no actions before effect settles'); |
| 150 | + |
| 151 | + // Render 2: effect creates menu; useMemo resolves actions in same cycle |
| 152 | + render(element); |
| 153 | + assert.deepStrictEqual(captured, [['g', [action('a1')]]]); |
| 154 | + }); |
| 155 | + |
| 156 | + test('updates actions when menu fires onDidChange', () => { |
| 157 | + menuActions = [['g', [action('v0')]]]; |
| 158 | + let captured: Actions = []; |
| 159 | + const services = createServices(); |
| 160 | + |
| 161 | + const element = ( |
| 162 | + <PositronReactServicesContext.Provider value={services}> |
| 163 | + <ComposedHarness |
| 164 | + contextKeyService={contextKeyService} |
| 165 | + onActions={a => { captured = a; }} |
| 166 | + /> |
| 167 | + </PositronReactServicesContext.Provider> |
| 168 | + ); |
| 169 | + |
| 170 | + render(element); |
| 171 | + render(element); |
| 172 | + assert.deepStrictEqual(captured, [['g', [action('v0')]]]); |
| 173 | + |
| 174 | + // Simulate menu content change |
| 175 | + menuActions = [['g', [action('v1')]]]; |
| 176 | + onDidChange.fire({} as IMenuChangeEvent); |
| 177 | + |
| 178 | + // One render to pick up the version bump |
| 179 | + render(element); |
| 180 | + assert.deepStrictEqual(captured, [['g', [action('v1')]]]); |
| 181 | + }); |
| 182 | + |
| 183 | + test('clears actions when contextKeyService becomes undefined', () => { |
| 184 | + menuActions = [['g', [action('a1')]]]; |
| 185 | + let captured: Actions = []; |
| 186 | + const services = createServices(); |
| 187 | + |
| 188 | + const withService = ( |
| 189 | + <PositronReactServicesContext.Provider value={services}> |
| 190 | + <ComposedHarness |
| 191 | + contextKeyService={contextKeyService} |
| 192 | + onActions={a => { captured = a; }} |
| 193 | + /> |
| 194 | + </PositronReactServicesContext.Provider> |
| 195 | + ); |
| 196 | + render(withService); |
| 197 | + render(withService); |
| 198 | + assert.deepStrictEqual(captured, [['g', [action('a1')]]]); |
| 199 | + |
| 200 | + // Remove contextKeyService -- actions must clear synchronously |
| 201 | + render( |
| 202 | + <PositronReactServicesContext.Provider value={services}> |
| 203 | + <ComposedHarness |
| 204 | + contextKeyService={undefined} |
| 205 | + onActions={a => { captured = a; }} |
| 206 | + /> |
| 207 | + </PositronReactServicesContext.Provider> |
| 208 | + ); |
| 209 | + assert.deepStrictEqual(captured, [], 'actions must clear when service disappears'); |
| 210 | + }); |
| 211 | + |
| 212 | + test('clears stale actions and disposes old menu on contextKeyService identity swap', () => { |
| 213 | + menuActions = [['g', [action('a1')]]]; |
| 214 | + let captured: Actions = []; |
| 215 | + const services = createServices(); |
| 216 | + |
| 217 | + const serviceA = contextKeyService; |
| 218 | + const serviceB = disposables.add(new MockContextKeyService()); |
| 219 | + |
| 220 | + // Mount with service A and settle |
| 221 | + const withA = ( |
| 222 | + <PositronReactServicesContext.Provider value={services}> |
| 223 | + <ComposedHarness |
| 224 | + contextKeyService={serviceA} |
| 225 | + onActions={a => { captured = a; }} |
| 226 | + /> |
| 227 | + </PositronReactServicesContext.Provider> |
| 228 | + ); |
| 229 | + render(withA); |
| 230 | + render(withA); |
| 231 | + assert.deepStrictEqual(captured, [['g', [action('a1')]]]); |
| 232 | + assert.strictEqual(createdMenus.length, 1, 'one menu created for service A'); |
| 233 | + |
| 234 | + // Swap to service B -- stale actions must not leak on first render |
| 235 | + render( |
| 236 | + <PositronReactServicesContext.Provider value={services}> |
| 237 | + <ComposedHarness |
| 238 | + contextKeyService={serviceB} |
| 239 | + onActions={a => { captured = a; }} |
| 240 | + /> |
| 241 | + </PositronReactServicesContext.Provider> |
| 242 | + ); |
| 243 | + assert.deepStrictEqual(captured, [], 'stale actions from service A must not appear'); |
| 244 | + |
| 245 | + // Settle the effect for service B |
| 246 | + render( |
| 247 | + <PositronReactServicesContext.Provider value={services}> |
| 248 | + <ComposedHarness |
| 249 | + contextKeyService={serviceB} |
| 250 | + onActions={a => { captured = a; }} |
| 251 | + /> |
| 252 | + </PositronReactServicesContext.Provider> |
| 253 | + ); |
| 254 | + assert.deepStrictEqual(captured, [['g', [action('a1')]]], 'actions from service B menu'); |
| 255 | + assert.strictEqual(createdMenus.length, 2, 'a new menu was created for service B'); |
| 256 | + sinon.assert.calledOnce(createdMenus[0].dispose); |
| 257 | + }); |
| 258 | + }); |
| 259 | +}); |
0 commit comments