-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcreate_issue_test.ts
More file actions
63 lines (58 loc) · 1.96 KB
/
create_issue_test.ts
File metadata and controls
63 lines (58 loc) · 1.96 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
import { assertEquals } from "@std/assert";
import { stub } from "@std/testing/mock";
import { SlackFunctionTester } from "@slack/sdk";
import handler from "./create_issue.ts";
/**
* The actual outputs of a function can be compared to expected outputs for a
* collection of given inputs.
*/
const { createContext } = SlackFunctionTester("create_issue");
const env = { logLevel: "CRITICAL" };
Deno.test("Create a GitHub issue with given inputs", async () => {
/**
* Mocked responses of Slack API and external endpoints can be set with
* mock_fetch.
*/
using _fetchStub = stub(
globalThis,
"fetch",
(url: string | URL | Request, options?: RequestInit) => {
const req = url instanceof Request ? url : new Request(url, options);
assertEquals(req.method, "POST");
switch (req.url) {
case "https://slack.com/api/apps.auth.external.get":
return Promise.resolve(
new Response(`{"ok": true, "external_token": "example-token"}`),
);
case "https://api.github.com/repos/slack-samples/deno-github-functions/issues":
return Promise.resolve(
new Response(
`{"number": 123, "html_url": "https://www.example.com/expected-html-url"}`,
{
status: 201,
},
),
);
default:
throw Error(
`No stub found for ${req.method} ${req.url}\nHeaders: ${
JSON.stringify(Object.fromEntries(req.headers.entries()))
}`,
);
}
},
);
const inputs = {
githubAccessTokenId: {},
url: "https://github.com/slack-samples/deno-github-functions",
title: "The issue title",
description: "issue description",
assignees: "batman",
};
const { outputs } = await handler(createContext({ inputs, env }));
assertEquals(outputs?.GitHubIssueNumber, 123);
assertEquals(
outputs?.GitHubIssueLink,
"https://www.example.com/expected-html-url",
);
});