-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathutils.test.ts
More file actions
79 lines (70 loc) · 3.29 KB
/
utils.test.ts
File metadata and controls
79 lines (70 loc) · 3.29 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import { getPRFetchQuery, sanitizeIssueTitle, variableSubstitution } from '../../github/utils';
import { IssueModel } from '../../github/issueModel';
describe('utils', () => {
describe('getPRFetchQuery', () => {
it('replaces all instances of ${user}', () => {
const user = 'rmacfarlane';
const query = 'reviewed-by:${user} -author:${user}';
const result = getPRFetchQuery(user, query)
assert.strictEqual(result, 'is:pull-request reviewed-by:rmacfarlane -author:rmacfarlane type:pr');
});
});
describe('sanitizeIssueTitle', () => {
[
{ input: 'Issue', expected: 'Issue' },
{ input: 'Issue A', expected: 'Issue-A' },
{ input: 'Issue A', expected: 'Issue-A' },
{ input: 'Issue A', expected: 'Issue-A' },
{ input: 'Issue @ A', expected: 'Issue-A' },
{ input: "Issue 'A'", expected: 'Issue-A' },
{ input: 'Issue "A"', expected: 'Issue-A' },
{ input: '@Issue "A"', expected: 'Issue-A' },
{ input: 'Issue "A"%', expected: 'Issue-A' },
{ input: 'Issue .A', expected: 'Issue-A' },
{ input: 'Issue ,A', expected: 'Issue-A' },
{ input: 'Issue :A', expected: 'Issue-A' },
{ input: 'Issue ;A', expected: 'Issue-A' },
{ input: 'Issue ~A', expected: 'Issue-A' },
{ input: 'Issue #A', expected: 'Issue-A' },
].forEach(testCase => {
it(`Transforms '${testCase.input}' into '${testCase.expected}'`, () => {
const actual = sanitizeIssueTitle(testCase.input);
assert.strictEqual(actual, testCase.expected);
});
});
});
describe('variableSubstitution', () => {
function makeIssueModel(overrides: { title?: string; number?: number; issueType?: string } = {}): IssueModel {
const number = overrides.number ?? 42;
const title = overrides.title ?? 'Some Issue';
return {
number,
title,
item: {
issueType: overrides.issueType,
},
} as unknown as IssueModel;
}
it('replaces ${issueType} with the issue type name', () => {
const result = variableSubstitution('${issueType}-${issueNumber}', makeIssueModel({ issueType: 'Feature', number: 7 }));
assert.strictEqual(result, 'Feature-7');
});
it('replaces ${sanitizedIssueType} with a branch-safe issue type', () => {
const result = variableSubstitution('${sanitizedIssueType}-${issueNumber}', makeIssueModel({ issueType: 'Production Bug Fix', number: 7 }));
assert.strictEqual(result, 'Production-Bug-Fix-7');
});
it('replaces ${sanitizedLowercaseIssueType} with a lowercase branch-safe issue type', () => {
const result = variableSubstitution('${sanitizedLowercaseIssueType}-${issueNumber}', makeIssueModel({ issueType: 'Production Bug Fix', number: 7 }));
assert.strictEqual(result, 'production-bug-fix-7');
});
it('leaves ${issueType} unsubstituted when the issue has no issue type', () => {
const result = variableSubstitution('${issueType}-${issueNumber}', makeIssueModel({ issueType: undefined, number: 7 }));
assert.strictEqual(result, '${issueType}-7');
});
});
});