Skip to content

Commit

Permalink
test: add integration test for default handler (#44)
Browse files Browse the repository at this point in the history
* test: add integration test for default handler

* fix(test): remove `only` from test case
  • Loading branch information
roziscoding authored Aug 29, 2024
1 parent 9753893 commit de2a349
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ import {
User,
} from "./deps.test.ts";

const bot = new Bot("dummy_token");
const getBot = () =>
new Bot<Context & CommandsFlavor>("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;
Expand Down Expand Up @@ -90,19 +103,7 @@ describe("Integration", () => {
myCommands.command("command", "_", (_, next) => next());

const setMyCommandsSpy = spy(resolvesNext([true] as const));
const bot = new Bot<Context & CommandsFlavor>("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") {
Expand Down Expand Up @@ -145,27 +146,15 @@ describe("Integration", () => {
});

const setMyCommandsSpy = spy(resolvesNext([true] as const));
const bot = new Bot<Context & CommandsFlavor>("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") {
return prev(method, payload, signal);
}
await setMyCommandsSpy(payload);

return { ok: true, result: true as ReturnType<typeof prev> };
return { ok: true as const, result: true as ReturnType<typeof prev> };
});

bot.use(commands());
Expand All @@ -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);
});
});
});
});

0 comments on commit de2a349

Please sign in to comment.