-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: run ad-hoc middleware with pseudo update
BREAKING CHANGE: payload is now typed optional
- Loading branch information
1 parent
6ad9c21
commit 77492c1
Showing
3 changed files
with
146 additions
and
32 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
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,65 @@ | ||
import "grammy-pseudo-update" | ||
|
||
import { Chat } from "@grammyjs/types" | ||
import { Bot } from "grammy" | ||
import { PseudoUpdatePayload } from "grammy-pseudo-update" | ||
import tap from "tap" | ||
|
||
tap.ok(true, "it works") | ||
declare module "grammy-pseudo-update" { | ||
interface PseudoUpdatePayload { | ||
test?: true | ||
} | ||
} | ||
|
||
function init() { | ||
const bot = new Bot("invalid_token") | ||
bot.botInfo = {} as any | ||
const log: string[] = [] | ||
bot.pseudo((ctx, next) => { | ||
log.push("mw") | ||
if (!ctx.pseudo?.test) { | ||
return next() | ||
} | ||
}) | ||
return { bot, log } | ||
} | ||
|
||
const chat: Chat = { type: "private", id: 12345, first_name: "John" } | ||
const payload: PseudoUpdatePayload = { test: true } | ||
|
||
tap.test("simple", async (tap) => { | ||
const { bot, log } = init() | ||
await bot.handlePseudoUpdate({ chat }) | ||
tap.same(log, ["mw"]) | ||
}) | ||
|
||
tap.test("simple with payload", async (tap) => { | ||
const { bot, log } = init() | ||
await bot.handlePseudoUpdate({ chat, payload }) | ||
tap.same(log, ["mw"]) | ||
}) | ||
|
||
tap.test("final handler called when no payload", async (tap) => { | ||
const { bot, log } = init() | ||
await bot.handlePseudoUpdate({ chat }, () => { | ||
log.push("final") | ||
}) | ||
tap.same(log, ["mw", "final"]) | ||
}) | ||
|
||
tap.test("final handler ignored when payload provided", async (tap) => { | ||
const { bot, log } = init() | ||
await bot.handlePseudoUpdate({ chat, payload }, () => { | ||
log.push("final") | ||
}) | ||
tap.same(log, ["mw"]) | ||
}) | ||
|
||
tap.test("final handler ignored when payload provided", async (tap) => { | ||
const { bot, log } = init() | ||
await bot.handlePseudoUpdate({ chat }, () => { | ||
log.push("final") | ||
}) | ||
await bot.handlePseudoUpdate({ chat }) | ||
tap.same(log, ["mw", "final", "mw"]) | ||
}) |