-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tests to base-client and instructions on how to run tests to t…
…he README. (#3)
- Loading branch information
Fil Maj
authored
Apr 6, 2022
1 parent
ab35a7b
commit 217aca1
Showing
4 changed files
with
176 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
scripts/api_spec.json | ||
.coverage |
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,11 +1,167 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { | ||
assertEquals, | ||
assertRejects, | ||
} from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import * as mf from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { SlackAPI } from "./mod.ts"; | ||
import { serializeData } from "./base-client.ts"; | ||
|
||
Deno.test("SlackAPI", () => { | ||
const client = SlackAPI("test-token", { | ||
slackApiUrl: "https://slack.com/api/", | ||
Deno.test("SlackAPI class", async (t) => { | ||
mf.install(); // mock out calls to `fetch` | ||
|
||
await t.step("instantiated with default API URL", async (t) => { | ||
const client = SlackAPI("test-token", {}); | ||
|
||
await t.step("base methods exist on client", () => { | ||
assertEquals(typeof client.apiCall, "function"); | ||
assertEquals(typeof client.response, "function"); | ||
}); | ||
|
||
await t.step("apiCall method", async (t) => { | ||
await t.step("should call the default API URL", async () => { | ||
mf.mock("POST@/api/chat.postMessage", (req: Request) => { | ||
assertEquals(req.url, "https://slack.com/api/chat.postMessage"); | ||
return new Response('{"ok":true}'); | ||
}); | ||
|
||
await client.apiCall("chat.postMessage", {}); | ||
|
||
mf.reset(); | ||
}); | ||
|
||
await t.step( | ||
"should prioritize calling provided token vs. token instantiated client with", | ||
async () => { | ||
mf.mock("POST@/api/chat.postMessage", (req: Request) => { | ||
assertEquals(req.headers.get("authorization"), "Bearer override"); | ||
return new Response('{"ok":true}'); | ||
}); | ||
|
||
await client.apiCall("chat.postMessage", { token: "override" }); | ||
|
||
mf.reset(); | ||
|
||
mf.mock("POST@/api/chat.postMessage", (req: Request) => { | ||
assertEquals(req.headers.get("authorization"), "Bearer test-token"); | ||
return new Response('{"ok":true}'); | ||
}); | ||
|
||
await client.apiCall("chat.postMessage", {}); | ||
|
||
mf.reset(); | ||
}, | ||
); | ||
|
||
await t.step( | ||
"should throw if response returns an HTTP status code >= 400", | ||
async () => { | ||
mf.mock("POST@/api/chat.postMessage", () => { | ||
return new Response("big explosions", { status: 500 }); | ||
}); | ||
|
||
await assertRejects( | ||
async () => { | ||
return await client.apiCall("chat.postMessage", {}); | ||
}, | ||
Error, | ||
"500: big explosions", | ||
); | ||
|
||
mf.reset(); | ||
}, | ||
); | ||
|
||
await t.step("should return successful response JSON", async () => { | ||
mf.mock("POST@/api/chat.postMessage", () => { | ||
return new Response('{"ok":true}'); | ||
}); | ||
|
||
const res = await client.apiCall("chat.postMessage", {}); | ||
assertEquals(res.ok, true); | ||
|
||
mf.reset(); | ||
}); | ||
}); | ||
|
||
await t.step("response method", async (t) => { | ||
await t.step( | ||
"should throw if response returns an HTTP status code >= 400", | ||
async () => { | ||
mf.mock("POST@/api/chat.postMessage", () => { | ||
return new Response("big explosions", { status: 500 }); | ||
}); | ||
|
||
await assertRejects( | ||
async () => { | ||
return await client.response( | ||
"https://slack.com/api/chat.postMessage", | ||
{}, | ||
); | ||
}, | ||
Error, | ||
"500: big explosions", | ||
); | ||
|
||
mf.reset(); | ||
}, | ||
); | ||
|
||
await t.step("should return successful response JSON", async () => { | ||
mf.mock("POST@/api/chat.postMessage", () => { | ||
return new Response('{"ok":true}'); | ||
}); | ||
|
||
const res = await client.response( | ||
"https://slack.com/api/chat.postMessage", | ||
{}, | ||
); | ||
assertEquals(res.ok, true); | ||
|
||
mf.reset(); | ||
}); | ||
}); | ||
}); | ||
|
||
assertEquals(typeof client.apiCall, "function"); | ||
assertEquals(typeof client.response, "function"); | ||
await t.step("instantiated with custom API URL", async (t) => { | ||
const client = SlackAPI("test-token", { | ||
slackApiUrl: "https://apitown.com/", | ||
}); | ||
|
||
await t.step("apiCall method", async (t) => { | ||
await t.step("should call the custom API URL", async () => { | ||
mf.mock("POST@/chat.postMessage", (req: Request) => { | ||
assertEquals(req.url, "https://apitown.com/chat.postMessage"); | ||
return new Response('{"ok":true}'); | ||
}); | ||
|
||
await client.apiCall("chat.postMessage", {}); | ||
|
||
mf.reset(); | ||
}); | ||
}); | ||
}); | ||
|
||
mf.uninstall(); | ||
}); | ||
|
||
Deno.test("serializeData helper function", async (t) => { | ||
await t.step( | ||
"should serialize string values as strings and return a URLSearchParams object", | ||
() => { | ||
assertEquals( | ||
serializeData({ "batman": "robin" }).toString(), | ||
"batman=robin", | ||
); | ||
}, | ||
); | ||
await t.step( | ||
"should serialize non-string values as JSON-encoded strings and return a URLSearchParams object", | ||
() => { | ||
assertEquals( | ||
serializeData({ "hockey": { "good": true, "awesome": "yes" } }) | ||
.toString(), | ||
"hockey=%7B%22good%22%3Atrue%2C%22awesome%22%3A%22yes%22%7D", | ||
); | ||
}, | ||
); | ||
}); |
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