-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmergify.test.js
More file actions
329 lines (255 loc) · 10.5 KB
/
mergify.test.js
File metadata and controls
329 lines (255 loc) · 10.5 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const {
MergifyCache,
findTimelineActions,
isPullRequestOpen,
isMergifyEnabledOnTheRepo,
getMergifyConfigurationStatus,
} = require("../mergify");
const { loadFixture, injectFixtureInDOM } = require("./utils");
describe("MergifyCache", () => {
beforeEach(() => {
localStorage.clear();
jest.spyOn(Date, "now").mockImplementation(() => 1000);
const mockFetch = jest.fn().mockResolvedValue({
text: jest.fn().mockResolvedValue(loadFixture("searchConfigFound")),
});
global.fetch = mockFetch;
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should return null if the cache is empty", () => {
const mergifyCache = new MergifyCache();
expect(mergifyCache.get("foo", "my-repo")).toBeNull();
});
it("should return the cache", () => {
const mergifyCache = new MergifyCache();
mergifyCache.update("foo", "my-repo", true);
expect(mergifyCache.get("foo", "my-repo")).toBe(true);
});
it("should use proper keys", () => {
const mergifyCache = new MergifyCache();
mergifyCache.update("org1", "repo1", 1);
mergifyCache.update("org1", "repo2", 2);
mergifyCache.update("org2", "repo1", 3);
expect(mergifyCache.get("org1", "repo1")).toBe(1);
expect(mergifyCache.get("org1", "repo2")).toBe(2);
expect(mergifyCache.get("org2", "repo1")).toBe(3);
});
it("should expire cache entries", () => {
const mergifyCache = new MergifyCache(500); // 500ms expiration
mergifyCache.update("foo", "my-repo", true);
expect(mergifyCache.get("foo", "my-repo")).toBe(true);
// Advance time beyond expiration
Date.now.mockImplementation(() => 1501);
expect(mergifyCache.get("foo", "my-repo")).toBeNull();
});
it("should handle invalid JSON in cache", () => {
const mergifyCache = new MergifyCache();
const key = mergifyCache.key("foo", "my-repo");
localStorage.setItem(key, "not-valid-json");
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
expect(mergifyCache.get("foo", "my-repo")).toBeNull();
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
});
describe("findTimelineActions", () => {
afterEach(() => {
document.body.innerHTML = "";
});
it("should find the new merge box on opened pull requests", () => {
injectFixtureInDOM("github_pr_opened");
const mergeBox = findTimelineActions();
expect(mergeBox).not.toBeUndefined();
expect(mergeBox.tagName).toBe("DIV");
expect(mergeBox.innerHTML).toMatch(/<section aria-label="Reviews"/);
expect(mergeBox.innerHTML).toMatch(/<section aria-label="Checks"/);
});
it("should find the new merge box on merged pull requests", () => {
injectFixtureInDOM("github_pr_merged");
const mergeBox = findTimelineActions();
expect(mergeBox).not.toBeUndefined();
expect(mergeBox.tagName).toBe("DIV");
expect(mergeBox.innerHTML).toMatch(
/Pull\s+request\s+successfully\s+merged\s+and\s+closed/,
);
});
});
describe("isPullRequestOpen", () => {
afterEach(() => {
document.body.innerHTML = "";
});
it("should detect open PR via legacy span.State (fixture)", () => {
injectFixtureInDOM("github_pr_opened");
const status = isPullRequestOpen();
expect(status).toBe(true);
});
it("should detect merged PR via legacy span.State (fixture)", () => {
injectFixtureInDOM("github_pr_merged");
const status = isPullRequestOpen();
expect(status).toBe(false);
});
it("should detect open PR via data-status=pullOpened attribute", () => {
document.body.innerHTML = '<span data-status="pullOpened">Open</span>';
expect(isPullRequestOpen()).toBe(true);
});
it("should detect draft PR via data-status=draft attribute", () => {
document.body.innerHTML = '<span data-status="draft">Draft</span>';
expect(isPullRequestOpen()).toBe(true);
});
it("should prefer data-status over legacy span.State", () => {
document.body.innerHTML =
'<span data-status="pullOpened">Open</span>' +
'<span class="State" title="Status: Closed">Closed</span>';
expect(isPullRequestOpen()).toBe(true);
});
it("should detect closed PR via legacy span.State when no data-status", () => {
document.body.innerHTML =
'<span class="State" title="Status: Closed">Closed</span>';
expect(isPullRequestOpen()).toBe(false);
});
it("should assume open when no status element is found", () => {
document.body.innerHTML = "<div>No status here</div>";
expect(isPullRequestOpen()).toBe(true);
});
it("should assume open when legacy span.State has no parseable title", () => {
const consoleSpy = jest
.spyOn(console, "warn")
.mockImplementation(() => {});
document.body.innerHTML =
'<span class="State" title="Malformed">Badge</span>';
expect(isPullRequestOpen()).toBe(true);
expect(consoleSpy).toHaveBeenCalledWith(
"Can't find pull request status",
);
consoleSpy.mockRestore();
});
});
describe("isMergifyEnabledOnTheRepo caching behavior", () => {
beforeEach(() => {
localStorage.clear();
// Mock document.location
delete window.location;
window.location = new URL(
"https://github.com/cypress-io/cypress/pull/32277",
);
});
afterEach(() => {
document.body.innerHTML = "";
localStorage.clear();
});
it("should return true if Mergify is enabled on the repo with config", () => {
injectFixtureInDOM("github_pr_opened");
const isEnabled = isMergifyEnabledOnTheRepo(true);
expect(isEnabled).toBe(true);
});
it("should return true if Mergify is enabled on the repo with no config", () => {
injectFixtureInDOM("github_pr_opened");
const isEnabled = isMergifyEnabledOnTheRepo(false);
expect(isEnabled).toBe(true);
});
it("should return false if Mergify is not enabled on the repo", () => {
injectFixtureInDOM("github_pr_no_mergify");
const isEnabled = isMergifyEnabledOnTheRepo(false);
expect(isEnabled).toBe(false);
});
it("should still return true if cache have false and the repo is enabled", () => {
injectFixtureInDOM("github_pr_opened");
const cache = new MergifyCache();
cache.update("cypress-io", "cypress", false);
const isEnabled = isMergifyEnabledOnTheRepo(true);
expect(isEnabled).toBe(true);
});
});
describe("getMergifyConfigurationStatus", () => {
beforeEach(() => {
localStorage.clear();
// Mock window.location for pull request data
//
delete window.location;
window.location = new URL(
"https://github.com/test-org/test-repo/pull/123",
);
});
afterEach(() => {
localStorage.clear();
jest.restoreAllMocks();
});
it("should return true if configuration is found in cache", async () => {
const cache = new MergifyCache();
cache.update("test-org", "test-repo", true);
const result = await getMergifyConfigurationStatus();
expect(result).toBe(true);
});
it("should return false if configuration is not found in cache and no config files exist", async () => {
const mockFetch = jest.fn().mockResolvedValue({
text: jest
.fn()
.mockResolvedValue(loadFixture("searchConfigNotFound")),
});
global.fetch = mockFetch;
const result = await getMergifyConfigurationStatus();
expect(result).toBe(false);
expect(mockFetch).toHaveBeenCalledWith(
"/search?q=repo%3Atest-org%2Ftest-repo+%28.mergify.yml+OR+.mergify%2Fconfig.yml+OR+.github%2Fmergify.yml%29&type=code",
);
});
it("should return true if configuration files are found via search", async () => {
const mockFetch = jest.fn().mockResolvedValue({
text: jest.fn().mockResolvedValue(loadFixture("searchConfigFound")),
});
global.fetch = mockFetch;
const result = await getMergifyConfigurationStatus();
expect(result).toBe(true);
expect(mockFetch).toHaveBeenCalledWith(
"/search?q=repo%3Atest-org%2Ftest-repo+%28.mergify.yml+OR+.mergify%2Fconfig.yml+OR+.github%2Fmergify.yml%29&type=code",
);
});
it("should update cache when search result differs from cached value", async () => {
const cache = new MergifyCache();
cache.update("test-org", "test-repo", false);
const mockFetch = jest.fn().mockResolvedValue({
text: jest.fn().mockResolvedValue(loadFixture("searchConfigFound")),
});
global.fetch = mockFetch;
const result = await getMergifyConfigurationStatus();
expect(result).toBe(true);
expect(cache.get("test-org", "test-repo")).toBe(true);
});
it("should handle malformed HTML response gracefully", async () => {
const mockFetch = jest.fn().mockResolvedValue({
text: jest.fn().mockResolvedValue("<html><body></body></html>"),
});
global.fetch = mockFetch;
const result = await getMergifyConfigurationStatus();
expect(result).toBe(false);
});
it("should handle fetch errors gracefully", async () => {
const mockFetch = jest
.fn()
.mockRejectedValue(new Error("Network error"));
global.fetch = mockFetch;
await expect(getMergifyConfigurationStatus()).rejects.toThrow(
"Network error",
);
});
it("should not update cache if cached value matches search result", async () => {
const cache = new MergifyCache();
cache.update("test-org", "test-repo", true);
const cacheSpy = jest.spyOn(cache, "update");
// Mock MergifyCache constructor to return our spy
jest.spyOn(require("../mergify"), "MergifyCache").mockImplementation(
() => cache,
);
const mockFetch = jest.fn().mockResolvedValue({
text: jest.fn().mockResolvedValue(loadFixture("searchConfigFound")),
});
global.fetch = mockFetch;
const result = await getMergifyConfigurationStatus();
expect(result).toBe(true);
expect(cacheSpy).not.toHaveBeenCalled();
});
});