forked from danger/danger-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_gitlab.test.ts
189 lines (156 loc) · 7.97 KB
/
_gitlab.test.ts
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
import GitLabAPI from "../gitlab/GitLabAPI"
import GitLab from "../GitLab"
import { GitLabDiscussion, GitLabNote, GitLabUser, GitLabUserProfile } from "../../dsl/GitLabDSL"
const baseUri = "https://my-gitlab.org/my-project"
const dangerUserId = 8797215
const getUser = async (): Promise<GitLabUserProfile> => {
const user: Partial<GitLabUserProfile> = { id: dangerUserId }
return user as GitLabUserProfile
}
const dangerID = "dddanger1234"
const dangerIdString = `DangerID: danger-id-${dangerID};`
function mockNote(
id: number,
authorId: number,
body = "",
type: "DiffNote" | "DiscussionNote" | null = "DiffNote"
): GitLabNote {
const author: Partial<GitLabUser> = { id: authorId }
const note: Partial<GitLabNote> = { author: author as GitLabUser, body, id, type: type }
return note as GitLabNote
}
function mockDiscussion(id: string, notes: GitLabNote[]): GitLabDiscussion {
const discussion: Partial<GitLabDiscussion> = { id, notes }
return discussion as GitLabDiscussion
}
function mockApi(withExisting = false): GitLabAPI {
const note1 = mockNote(1001, dangerUserId, `some body ${dangerIdString} asdf`)
const note2 = mockNote(1002, 125235)
const note3 = mockNote(1003, dangerUserId, `Main Danger comment ${dangerIdString} More text to ensure the Danger ID string can be found in the middle of a comment`, null)
const note4 = mockNote(1004, 745774)
const note5 = mockNote(1005, 745774)
const discussion1 = mockDiscussion("aaaaffff1111", [note1, note2])
const discussion2 = mockDiscussion("aaaaffff2222", [note3, note4])
const discussion3 = mockDiscussion("aaaaffff3333", [note5])
const newNote = mockNote(4711, dangerUserId)
const newDiscussion = mockDiscussion("aaaaffff0000", [newNote])
const api: Partial<GitLabAPI> = {
getUser,
createMergeRequestDiscussion: jest.fn(() => Promise.resolve(newDiscussion)),
createMergeRequestNote: jest.fn(() => Promise.resolve(newNote)),
deleteMergeRequestNote: jest.fn(() => Promise.resolve(true)),
getMergeRequestDiscussions: jest.fn(() =>
Promise.resolve(withExisting ? [discussion1, discussion2, discussion3] : [])
),
getMergeRequestNotes: jest.fn(() =>
Promise.resolve(withExisting ? [note1, note2, note3, note4, note5] : [])
),
mergeRequestURL: baseUri,
updateMergeRequestNote: jest.fn(() => Promise.resolve(newNote)),
}
return api as GitLabAPI
}
describe("updateOrCreateComment", () => {
const comment = "my new comment"
it("create a new single comment not using threads", async () => {
delete process.env.DANGER_GITLAB_USE_THREADS
const api = mockApi()
const url = await new GitLab(api as GitLabAPI).updateOrCreateComment(dangerID, comment)
expect(url).toEqual(`${baseUri}#note_4711`)
expect(api.getMergeRequestDiscussions).toHaveBeenCalledTimes(0)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(1)
expect(api.createMergeRequestNote).toHaveBeenCalledWith(comment)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(0)
})
it("create a new single comment using threads", async () => {
process.env.DANGER_GITLAB_USE_THREADS = "true"
const api = mockApi()
const url = await new GitLab(api as GitLabAPI).updateOrCreateComment(dangerID, comment)
expect(url).toEqual(`${baseUri}#note_4711`)
expect(api.getMergeRequestDiscussions).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(1)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledWith(comment)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(0)
})
it("create a new thread", async () => {
process.env.DANGER_GITLAB_USE_THREADS = "true"
const api = mockApi()
const url = await new GitLab(api as GitLabAPI).updateOrCreateComment(dangerID, comment)
expect(url).toEqual(`${baseUri}#note_4711`)
expect(api.getMergeRequestDiscussions).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(1)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledWith(comment)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(0)
})
it("update an existing main note", async () => {
delete process.env.DANGER_GITLAB_USE_THREADS
const api = mockApi(true)
const url = await new GitLab(api as GitLabAPI).updateOrCreateComment(dangerID, comment)
expect(url).toEqual(`${baseUri}#note_4711`)
expect(api.getMergeRequestNotes).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(1)
expect(api.updateMergeRequestNote).toHaveBeenCalledWith(1003, comment)
})
it("update an existing main thread", async () => {
process.env.DANGER_GITLAB_USE_THREADS = "true"
const api = mockApi(true)
const url = await new GitLab(api as GitLabAPI).updateOrCreateComment(dangerID, comment)
expect(url).toEqual(`${baseUri}#note_4711`)
expect(api.getMergeRequestDiscussions).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(2)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(1, 1003)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(2, 1004)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(1)
expect(api.updateMergeRequestNote).toHaveBeenCalledWith(1001, comment)
})
})
describe("deleteMainComment", () => {
it("delete nothing", async () => {
const api = mockApi()
const result = await new GitLab(api as GitLabAPI).deleteMainComment(dangerID)
expect(result).toEqual(false)
expect(api.getMergeRequestDiscussions).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(0)
})
it("delete all danger attached notes not using threads", async () => {
delete process.env.DANGER_GITLAB_USE_THREADS
const api = mockApi(true)
const result = await new GitLab(api as GitLabAPI).deleteMainComment(dangerID)
expect(result).toEqual(true)
expect(api.getMergeRequestNotes).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(1, 1003)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(0)
})
it("delete all danger attached notes using threads", async () => {
process.env.DANGER_GITLAB_USE_THREADS = "true"
const api = mockApi(true)
const result = await new GitLab(api as GitLabAPI).deleteMainComment(dangerID)
expect(result).toEqual(true)
expect(api.getMergeRequestDiscussions).toHaveBeenCalledTimes(1)
expect(api.deleteMergeRequestNote).toHaveBeenCalledTimes(4)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(1, 1001)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(2, 1002)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(3, 1003)
expect(api.deleteMergeRequestNote).toHaveBeenNthCalledWith(4, 1004)
expect(api.createMergeRequestDiscussion).toHaveBeenCalledTimes(0)
expect(api.createMergeRequestNote).toHaveBeenCalledTimes(0)
expect(api.updateMergeRequestNote).toHaveBeenCalledTimes(0)
})
})