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