-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.test.js
197 lines (170 loc) · 5.42 KB
/
index.test.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as core from '@actions/core';
import github from '@actions/github';
import * as action from '.';
let inputs = {};
const stories = {
1: { name: '(feat) Story 1', id: '1', story_type: 'bug' },
2: { name: '(feat) Story 2', id: '2', story_type: 'chore' },
};
jest.mock('@useshortcut/client', () => {
class ShortcutClient {
/* eslint-disable class-methods-use-this */
async getStory(id) {
return new Promise((resolve) => {
process.nextTick(() => resolve({ data: stories[id] }));
}).catch(() => 'Error fetching story!');
}
}
return { ShortcutClient };
});
jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
getOctokit: jest.fn(),
}));
github.context = {
payload: {
pull_request: {
title: 'Feature Name',
head: { ref: 'username/sc-1/feature-name' },
},
},
};
jest.spyOn(core, 'getInput').mockImplementation((name) => inputs[name]);
jest.spyOn(core, 'info').mockImplementation(jest.fn());
jest.spyOn(core, 'setFailed').mockImplementation(jest.fn());
jest.spyOn(core, 'setSecret').mockImplementation(jest.fn());
describe('Update Pull Request', () => {
describe('run()', () => {
beforeEach(() => {
inputs = {};
});
test('should exit if no ghToken input', async () => {
await action.run();
expect(core.setFailed).toHaveBeenCalledTimes(1);
});
test('should exit if no chToken input', async () => {
inputs.ghToken = '123';
await action.run();
expect(core.setFailed).toHaveBeenCalledTimes(1);
});
});
describe('Creating the PR Title', () => {
test('should use story name from shortcut as title', async () => {
const prTitle = action.getTitle(
['5678'],
{ name: 'A shortcut story name', story_type: 'feature' },
'sc-',
'sc-',
true
);
expect(prTitle).toEqual('(feature) A shortcut story name [sc-5678]');
});
test('should not use story name from shortcut as title', async () => {
const prTitle = action.getTitle(
['5678'],
{ name: 'A shortcut story name', story_type: 'feature' },
'A PR title that should not be replaced',
'sc-',
true
);
expect(prTitle).toEqual(
'(feature) A PR title that should not be replaced [sc-5678]'
);
});
test('should not add story type when option is false', async () => {
const prTitle = action.getTitle(
['5678'],
{ name: 'A shortcut story name', story_type: 'feature' },
'A PR title that should not be replaced',
'sc-',
false
);
expect(prTitle).toEqual(
'A PR title that should not be replaced [sc-5678]'
);
});
test('should not duplicate story number if already present', async () => {
const prTitle = action.getTitle(
['5678'],
{ name: 'A shortcut story name [sc-5678]', story_type: 'feature' },
'sc-',
'sc-',
true
);
expect(prTitle).toEqual('(feature) A shortcut story name [sc-5678]');
});
test('should not duplicate story type if already present', async () => {
const prTitle = action.getTitle(
['5678'],
{ name: '(feature) A shortcut story name', story_type: 'feature' },
'sc-',
'sc-',
true
);
expect(prTitle).toEqual('(feature) A shortcut story name [sc-5678]');
});
});
describe('getStoryIds', () => {
test('should return storyIds from branchName', () => {
const { pull_request: pullRequest } = github.context.payload;
expect(action.getStoryIds(pullRequest)).toEqual(['1']);
});
test('should return [sc-#] storyIds from PR title', () => {
const pullRequest = {
title: '[sc-2]',
head: { ref: 'i-named-this-in-a-diff-format' },
};
expect(action.getStoryIds(pullRequest)).toEqual(['2']);
});
test('should return sc-# storyIds from PR title', () => {
const pullRequest = {
title: 'sc-2',
head: { ref: 'i-named-this-in-a-diff-format' },
};
expect(action.getStoryIds(pullRequest)).toEqual(['2']);
});
test('should return an empty array if no sc- id in PR title or branchName', () => {
const pullRequest = {
title: 'I have nothing related to sc- ids in my title',
head: { ref: 'i-dont-have-a-sc-id-in-here' },
};
const result = action.getStoryIds(pullRequest);
expect(result).toEqual([]);
});
});
describe('fetchStoryAndUpdatePr', () => {
let params;
beforeAll(() => {
params = {
ghToken: 'fake',
chToken: 'fake',
addStoryType: true,
useStoryNameTrigger: true,
pullRequest: {
head: {
ref: 'username/sc-2/feature-name',
},
title: 'Original Title',
},
repository: {
name: 'api',
owner: {
login: 'username',
},
},
dryRun: false,
};
});
test('should return the story title and update the PR if a story is found', async () => {
await expect(action.fetchStoryAndUpdatePr(params)).resolves.toEqual(
'(chore) Original Title [sc-2]'
);
});
test('should return the original title and not update the PR if a story is not found', async () => {
params.pullRequest.head.ref = 'username/feature-name';
await expect(action.fetchStoryAndUpdatePr(params)).resolves.toEqual(
'Original Title'
);
});
});
});