-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gracefully handle errors from the
datastore.put
response (#48)
- Loading branch information
Showing
2 changed files
with
44 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { | ||
assertEquals, | ||
assertExists, | ||
assertStringIncludes, | ||
} from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import SampleFunction from "./sample_function.ts"; | ||
import * as mf from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
|
@@ -8,9 +12,14 @@ const { createContext } = SlackFunctionTester("sample_function"); | |
// Replaces globalThis.fetch with the mocked copy | ||
mf.install(); | ||
|
||
mf.mock("POST@/api/apps.datastore.put", () => { | ||
// Shared mocks can be defined at the top level of tests | ||
mf.mock("POST@/api/apps.datastore.put", async (args) => { | ||
const body = await args.formData(); | ||
const datastore = body.get("datastore"); | ||
const item = body.get("item"); | ||
|
||
return new Response( | ||
`{"ok": true, "item": {"object_id": "d908f8bd-00c6-43f0-9fc3-4da3c2746e14"}}`, | ||
`{"ok": true, "datastore": "${datastore}", "item": ${item}}`, | ||
{ | ||
status: 200, | ||
}, | ||
|
@@ -19,9 +28,27 @@ mf.mock("POST@/api/apps.datastore.put", () => { | |
|
||
Deno.test("Sample function test", async () => { | ||
const inputs = { message: "Hello, World!", user: "U01234567" }; | ||
const { outputs } = await SampleFunction(createContext({ inputs })); | ||
await assertEquals( | ||
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 () => { | ||
// Mocks specific to a test can be overriden within a test | ||
mf.mock("POST@/api/apps.datastore.put", () => { | ||
return 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); | ||
}); |