|
| 1 | +/** |
| 2 | + * Copyright 2026 Arm Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import 'jest'; |
| 18 | +import * as vscode from 'vscode'; |
| 19 | +import * as manifest from '../../manifest'; |
| 20 | +import { csolutionFactory } from '../../solutions/csolution.factory'; |
| 21 | +import { solutionManagerFactory } from '../../solutions/solution-manager.factories'; |
| 22 | +import { commandsProviderFactory } from '../../vscode-api/commands-provider.factories'; |
| 23 | +import { configurationProviderFactory } from '../../vscode-api/configuration-provider.factories'; |
| 24 | +import { ManageSolutionCustomEditorProvider, registerManageSolutionCommand } from './manage-solution-custom-editor'; |
| 25 | + |
| 26 | +type MockTabGroups = { |
| 27 | + all: Array<{ tabs: vscode.Tab[] }>; |
| 28 | + close: jest.Mock; |
| 29 | + onDidChangeTabs: jest.Mock; |
| 30 | +}; |
| 31 | + |
| 32 | +describe('registerManageSolutionCommand', () => { |
| 33 | + const solutionUri = vscode.Uri.file('/workspace/folder/solution.csolution.yml'); |
| 34 | + const commandOpenUiEditor = `${manifest.PACKAGE_NAME}.openManageSolutionUiEditor`; |
| 35 | + const commandOpenTextEditor = `${manifest.PACKAGE_NAME}.openManageSolutionTextEditor`; |
| 36 | + |
| 37 | + let tabGroups: MockTabGroups; |
| 38 | + let onDidChangeTabsHandler: ((event: vscode.TabChangeEvent) => void) | undefined; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + onDidChangeTabsHandler = undefined; |
| 42 | + tabGroups = { |
| 43 | + all: [], |
| 44 | + close: jest.fn().mockResolvedValue(undefined), |
| 45 | + onDidChangeTabs: jest.fn().mockImplementation((cb: (event: vscode.TabChangeEvent) => void) => { |
| 46 | + onDidChangeTabsHandler = cb; |
| 47 | + return { dispose: jest.fn() }; |
| 48 | + }), |
| 49 | + }; |
| 50 | + |
| 51 | + (vscode.window as unknown as { tabGroups: MockTabGroups }).tabGroups = tabGroups; |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + jest.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('closes text tab for same solution when opening UI editor in single-editor mode', async () => { |
| 59 | + const commandsProvider = commandsProviderFactory(); |
| 60 | + const solutionManager = solutionManagerFactory(); |
| 61 | + const configurationProvider = configurationProviderFactory(); |
| 62 | + configurationProvider.getConfigVariableOrDefault.mockReturnValue(true); |
| 63 | + solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: solutionUri.fsPath })); |
| 64 | + |
| 65 | + const textTab = { input: { uri: solutionUri } } as unknown as vscode.Tab; |
| 66 | + const customTab = { input: { uri: solutionUri, viewType: ManageSolutionCustomEditorProvider.viewType } } as unknown as vscode.Tab; |
| 67 | + tabGroups.all = [{ tabs: [textTab, customTab] }]; |
| 68 | + |
| 69 | + registerManageSolutionCommand(commandsProvider, solutionManager, configurationProvider); |
| 70 | + |
| 71 | + await commandsProvider.mockRunRegistered(commandOpenUiEditor, solutionUri); |
| 72 | + |
| 73 | + expect(tabGroups.close).toHaveBeenCalledWith([textTab], true); |
| 74 | + expect(commandsProvider.executeCommand).toHaveBeenCalledWith( |
| 75 | + 'vscode.openWith', |
| 76 | + solutionUri, |
| 77 | + ManageSolutionCustomEditorProvider.viewType |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('closes manage solution custom tab for same solution when opening text editor in single-editor mode', async () => { |
| 82 | + const commandsProvider = commandsProviderFactory(); |
| 83 | + const solutionManager = solutionManagerFactory(); |
| 84 | + const configurationProvider = configurationProviderFactory(); |
| 85 | + configurationProvider.getConfigVariableOrDefault.mockReturnValue(true); |
| 86 | + solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: solutionUri.fsPath })); |
| 87 | + |
| 88 | + const textTab = { input: { uri: solutionUri } } as unknown as vscode.Tab; |
| 89 | + const customTab = { input: { uri: solutionUri, viewType: ManageSolutionCustomEditorProvider.viewType } } as unknown as vscode.Tab; |
| 90 | + tabGroups.all = [{ tabs: [textTab, customTab] }]; |
| 91 | + |
| 92 | + registerManageSolutionCommand(commandsProvider, solutionManager, configurationProvider); |
| 93 | + |
| 94 | + await commandsProvider.mockRunRegistered(commandOpenTextEditor, solutionUri); |
| 95 | + |
| 96 | + expect(tabGroups.close).toHaveBeenCalledWith([customTab], true); |
| 97 | + expect(commandsProvider.executeCommand).toHaveBeenCalledWith('vscode.openWith', solutionUri, 'default'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('closes newly opened text tab from explorer when webview tab already exists', async () => { |
| 101 | + const commandsProvider = commandsProviderFactory(); |
| 102 | + const solutionManager = solutionManagerFactory(); |
| 103 | + const configurationProvider = configurationProviderFactory(); |
| 104 | + configurationProvider.getConfigVariableOrDefault.mockReturnValue(true); |
| 105 | + solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: solutionUri.fsPath })); |
| 106 | + |
| 107 | + const textTab = { input: { uri: solutionUri } } as unknown as vscode.Tab; |
| 108 | + const customTab = { input: { uri: solutionUri, viewType: ManageSolutionCustomEditorProvider.viewType } } as unknown as vscode.Tab; |
| 109 | + tabGroups.all = [{ tabs: [customTab, textTab] }]; |
| 110 | + |
| 111 | + registerManageSolutionCommand(commandsProvider, solutionManager, configurationProvider); |
| 112 | + expect(onDidChangeTabsHandler).toBeDefined(); |
| 113 | + |
| 114 | + onDidChangeTabsHandler!({ opened: [textTab], closed: [], changed: [] }); |
| 115 | + await Promise.resolve(); |
| 116 | + |
| 117 | + expect(tabGroups.close).toHaveBeenCalledWith([textTab], true); |
| 118 | + }); |
| 119 | + |
| 120 | + it('does not close tabs when single-editor mode is disabled', async () => { |
| 121 | + const commandsProvider = commandsProviderFactory(); |
| 122 | + const solutionManager = solutionManagerFactory(); |
| 123 | + const configurationProvider = configurationProviderFactory(); |
| 124 | + configurationProvider.getConfigVariableOrDefault.mockReturnValue(false); |
| 125 | + solutionManager.getCsolution.mockReturnValue(csolutionFactory({ solutionPath: solutionUri.fsPath })); |
| 126 | + |
| 127 | + const textTab = { input: { uri: solutionUri } } as unknown as vscode.Tab; |
| 128 | + const customTab = { input: { uri: solutionUri, viewType: ManageSolutionCustomEditorProvider.viewType } } as unknown as vscode.Tab; |
| 129 | + tabGroups.all = [{ tabs: [customTab, textTab] }]; |
| 130 | + |
| 131 | + registerManageSolutionCommand(commandsProvider, solutionManager, configurationProvider); |
| 132 | + |
| 133 | + await commandsProvider.mockRunRegistered(commandOpenUiEditor, solutionUri); |
| 134 | + expect(onDidChangeTabsHandler).toBeDefined(); |
| 135 | + onDidChangeTabsHandler!({ opened: [textTab], closed: [], changed: [] }); |
| 136 | + await Promise.resolve(); |
| 137 | + |
| 138 | + expect(tabGroups.close).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments