-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsample_function_test.ts
More file actions
66 lines (55 loc) · 2.13 KB
/
sample_function_test.ts
File metadata and controls
66 lines (55 loc) · 2.13 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
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts";
import { assertEquals, assertExists, assertStringIncludes } from "@std/assert";
import { stub } from "@std/testing/mock";
import SampleFunction from "./sample_function.ts";
const { createContext } = SlackFunctionTester("sample_function");
Deno.test("Sample function test", async () => {
// Replaces globalThis.fetch with the mocked copy
using _stubFetch = stub(
globalThis,
"fetch",
async (url: string | URL | Request, options?: RequestInit) => {
const request = url instanceof Request ? url : new Request(url, options);
assertEquals(request.method, "POST");
assertEquals(request.url, "https://slack.com/api/apps.datastore.put");
const body = await request.formData();
const datastore = body.get("datastore");
const item = body.get("item");
return new Response(
`{"ok": true, "datastore": "${datastore}", "item": ${item}}`,
{
status: 200,
},
);
},
);
const inputs = { message: "Hello, World!", user: "U01234567" };
const { outputs, error } = await SampleFunction(createContext({ inputs }));
assertEquals(error, undefined);
assertEquals(
outputs?.updatedMsg,
":wave: <@U01234567> submitted the following message: \n\n>Hello, World!",
);
});
Deno.test("Sample function datastore error handling", async () => {
// Replaces globalThis.fetch with the mocked copy
using _stubFetch = stub(
globalThis,
"fetch",
(url: string | URL | Request, options?: RequestInit) => {
const request = url instanceof Request ? url : new Request(url, options);
assertEquals(request.method, "POST");
assertEquals(request.url, "https://slack.com/api/apps.datastore.put");
return Promise.resolve(
new Response(`{"ok": false, "error": "datastore_error"}`, {
status: 200,
}),
);
},
);
const inputs = { message: "Hello, World!", user: "U01234567" };
const { outputs, error } = await SampleFunction(createContext({ inputs }));
assertExists(error);
assertStringIncludes(error, "datastore_error");
assertEquals(outputs, undefined);
});