Skip to content

Commit 9421f14

Browse files
Copilotalexr00
andcommitted
Add tests for PR title truncation based on horizontal scrolling setting
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 4cd47d8 commit 9421f14

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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 * as vscode from 'vscode';
7+
import { SinonSandbox, createSandbox } from 'sinon';
8+
import { default as assert } from 'assert';
9+
10+
import { PRNode } from '../../../view/treeNodes/pullRequestNode';
11+
import { PullRequestModel } from '../../../github/pullRequestModel';
12+
import { FolderRepositoryManager } from '../../../github/folderRepositoryManager';
13+
import { MockTelemetry } from '../../mocks/mockTelemetry';
14+
import { MockExtensionContext } from '../../mocks/mockExtensionContext';
15+
import { MockRepository } from '../../mocks/mockRepository';
16+
import { MockGitHubRepository } from '../../mocks/mockGitHubRepository';
17+
import { MockThemeWatcher } from '../../mocks/mockThemeWatcher';
18+
import { GitHubRemote } from '../../../common/remote';
19+
import { Protocol } from '../../../common/protocol';
20+
import { CredentialStore } from '../../../github/credentials';
21+
import { parseGraphQLPullRequest } from '../../../github/utils';
22+
import { GitApiImpl } from '../../../api/api1';
23+
import { RepositoriesManager } from '../../../github/repositoriesManager';
24+
import { GitHubServerType } from '../../../common/authentication';
25+
import { CreatePullRequestHelper } from '../../../view/createPullRequestHelper';
26+
import { MockNotificationManager } from '../../mocks/mockNotificationManager';
27+
import { PrsTreeModel } from '../../../view/prsTreeModel';
28+
import { NotificationsManager } from '../../../notifications/notificationsManager';
29+
30+
describe('PRNode', function () {
31+
let sinon: SinonSandbox;
32+
let context: MockExtensionContext;
33+
let telemetry: MockTelemetry;
34+
let credentialStore: CredentialStore;
35+
let reposManager: RepositoriesManager;
36+
let createPrHelper: CreatePullRequestHelper;
37+
let mockThemeWatcher: MockThemeWatcher;
38+
let mockNotificationsManager: MockNotificationManager;
39+
let prsTreeModel: PrsTreeModel;
40+
41+
beforeEach(function () {
42+
sinon = createSandbox();
43+
mockThemeWatcher = new MockThemeWatcher();
44+
context = new MockExtensionContext();
45+
telemetry = new MockTelemetry();
46+
credentialStore = new CredentialStore(telemetry, context);
47+
reposManager = new RepositoriesManager(credentialStore, telemetry);
48+
prsTreeModel = new PrsTreeModel(telemetry, reposManager, context);
49+
mockNotificationsManager = new MockNotificationManager();
50+
createPrHelper = new CreatePullRequestHelper();
51+
});
52+
53+
afterEach(function () {
54+
context.dispose();
55+
sinon.restore();
56+
});
57+
58+
describe('PR title truncation', function () {
59+
it('truncates long PR titles when horizontal scrolling is disabled', async function () {
60+
// Setup a PR with a very long title
61+
const longTitle = 'This is a very long pull request title that exceeds fifty characters and should be truncated when horizontal scrolling is disabled';
62+
const url = 'git@github.com:test/repo';
63+
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
64+
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
65+
gitHubRepository.buildMetadata(m => {
66+
m.clone_url('https://github.com/test/repo');
67+
});
68+
69+
const pr = gitHubRepository.addGraphQLPullRequest(builder => {
70+
builder.pullRequest(pr => {
71+
pr.repository(r =>
72+
r.pullRequest(p => {
73+
p.databaseId(1234);
74+
p.number(1234);
75+
p.title(longTitle);
76+
p.author(a => a.login('testuser').avatarUrl('https://github.com/testuser.jpg'));
77+
p.baseRef!(b => b.repository(br => br.url('https://github.com/test/repo')));
78+
p.baseRepository(r => r.url('https://github.com/test/repo'));
79+
}),
80+
);
81+
});
82+
}).pullRequest;
83+
const prItem = await parseGraphQLPullRequest(pr.repository!.pullRequest, gitHubRepository);
84+
const pullRequestModel = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
85+
86+
const repository = new MockRepository();
87+
await repository.addRemote(remote.remoteName, remote.url);
88+
const manager = new FolderRepositoryManager(0, context, repository, telemetry, new GitApiImpl(reposManager), credentialStore, createPrHelper, mockThemeWatcher);
89+
90+
// Stub horizontal scrolling setting to be false (default)
91+
sinon.stub(vscode.workspace, 'getConfiguration').callsFake((section?: string) => {
92+
if (section === 'workbench') {
93+
return {
94+
get: (key: string, defaultValue?: any) => {
95+
if (key === 'list.horizontalScrolling') {
96+
return false;
97+
}
98+
return defaultValue;
99+
},
100+
} as any;
101+
}
102+
// Return a mock for other configurations
103+
return {
104+
get: (key: string, defaultValue?: any) => defaultValue,
105+
} as any;
106+
});
107+
108+
const prNode = new PRNode(
109+
{} as any,
110+
manager,
111+
pullRequestModel,
112+
false,
113+
mockNotificationsManager as NotificationsManager,
114+
prsTreeModel,
115+
);
116+
117+
const treeItem = await prNode.getTreeItem();
118+
const label = (treeItem.label as vscode.TreeItemLabel2).label as vscode.MarkdownString;
119+
120+
// Title should be truncated to 50 characters plus '...'
121+
assert.ok(label.value.includes('...'));
122+
assert.ok(label.value.length < longTitle.length);
123+
assert.strictEqual(label.value, longTitle.substring(0, 50) + '...');
124+
});
125+
126+
it('shows full PR title when horizontal scrolling is enabled', async function () {
127+
// Setup a PR with a very long title
128+
const longTitle = 'This is a very long pull request title that exceeds fifty characters and should NOT be truncated when horizontal scrolling is enabled';
129+
const url = 'git@github.com:test/repo';
130+
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
131+
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
132+
gitHubRepository.buildMetadata(m => {
133+
m.clone_url('https://github.com/test/repo');
134+
});
135+
136+
const pr = gitHubRepository.addGraphQLPullRequest(builder => {
137+
builder.pullRequest(pr => {
138+
pr.repository(r =>
139+
r.pullRequest(p => {
140+
p.databaseId(5678);
141+
p.number(5678);
142+
p.title(longTitle);
143+
p.author(a => a.login('testuser').avatarUrl('https://github.com/testuser.jpg'));
144+
p.baseRef!(b => b.repository(br => br.url('https://github.com/test/repo')));
145+
p.baseRepository(r => r.url('https://github.com/test/repo'));
146+
}),
147+
);
148+
});
149+
}).pullRequest;
150+
const prItem = await parseGraphQLPullRequest(pr.repository!.pullRequest, gitHubRepository);
151+
const pullRequestModel = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
152+
153+
const repository = new MockRepository();
154+
await repository.addRemote(remote.remoteName, remote.url);
155+
const manager = new FolderRepositoryManager(0, context, repository, telemetry, new GitApiImpl(reposManager), credentialStore, createPrHelper, mockThemeWatcher);
156+
157+
// Stub horizontal scrolling setting to be true
158+
sinon.stub(vscode.workspace, 'getConfiguration').callsFake((section?: string) => {
159+
if (section === 'workbench') {
160+
return {
161+
get: (key: string, defaultValue?: any) => {
162+
if (key === 'list.horizontalScrolling') {
163+
return true;
164+
}
165+
return defaultValue;
166+
},
167+
} as any;
168+
}
169+
// Return a mock for other configurations
170+
return {
171+
get: (key: string, defaultValue?: any) => defaultValue,
172+
} as any;
173+
});
174+
175+
const prNode = new PRNode(
176+
{} as any,
177+
manager,
178+
pullRequestModel,
179+
false,
180+
mockNotificationsManager as NotificationsManager,
181+
prsTreeModel,
182+
);
183+
184+
const treeItem = await prNode.getTreeItem();
185+
const label = (treeItem.label as vscode.TreeItemLabel2).label as vscode.MarkdownString;
186+
187+
// Title should NOT be truncated when horizontal scrolling is enabled
188+
assert.ok(!label.value.includes('...'));
189+
assert.strictEqual(label.value, longTitle);
190+
});
191+
192+
it('does not truncate short PR titles regardless of horizontal scrolling setting', async function () {
193+
const shortTitle = 'Short title';
194+
const url = 'git@github.com:test/repo';
195+
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
196+
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
197+
gitHubRepository.buildMetadata(m => {
198+
m.clone_url('https://github.com/test/repo');
199+
});
200+
201+
const pr = gitHubRepository.addGraphQLPullRequest(builder => {
202+
builder.pullRequest(pr => {
203+
pr.repository(r =>
204+
r.pullRequest(p => {
205+
p.databaseId(9999);
206+
p.number(9999);
207+
p.title(shortTitle);
208+
p.author(a => a.login('testuser').avatarUrl('https://github.com/testuser.jpg'));
209+
p.baseRef!(b => b.repository(br => br.url('https://github.com/test/repo')));
210+
p.baseRepository(r => r.url('https://github.com/test/repo'));
211+
}),
212+
);
213+
});
214+
}).pullRequest;
215+
const prItem = await parseGraphQLPullRequest(pr.repository!.pullRequest, gitHubRepository);
216+
const pullRequestModel = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
217+
218+
const repository = new MockRepository();
219+
await repository.addRemote(remote.remoteName, remote.url);
220+
const manager = new FolderRepositoryManager(0, context, repository, telemetry, new GitApiImpl(reposManager), credentialStore, createPrHelper, mockThemeWatcher);
221+
222+
// Test with horizontal scrolling disabled
223+
sinon.stub(vscode.workspace, 'getConfiguration').callsFake((section?: string) => {
224+
if (section === 'workbench') {
225+
return {
226+
get: (key: string, defaultValue?: any) => {
227+
if (key === 'list.horizontalScrolling') {
228+
return false;
229+
}
230+
return defaultValue;
231+
},
232+
} as any;
233+
}
234+
return {
235+
get: (key: string, defaultValue?: any) => defaultValue,
236+
} as any;
237+
});
238+
239+
const prNode = new PRNode(
240+
{} as any,
241+
manager,
242+
pullRequestModel,
243+
false,
244+
mockNotificationsManager as NotificationsManager,
245+
prsTreeModel,
246+
);
247+
248+
const treeItem = await prNode.getTreeItem();
249+
const label = (treeItem.label as vscode.TreeItemLabel2).label as vscode.MarkdownString;
250+
251+
// Short title should not be truncated
252+
assert.ok(!label.value.includes('...'));
253+
assert.strictEqual(label.value, shortTitle);
254+
});
255+
});
256+
});

0 commit comments

Comments
 (0)