forked from mheap/require-checklist-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
175 lines (139 loc) · 5.06 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
const { Toolkit } = require("actions-toolkit");
const nock = require("nock");
nock.disableNetConnect();
process.env.GITHUB_WORKFLOW = "demo-workflow";
process.env.GITHUB_ACTION = "require-checklist-action";
process.env.GITHUB_ACTOR = "YOUR_USERNAME";
process.env.GITHUB_REPOSITORY = "YOUR_USERNAME/action-test";
process.env.GITHUB_WORKSPACE = "/tmp/github/workspace";
process.env.GITHUB_SHA = "fake-sha-a1c85481edd2ea7d19052874ea3743caa8f1bdf6";
describe("Require Checklist", () => {
let action, tools;
Toolkit.run = jest.fn((actionFn) => {
action = actionFn;
});
require(".");
beforeEach(() => {
jest.resetModules();
tools = mockEvent("pull_request", {
action: "opened",
pull_request: { number: 17 },
});
});
it("handles issues with no checklist, requireChecklist disabled", async () => {
process.env.INPUT_REQUIRECHECKLIST = "false";
mockIssueBody("No checklist in the body");
mockIssueComments(["Or in the comments"]);
tools.exit.success = jest.fn();
await action(tools);
expect(tools.exit.success).toBeCalledWith(
"There are no incomplete task list items"
);
});
it("handles issues with completed checklist", async () => {
process.env.INPUT_REQUIRECHECKLIST = "true";
mockIssueBody("Demo\r\n\r\n- [x] One\r\n- [x] Two\n- [x] Three");
mockIssueComments(["- [x] Comment done"]);
tools.log.success = jest.fn();
tools.exit.success = jest.fn();
await action(tools);
expect(tools.log.success).toBeCalledWith("Completed task list item: One");
expect(tools.log.success).toBeCalledWith("Completed task list item: Two");
expect(tools.log.success).toBeCalledWith("Completed task list item: Three");
expect(tools.log.success).toBeCalledWith(
"Completed task list item: Comment done"
);
expect(tools.exit.success).toBeCalledWith(
"There are no incomplete task list items"
);
});
it("handles issues with no checklist, requireChecklist enabled", async () => {
process.env.INPUT_REQUIRECHECKLIST = "true";
mockIssueBody("No checklist in the body");
mockIssueComments(["Or in the comments"]);
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.exit.failure).toBeCalledWith(
"No task list was present and requireChecklist is turned on"
);
});
it("handles incomplete checklist in body", async () => {
mockIssueBody("Demo\r\n\r\n- [x] One\r\n- [ ] Two\n- [ ] Three");
mockIssueComments(["No checklist in comment"]);
tools.log.success = jest.fn();
tools.log.fatal = jest.fn();
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.log.success).toBeCalledWith("Completed task list item: One");
expect(tools.log.fatal).toBeCalledWith("Incomplete task list item: Two");
expect(tools.log.fatal).toBeCalledWith("Incomplete task list item: Three");
expect(tools.exit.failure).toBeCalledWith(
"The following items are not marked as completed: Two, Three"
);
});
it("handles incomplete checklist in comments", async () => {
mockIssueBody("Nothing in the body");
mockIssueComments(["Demo\r\n\r\n- [x] One\r\n- [ ] Two\n- [ ] Three"]);
tools.log.success = jest.fn();
tools.log.fatal = jest.fn();
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.log.success).toBeCalledWith("Completed task list item: One");
expect(tools.log.fatal).toBeCalledWith("Incomplete task list item: Two");
expect(tools.log.fatal).toBeCalledWith("Incomplete task list item: Three");
expect(tools.exit.failure).toBeCalledWith(
"The following items are not marked as completed: Two, Three"
);
});
it("handles issues with empty body, requireChecklist disabled", async () => {
process.env.INPUT_REQUIRECHECKLIST = "false";
mockIssueBody(null);
mockIssueComments(["No checklist in comment"]);
tools.exit.success = jest.fn();
await action(tools);
expect(tools.exit.success).toBeCalledWith(
"There are no incomplete task list items"
);
});
it("handles issues with empty body, requireChecklist enabled", async () => {
process.env.INPUT_REQUIRECHECKLIST = "true";
mockIssueBody(null);
mockIssueComments(["No checklist in comment"]);
tools.exit.failure = jest.fn();
await action(tools);
expect(tools.exit.failure).toBeCalledWith(
"No task list was present and requireChecklist is turned on"
);
});
});
function mockIssueBody(body) {
nock("https://api.github.com")
.get("/repos/YOUR_USERNAME/action-test/issues/17")
.reply(200, {
body,
});
}
function mockIssueComments(comments) {
nock("https://api.github.com")
.get("/repos/YOUR_USERNAME/action-test/issues/17/comments")
.reply(
200,
comments.map((c) => {
return { body: c };
})
);
}
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();
}