-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
151 lines (132 loc) · 4.38 KB
/
index.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
"use strict";
const { checkUnitTest } = require("../../../src/plugins/index");
const nock = require("nock");
const probot = require("probot");
const GitHubApi = require("@octokit/rest");
/**
* Mocks a given PR on issue existing with specified files
* @param {string} url The relative URL of the file from the repository root
* @returns {NockScope} A nock scope with the given responses
*/
function mockPrWithFiles(url) {
return nock("https://api.github.com")
.get("/repos/test/repo-test/pulls/1/files")
.reply(200, [
{
blob_url: "https://github.com/test/repo-test/x/b/lime.js"
},
{
blob_url: `https://github.com/test/repo-test/${url}`
}
]);
}
/**
* Emits a bot event for this plugin
* @param {probot.Robot} bot A probot instance
* @param {Object} options Configure the event
* @param {string} options.action The name of the webhook action
* @param {string} options.title The title of the PR
* @returns {Promise<void>} A Promise that fulfills when the webhook completes
*/
function emitBotEvent(bot, { action, title }) {
return bot.receive({
name: "pull_request",
payload: {
installation: {
id: 1
},
action,
pull_request: {
number: 1,
title
},
sender: {
login: "user-a"
},
repository: {
name: "repo-test",
html_url: "https://github.com/test/repo-test",
owner: {
login: "test"
}
}
}
});
}
describe("check-unit-test", () => {
let bot = null;
let nockScope = null;
beforeAll(() => {
bot = new probot.Application({
id: "test",
cert: "test",
cache: {
wrap: () => Promise.resolve({ data: { token: "test" } })
}
});
bot.auth = () => new GitHubApi();
checkUnitTest(bot);
});
beforeEach(() => {
nockScope = nock("https://api.github.com")
.post("/repos/test/repo-test/issues/1/comments")
.reply(201);
});
afterEach(() => {
nock.cleanAll();
});
describe("pull request opened", () => {
test("Add message if the there are no test files", async () => {
mockPrWithFiles("src/make.js");
await emitBotEvent(bot, {
action: "opened",
title: "New: some work done"
});
expect(nockScope.isDone()).toBeTruthy();
});
test("Do not add message if the test folder is present", async () => {
mockPrWithFiles("src/tests/make.js");
await emitBotEvent(bot, {
action: "opened",
title: "New: some work done"
});
expect(nockScope.isDone()).not.toBeTruthy();
});
test("Add message if the test folder is not present tho a file with named test is", async () => {
mockPrWithFiles("src/lib/test.js");
await emitBotEvent(bot, {
action: "opened",
title: "New: some work done"
});
expect(nockScope.isDone()).toBeTruthy();
});
test("Do not add message if the test files are not present and commit is chore", async () => {
mockPrWithFiles("src/make.js");
await emitBotEvent(bot, {
action: "opened",
title: "Chore: some work done"
});
expect(nockScope.isDone()).not.toBeTruthy();
});
});
describe("pull request reopened", () => {
test("Add message if the there are no test files", async () => {
mockPrWithFiles("src/make.js");
await emitBotEvent(bot, {
action: "reopened",
title: "New: some work done"
});
expect(nockScope.isDone()).toBeTruthy();
});
});
describe("pull request synchronize", () => {
test("Add message if the there are no test files", async () => {
mockPrWithFiles("src/make.js");
await emitBotEvent(bot, {
action: "synchronize",
title: "New: some work done"
});
expect(nockScope.isDone()).toBeTruthy();
});
});
});