Skip to content

Commit 2d4e151

Browse files
committed
Update internalLink unit tests
1 parent cd8f06e commit 2d4e151

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

org/allPRs.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ export const prSize = async () => {
1111
export const internalLink = async () => {
1212
const regex = /https:\/\/app.asana.com\/[0-9]\/[0-9]*\/([0-9]*)/
1313

14+
let hasLink = false;
1415
// Warn when link to internal task is missing
1516
for (let bodyLine of danger.github.pr.body.toLowerCase().split(/\n/)) {
1617
if (bodyLine.includes("task/issue url:")) {
1718

1819
let match = bodyLine.match(regex);
1920
if (!match || match.length < 2) {
2021
fail("Please, don't forget to add a link to the internal task");
22+
return;
2123
}
2224

25+
hasLink = true;
26+
2327
let taskGID = match[1];
2428

2529
if (process.env.ASANA_ACCESS_TOKEN && process.env.ASANA_PROJECT_ID && process.env.ASANA_PROJECT_NAME) {
@@ -32,10 +36,19 @@ export const internalLink = async () => {
3236
let projects = result.data.projects.map( (p) => p.gid );
3337
if (!projects.includes(process.env.ASANA_PROJECT_ID)) {
3438
fail(`Please ensure that the Asana task is added to ${process.env.ASANA_PROJECT_NAME} project`);
39+
return;
3540
}
3641
}
42+
43+
if (hasLink) {
44+
break;
45+
}
3746
}
3847
}
48+
49+
if (!hasLink) {
50+
fail("Please, don't forget to add a link to the internal task");
51+
}
3952
}
4053

4154
export const xcodeprojConfiguration = async () => {

tests/internalLink.allPRs.test.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
jest.mock("danger", () => jest.fn())
2+
jest.mock("Asana", () => jest.fn())
23
import danger from 'danger'
4+
import Asana from 'asana'
35
const dm = danger as any;
6+
const a = Asana as any;
47

58
import { internalLink } from '../org/allPRs'
69

@@ -11,17 +14,40 @@ beforeEach(() => {
1114
dm.danger = {
1215
github: {
1316
pr: {
14-
body: 'task/issue url: https://app.asana.com/someurl/0001'
17+
body: 'task/issue url: https://app.asana.com/0/0001/0002/f'
1518
}
1619
},
1720
}
21+
22+
a.ApiClient = {
23+
instance: {
24+
authentications: {
25+
token: {
26+
accessToken: '1234'
27+
}
28+
}
29+
}
30+
}
31+
32+
a.TasksApi = jest.fn().mockImplementation(() => {
33+
return {
34+
getTask: jest.fn().mockReturnValue({
35+
data: {
36+
projects: [
37+
{ gid: '1234' },
38+
{ gid: '0001' },
39+
]
40+
}
41+
})
42+
}
43+
})
1844
})
1945

2046
describe("Internal Asana link test", () => {
2147
it("does not fail when the description contains a link to Asana", async () => {
2248
await internalLink()
2349

24-
expect(dm.warn).not.toHaveBeenCalled()
50+
expect(dm.fail).not.toHaveBeenCalled()
2551
})
2652

2753
it("fails when the description doesn't contain a link to Asana", async () => {
@@ -31,4 +57,40 @@ describe("Internal Asana link test", () => {
3157

3258
expect(dm.fail).toHaveBeenCalledWith("Please, don't forget to add a link to the internal task")
3359
})
60+
61+
it("fails when the description contains a malformed link to Asana", async () => {
62+
dm.danger.github.pr.body = 'task/issue url: https://app.asana.com/000010002/f'
63+
64+
await internalLink();
65+
66+
expect(dm.fail).toHaveBeenCalledWith("Please, don't forget to add a link to the internal task")
67+
})
68+
69+
it("fails when the description doesn't contain a line with task/issue URL", async () => {
70+
dm.danger.github.pr.body = 'sample random body'
71+
72+
await internalLink();
73+
74+
expect(dm.fail).toHaveBeenCalledWith("Please, don't forget to add a link to the internal task")
75+
})
76+
77+
it("does not fail when the description contains a link to Asana task in a correct project", async () => {
78+
process.env.ASANA_ACCESS_TOKEN = '1234'
79+
process.env.ASANA_PROJECT_ID = '0001'
80+
process.env.ASANA_PROJECT_NAME = 'macOS App Board'
81+
82+
await internalLink();
83+
84+
expect(dm.fail).not.toHaveBeenCalled()
85+
})
86+
87+
it("fails when the description contains a link to Asana task not in a correct project", async () => {
88+
process.env.ASANA_ACCESS_TOKEN = '1234'
89+
process.env.ASANA_PROJECT_ID = '9999'
90+
process.env.ASANA_PROJECT_NAME = 'macOS App Board'
91+
92+
await internalLink();
93+
94+
expect(dm.fail).toHaveBeenCalledWith("Please ensure that the Asana task is added to macOS App Board project")
95+
})
3496
})

0 commit comments

Comments
 (0)