forked from ytanikin/pr-conventional-commits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
178 lines (152 loc) · 5.88 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
const { getInput, setFailed } = require('@actions/core');
const { getOctokit, context } = require('@actions/github');
const toConventionalChangelogFormat = require('conventional-commits-parser');
jest.mock('@actions/core');
jest.mock('@actions/github');
const myModule = require('./index');
const utils = require('./utils');
beforeEach(() => {
jest.resetAllMocks();
});
let logMock;
beforeEach(() => {
logMock = jest.spyOn(console, 'log');
logMock.mockImplementation(() => {});
});
afterEach(() => {
logMock.mockRestore();
});
describe('checkConventionalCommits', () => {
it('should succeed when a valid task type is provided', async () => {
getInput.mockReturnValue(JSON.stringify(['feat', 'fix']));
context.payload = {
pull_request: { title: 'feat(login): add new login feature' }
};
const commitDetail = await myModule.checkConventionalCommits();
expect(setFailed).not.toHaveBeenCalled();
expect(commitDetail).toEqual({
type: 'feat',
scope: 'login',
breaking: false
});
});
it('should succeed when a valid task type is provided and breaking change', async () => {
getInput.mockReturnValue(JSON.stringify(['feat', 'fix']));
context.payload = {
pull_request: { title: 'feat(login)!: add new login feature' }
};
const commitDetail = await myModule.checkConventionalCommits();
expect(setFailed).not.toHaveBeenCalled();
expect(commitDetail).toEqual({
type: 'feat',
scope: 'login',
breaking: true
});
});
it('should fail when task_types input is missing', async () => {
getInput.mockReturnValue('');
await myModule.checkConventionalCommits();
expect(setFailed).toHaveBeenCalledWith('Missing required input: task_types');
});
it('should fail when task_types input is invalid JSON', async () => {
getInput.mockReturnValue('invalid JSON');
await myModule.checkConventionalCommits();
expect(setFailed).toHaveBeenCalledWith('Invalid task_types input. Expecting a JSON array.');
});
});
describe('checkTicketNumber', () => {
it('should fail when ticket number is missing or invalid', async () => {
getInput.mockReturnValue('\\d+');
context.payload = {
pull_request: { title: 'no number here' }
};
await myModule.checkTicketNumber();
expect(setFailed).toHaveBeenCalledWith('Invalid or missing task number: \'\'. Must match: \\d+');
});
});
describe('applyLabel', () => {
beforeEach(() => {
// Mock the context.repo object to provide owner and repo values
context.repo = {
owner: 'mockOwner',
repo: 'mockRepo',
};
});
it('should skip label addition if add_label is set to false', async () => {
getInput.mockReturnValue('false');
await myModule.applyLabel({}, {});
expect(setFailed).not.toHaveBeenCalled();
});
it('should fail if custom_labels input is invalid JSON', async () => {
getInput.mockReturnValueOnce('true').mockReturnValueOnce('invalid JSON');
await myModule.applyLabel({}, {});
expect(setFailed).toHaveBeenCalledWith('Invalid custom_labels input. Unable to parse JSON.');
});
it('should remove existing labels that are in the managed list but not in the new labels', async () => {
const mockOctokit = {
rest: {
issues: {
listLabelsOnIssue: jest.fn().mockResolvedValue({
data: [
{ name: 'feat' },
{ name: 'fix' },
{ name: 'breaking change' },
],
}),
removeLabel: jest.fn().mockResolvedValue({}),
addLabels: jest.fn().mockResolvedValue({}),
},
},
};
getInput.mockImplementation((inputName) => {
if (inputName === 'task_types') {
return JSON.stringify(['feat', 'fix']);
}
if (inputName === 'token') {
return 'token';
}
return undefined;
});
getOctokit.mockReturnValue(mockOctokit);
const pr = {
number: 123,
};
const commitDetail = {
type: 'fix',
breaking: false,
};
const customLabels = {};
getInput.mockReturnValueOnce(JSON.stringify(['feat', 'fix'])); // task_types
getOctokit.mockReturnValue(mockOctokit);
// Directly call the updateLabels function
await myModule.updateLabels(pr, commitDetail, customLabels, "feat", "custom_labels");
// Assert removeLabel was called for 'feat' and 'breaking change'
expect(mockOctokit.rest.issues.removeLabel).toHaveBeenCalledWith({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name: 'feat',
});
expect(mockOctokit.rest.issues.removeLabel).toHaveBeenCalledWith({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name: 'breaking change',
});
});
});
describe('generateColor', () => {
it('should return a string', () => {
expect(typeof utils.generateColor('test')).toBe('string');
});
it('should generate different colors for different inputs', () => {
const color1 = utils.generateColor('test1');
const color2 = utils.generateColor('test2');
expect(color1).not.toEqual(color2);
});
it('should generate the same colors for different inputs', () => {
const color1 = utils.generateColor('test1');
const color2 = utils.generateColor('test1');
expect(color1).toEqual(color2);
});
});