-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
241 lines (217 loc) · 5.9 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const { Toolkit } = require("actions-toolkit");
const mockedEnv = require("mocked-env");
const nock = require("nock");
nock.disableNetConnect();
const defaultRelease = {
action: "published",
release: {
name: "v1.2.3",
body: "This is an example release body",
html_url: "https://github.com/nexmo/fake-test-repo/releases/v1.2.3",
published_at: "2019-05-15T15:20:53Z",
},
repository: {
name: "fake-test-repo",
},
};
describe("GitHub Release to Noticeable", () => {
let action, restore, restoreTest;
// Mock Toolkit.run to define `action` so we can call it
Toolkit.run = jest.fn((actionFn) => {
action = actionFn;
});
require(".");
beforeEach(() => {
jest.resetModules();
// Default in case the test does not use per-test environment vars
restoreTest = () => {};
restore = mockedEnv({
GITHUB_WORKFLOW: "Noticeable Release",
GITHUB_ACTION: "Noticeable Release Action",
GITHUB_ACTOR: "NexmoDev",
GITHUB_WORKSPACE: "/tmp",
GITHUB_SHA: "fake-sha-abc-123",
GITHUB_REPOSITORY: "nexmo/fake-test-repo",
GITHUB_EVENT_NAME: "",
GITHUB_EVENT_PATH: "",
NOTICEABLE_PROJECT_ID: "Example-Project-ID",
NOTICEABLE_API_KEY: "Noticeable-API-123",
});
});
afterEach(() => {
restore();
restoreTest();
if (!nock.isDone()) {
throw new Error(
`Not all HTTP mocks have been used:\n\n${nock.pendingMocks()}`
);
nock.cleanAll();
}
});
it("exits if the project ID is missing", async () => {
restoreTest = mockedEnv({
NOTICEABLE_PROJECT_ID: undefined,
});
const tools = mockEvent("release", defaultRelease);
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.exit.failure).toHaveBeenCalledWith(
"Missing 'NOTICEABLE_PROJECT_ID' environment variable"
);
});
it("exits if the noticeable API key is missing", async () => {
restoreTest = mockedEnv({
NOTICEABLE_API_KEY: undefined,
});
const tools = mockEvent("release", defaultRelease);
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.exit.failure).toHaveBeenCalledWith(
"Missing 'NOTICEABLE_API_KEY' environment variable"
);
});
it("stops execution when the event is not published", async () => {
const tools = mockEvent("release", {
...defaultRelease,
action: "deleted",
});
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.exit.failure).toHaveBeenCalledWith(
"Release status is not in 'published'. Got 'deleted'"
);
});
for (let t of [
{
name: "runs with the default parameters",
env: {},
variables: {},
},
{
name: "adds a title prefix if the RELEASE_PREFIX input is provided",
env: {
INPUT_RELEASE_PREFIX: "demo-project",
},
variables: {
title: "demo-project v1.2.3",
},
},
{
name:
"does not append the GitHub release link if the DISABLE_REPO_LINK input is provided",
env: {
INPUT_DISABLE_REPO_LINK: "true",
},
variables: {
content: "This is an example release body",
},
},
{
name: "adds the beta label if it is a prerelease",
event: {
release: {
...defaultRelease.release,
prerelease: true,
},
},
variables: {
labels: [{ name: "Beta" }],
},
},
{
name: "adds as a draft if the DRAFT input is set",
env: {
INPUT_DRAFT: "true",
},
variables: {
draft: true,
},
},
]) {
it(t.name, async () => {
restoreTest = mockedEnv(t.env || {});
const tools = mockEvent("release", {
...defaultRelease,
...(t.event || {}),
});
tools.exit.success = jest.fn();
mockCreatePost(t.variables);
await action(tools);
expect(tools.exit.success).toHaveBeenCalledWith(
"Changelog entry created"
);
});
}
});
function mockEvent(name, mockPayload) {
jest.mock(
"/github/workspace/event.json",
() => {
return mockPayload;
},
{
virtual: true,
}
);
process.env.GITHUB_EVENT_NAME = name;
process.env.GITHUB_EVENT_PATH = "/github/workspace/event.json";
return new Toolkit();
}
function mockCreatePost(variables) {
nock("https://api.noticeable.io", {
reqheaders: {
Authorization: "Apikey Noticeable-API-123",
},
})
.post("/graphql", {
query:
"\n" +
" mutation AddEntry(\n" +
" $project: ID!\n" +
" $title: String!\n" +
" $content: String!\n" +
" $draft: Boolean!\n" +
" $created: DateTime!\n" +
" $labels: [LabelInput]\n" +
" ) {\n" +
" createPost(\n" +
" input: {\n" +
" projectId: $project\n" +
' author: { fullName: "Vonage" }\n' +
" content: $content\n" +
" isDraft: $draft\n" +
" labels: $labels\n" +
" publicationTime: $created\n" +
" title: $title\n" +
" }\n" +
" ) {\n" +
" post {\n" +
" id\n" +
" permalink\n" +
" }\n" +
" }\n" +
" }\n" +
" ",
variables: {
project: "Example-Project-ID",
title: "v1.2.3",
content:
"This is an example release body\n\nhttps://github.com/nexmo/fake-test-repo/releases/v1.2.3",
draft: false,
created: "2019-05-15T15:20:53.000Z",
labels: [],
...variables,
},
})
.reply(200, {
data: {
createPost: {
post: {
id: "8bJYiSW4IJbN7Iru93Nq",
permalink:
"https://timeline.noticeable.io/IiJ4ZSyjbeAdrXpWHGng/posts/demo-post-8bjyisw4ijbn7iru93nq",
},
},
},
});
}