From de2a349f10e43589b89aeab61765ed6dcdaa6d39 Mon Sep 17 00:00:00 2001 From: Roz <3948961+roziscoding@users.noreply.github.com> Date: Thu, 29 Aug 2024 07:15:30 -0300 Subject: [PATCH] test: add integration test for default handler (#44) * test: add integration test for default handler * fix(test): remove `only` from test case --- test/integration.test.ts | 64 ++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/test/integration.test.ts b/test/integration.test.ts index c876de0..0a014f6 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -19,7 +19,20 @@ import { User, } from "./deps.test.ts"; -const bot = new Bot("dummy_token"); +const getBot = () => + new Bot("dummy_token", { + botInfo: { + id: 1, + is_bot: true, + username: "", + can_join_groups: true, + can_read_all_group_messages: true, + supports_inline_queries: true, + first_name: "", + can_connect_to_business: true, + has_main_web_app: false, + }, + }); const getDummyUpdate = ({ userInput, language, noChat }: { userInput?: string; @@ -90,19 +103,7 @@ describe("Integration", () => { myCommands.command("command", "_", (_, next) => next()); const setMyCommandsSpy = spy(resolvesNext([true] as const)); - const bot = new Bot("dummy_token", { - botInfo: { - id: 1, - is_bot: true, - username: "", - can_join_groups: true, - can_read_all_group_messages: true, - supports_inline_queries: true, - first_name: "", - can_connect_to_business: true, - has_main_web_app: false, - }, - }); + const bot = getBot(); bot.api.config.use(async (prev, method, payload, signal) => { if (method !== "setMyCommands") { @@ -145,19 +146,7 @@ describe("Integration", () => { }); const setMyCommandsSpy = spy(resolvesNext([true] as const)); - const bot = new Bot("dummy_token", { - botInfo: { - id: 1, - is_bot: true, - username: "", - can_join_groups: true, - can_read_all_group_messages: true, - supports_inline_queries: true, - first_name: "", - can_connect_to_business: true, - has_main_web_app: false, - }, - }); + const bot = getBot(); bot.api.config.use(async (prev, method, payload, signal) => { if (method !== "setMyCommands") { @@ -165,7 +154,7 @@ describe("Integration", () => { } await setMyCommandsSpy(payload); - return { ok: true, result: true as ReturnType }; + return { ok: true as const, result: true as ReturnType }; }); bot.use(commands()); @@ -180,4 +169,23 @@ describe("Integration", () => { assertSpyCalls(setMyCommandsSpy, 0); }); }); + + describe("CommandGroup", () => { + describe("command", () => { + it("should add a command with a default handler", async () => { + const handler = spy(() => {}); + + const commandGroup = new CommandGroup({ prefix: "!" }); + commandGroup.command("command", "_", handler); + + const bot = getBot(); + bot.use(commands()); + bot.use(commandGroup); + + await bot.handleUpdate(getDummyUpdate({ userInput: "!command" })); + + assertSpyCalls(handler, 1); + }); + }); + }); });