Skip to content

Commit 9ef33a8

Browse files
Copilotalexr00
andcommitted
Add tests for workingBaseBranch setting
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent e0ebc5a commit 9ef33a8

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { default as assert } from 'assert';
7+
import * as vscode from 'vscode';
8+
import { IssueFeatureRegistrar } from '../../issues/issueFeatureRegistrar';
9+
import { StateManager } from '../../issues/stateManager';
10+
import { WORKING_BASE_BRANCH, ISSUES_SETTINGS_NAMESPACE } from '../../common/settingKeys';
11+
12+
// Mock classes for testing
13+
class MockStateManager {
14+
setCurrentIssueCalled = false;
15+
setCurrentIssueCheckoutFlag = false;
16+
17+
async setCurrentIssue(repoManager: any, issue: any, checkoutDefaultBranch: boolean) {
18+
this.setCurrentIssueCalled = true;
19+
this.setCurrentIssueCheckoutFlag = checkoutDefaultBranch;
20+
}
21+
}
22+
23+
class MockFolderRepositoryManager {
24+
async findUpstreamForItem() {
25+
return { needsFork: false, remote: {} };
26+
}
27+
}
28+
29+
class MockIssueModel {
30+
githubRepository = { remote: {} };
31+
remote = {};
32+
}
33+
34+
describe('IssueFeatureRegistrar workingBaseBranch setting', function () {
35+
let issueFeatureRegistrar: IssueFeatureRegistrar;
36+
let mockStateManager: MockStateManager;
37+
let originalGetConfiguration: typeof vscode.workspace.getConfiguration;
38+
let originalShowQuickPick: typeof vscode.window.showQuickPick;
39+
40+
beforeEach(() => {
41+
mockStateManager = new MockStateManager();
42+
issueFeatureRegistrar = new IssueFeatureRegistrar(
43+
undefined as any,
44+
undefined as any,
45+
undefined as any,
46+
undefined as any,
47+
undefined as any,
48+
mockStateManager as any,
49+
undefined as any
50+
);
51+
52+
originalGetConfiguration = vscode.workspace.getConfiguration;
53+
originalShowQuickPick = vscode.window.showQuickPick;
54+
});
55+
56+
afterEach(() => {
57+
vscode.workspace.getConfiguration = originalGetConfiguration;
58+
vscode.window.showQuickPick = originalShowQuickPick;
59+
});
60+
61+
it('should not checkout default branch when workingBaseBranch is currentBranch', async function () {
62+
// Mock workspace configuration to return 'currentBranch'
63+
vscode.workspace.getConfiguration = (section?: string) => {
64+
if (section === ISSUES_SETTINGS_NAMESPACE) {
65+
return {
66+
get: (key: string) => {
67+
if (key === WORKING_BASE_BRANCH) {
68+
return 'currentBranch';
69+
}
70+
return undefined;
71+
},
72+
} as any;
73+
}
74+
return originalGetConfiguration(section);
75+
};
76+
77+
const mockRepoManager = new MockFolderRepositoryManager();
78+
const mockIssueModel = new MockIssueModel();
79+
80+
await issueFeatureRegistrar.doStartWorking(mockRepoManager as any, mockIssueModel as any);
81+
82+
assert.strictEqual(mockStateManager.setCurrentIssueCalled, true, 'setCurrentIssue should have been called');
83+
assert.strictEqual(mockStateManager.setCurrentIssueCheckoutFlag, false, 'setCurrentIssue should have been called with checkoutDefaultBranch=false when workingBaseBranch is currentBranch');
84+
});
85+
86+
it('should checkout default branch when workingBaseBranch is defaultBranch', async function () {
87+
// Mock workspace configuration to return 'defaultBranch'
88+
vscode.workspace.getConfiguration = (section?: string) => {
89+
if (section === ISSUES_SETTINGS_NAMESPACE) {
90+
return {
91+
get: (key: string) => {
92+
if (key === WORKING_BASE_BRANCH) {
93+
return 'defaultBranch';
94+
}
95+
return undefined;
96+
},
97+
} as any;
98+
}
99+
return originalGetConfiguration(section);
100+
};
101+
102+
const mockRepoManager = new MockFolderRepositoryManager();
103+
const mockIssueModel = new MockIssueModel();
104+
105+
await issueFeatureRegistrar.doStartWorking(mockRepoManager as any, mockIssueModel as any);
106+
107+
assert.strictEqual(mockStateManager.setCurrentIssueCalled, true, 'setCurrentIssue should have been called');
108+
assert.strictEqual(mockStateManager.setCurrentIssueCheckoutFlag, true, 'setCurrentIssue should have been called with checkoutDefaultBranch=true when workingBaseBranch is defaultBranch');
109+
});
110+
111+
it('should prompt user when workingBaseBranch is prompt and user selects current branch', async function () {
112+
// Mock workspace configuration to return 'prompt'
113+
vscode.workspace.getConfiguration = (section?: string) => {
114+
if (section === ISSUES_SETTINGS_NAMESPACE) {
115+
return {
116+
get: (key: string) => {
117+
if (key === WORKING_BASE_BRANCH) {
118+
return 'prompt';
119+
}
120+
return undefined;
121+
},
122+
} as any;
123+
}
124+
return originalGetConfiguration(section);
125+
};
126+
127+
// Mock showQuickPick to return 'Current Branch'
128+
vscode.window.showQuickPick = (async (items: any, options?: any) => {
129+
// Return the first item which should be 'Current Branch'
130+
return vscode.l10n.t('Current Branch');
131+
}) as any;
132+
133+
const mockRepoManager = new MockFolderRepositoryManager();
134+
const mockIssueModel = new MockIssueModel();
135+
136+
await issueFeatureRegistrar.doStartWorking(mockRepoManager as any, mockIssueModel as any);
137+
138+
assert.strictEqual(mockStateManager.setCurrentIssueCalled, true, 'setCurrentIssue should have been called');
139+
assert.strictEqual(mockStateManager.setCurrentIssueCheckoutFlag, false, 'setCurrentIssue should have been called with checkoutDefaultBranch=false when user selects Current Branch');
140+
});
141+
142+
it('should prompt user when workingBaseBranch is prompt and user selects default branch', async function () {
143+
// Mock workspace configuration to return 'prompt'
144+
vscode.workspace.getConfiguration = (section?: string) => {
145+
if (section === ISSUES_SETTINGS_NAMESPACE) {
146+
return {
147+
get: (key: string) => {
148+
if (key === WORKING_BASE_BRANCH) {
149+
return 'prompt';
150+
}
151+
return undefined;
152+
},
153+
} as any;
154+
}
155+
return originalGetConfiguration(section);
156+
};
157+
158+
// Mock showQuickPick to return 'Default Branch'
159+
vscode.window.showQuickPick = (async (items: any, options?: any) => {
160+
// Return the second item which should be 'Default Branch'
161+
return vscode.l10n.t('Default Branch');
162+
}) as any;
163+
164+
const mockRepoManager = new MockFolderRepositoryManager();
165+
const mockIssueModel = new MockIssueModel();
166+
167+
await issueFeatureRegistrar.doStartWorking(mockRepoManager as any, mockIssueModel as any);
168+
169+
assert.strictEqual(mockStateManager.setCurrentIssueCalled, true, 'setCurrentIssue should have been called');
170+
assert.strictEqual(mockStateManager.setCurrentIssueCheckoutFlag, true, 'setCurrentIssue should have been called with checkoutDefaultBranch=true when user selects Default Branch');
171+
});
172+
173+
it('should cancel operation when workingBaseBranch is prompt and user cancels', async function () {
174+
// Mock workspace configuration to return 'prompt'
175+
vscode.workspace.getConfiguration = (section?: string) => {
176+
if (section === ISSUES_SETTINGS_NAMESPACE) {
177+
return {
178+
get: (key: string) => {
179+
if (key === WORKING_BASE_BRANCH) {
180+
return 'prompt';
181+
}
182+
return undefined;
183+
},
184+
} as any;
185+
}
186+
return originalGetConfiguration(section);
187+
};
188+
189+
// Mock showQuickPick to return undefined (user cancelled)
190+
vscode.window.showQuickPick = (async (items: any, options?: any) => {
191+
return undefined;
192+
}) as any;
193+
194+
const mockRepoManager = new MockFolderRepositoryManager();
195+
const mockIssueModel = new MockIssueModel();
196+
197+
await issueFeatureRegistrar.doStartWorking(mockRepoManager as any, mockIssueModel as any);
198+
199+
assert.strictEqual(mockStateManager.setCurrentIssueCalled, false, 'setCurrentIssue should not have been called when user cancels prompt');
200+
});
201+
});

0 commit comments

Comments
 (0)