From 6e9b97486ae7c2fcd3418f5e5c9b336d97d31e4d Mon Sep 17 00:00:00 2001 From: Edgar Toll Date: Tue, 19 Mar 2019 14:44:01 +0100 Subject: [PATCH] test(menuPhoto): test menuPhoto --- test/helpers/telegraf-typing-overrides.ts | 3 +- test/menu-photo.ts | 121 ++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 test/menu-photo.ts diff --git a/test/helpers/telegraf-typing-overrides.ts b/test/helpers/telegraf-typing-overrides.ts index feb2e71..def42b1 100644 --- a/test/helpers/telegraf-typing-overrides.ts +++ b/test/helpers/telegraf-typing-overrides.ts @@ -1,8 +1,9 @@ import {InlineKeyboardMarkup, ForceReply} from 'telegram-typings' export interface InlineExtra { - reply_markup: InlineKeyboardMarkup; + caption?: string; parse_mode?: 'Markdown'; + reply_markup: InlineKeyboardMarkup; } export interface ForceReplyExtra { diff --git a/test/menu-photo.ts b/test/menu-photo.ts new file mode 100644 index 0000000..bc30fb6 --- /dev/null +++ b/test/menu-photo.ts @@ -0,0 +1,121 @@ +import test, {ExecutionContext} from 'ava' +import Telegraf from 'telegraf' +import {Update} from 'telegram-typings' + +import TelegrafInlineMenu from '../source' +import {emojiTrue} from '../source/prefix' + +import {InlineExtra} from './helpers/telegraf-typing-overrides' + +function createBasicBot(t: ExecutionContext, menu: TelegrafInlineMenu): any { + menu.toggle('toggle me', 'c', { + setFunc: () => Promise.reject(new Error('Nothing has to be set when only showing the menu')), + isSetFunc: () => true + }) + + const bot = new Telegraf('') + bot.use(menu.init({actionCode: 'a'})) + bot.catch((error: any) => { + t.log(error) + t.fail(error.message) + }) + + bot.context.answerCbQuery = async () => true + + return bot +} + +const EXPECTED_KEYBOARD = [[{ + text: emojiTrue + ' toggle me', + callback_data: 'a:c-false' +}]] + +const PHOTO_MEDIA = {source: 'cat.png'} + +test('menu from callback on photo edits photo', async t => { + const bot = createBasicBot(t, new TelegrafInlineMenu('yaay', { + photo: PHOTO_MEDIA + })) + const errorMsg = 'only editMessageMedia should be called' + bot.context.deleteMessage = () => Promise.reject(new Error(errorMsg)) + bot.context.editMessageText = () => Promise.reject(new Error(errorMsg)) + bot.context.reply = () => Promise.reject(new Error(errorMsg)) + bot.context.replyWithPhoto = () => Promise.reject(new Error(errorMsg)) + + bot.context.editMessageMedia = async (media: any, extra: InlineExtra) => { + t.deepEqual(media, { + type: 'photo', + media: PHOTO_MEDIA, + caption: 'yaay' + }) + t.deepEqual(extra.reply_markup.inline_keyboard, EXPECTED_KEYBOARD) + return true + } + + await bot.handleUpdate({callback_query: {data: 'a', message: {photo: [{}, {}]}}} as Update) +}) + +test('menu from command replies photo', async t => { + const bot = createBasicBot(t, new TelegrafInlineMenu('yaay', { + photo: PHOTO_MEDIA + }).setCommand('test')) + const errorMsg = 'only replyWithPhoto should be called' + bot.context.deleteMessage = () => Promise.reject(new Error(errorMsg)) + bot.context.editMessageMedia = () => Promise.reject(new Error(errorMsg)) + bot.context.editMessageText = () => Promise.reject(new Error(errorMsg)) + bot.context.reply = () => Promise.reject(new Error(errorMsg)) + + bot.context.replyWithPhoto = async (photo: any, extra: InlineExtra) => { + t.is(photo, PHOTO_MEDIA) + t.is(extra.caption, 'yaay') + t.deepEqual(extra.reply_markup.inline_keyboard, EXPECTED_KEYBOARD) + return true + } + + await bot.handleUpdate({message: { + text: '/test', + entities: [{type: 'bot_command', offset: 0, length: 5}] + }} as Update) +}) + +test('replace message without photo to add photo to menu', async t => { + t.plan(4) + const bot = createBasicBot(t, new TelegrafInlineMenu('yaay', { + photo: PHOTO_MEDIA + })) + const errorMsg = 'replyWithPhoto should be called' + bot.context.editMessageMedia = () => Promise.reject(new Error(errorMsg)) + bot.context.editMessageText = () => Promise.reject(new Error(errorMsg)) + bot.context.reply = () => Promise.reject(new Error(errorMsg)) + + bot.context.deleteMessage = async () => t.pass() + bot.context.replyWithPhoto = async (photo: any, extra: InlineExtra) => { + t.deepEqual(photo, PHOTO_MEDIA) + t.is(extra.caption, 'yaay') + t.deepEqual(extra.reply_markup.inline_keyboard, EXPECTED_KEYBOARD) + return true + } + + await bot.handleUpdate({callback_query: {data: 'a'}} as Update) +}) + +test('replace message with photo to remove photo from menu', async t => { + t.plan(3) + const bot = createBasicBot(t, new TelegrafInlineMenu('yaay', { + photo: undefined + })) + const errorMsg = 'reply should be called' + bot.context.editMessageMedia = () => Promise.reject(new Error(errorMsg)) + bot.context.editMessageText = () => Promise.reject(new Error(errorMsg)) + bot.context.reply = () => Promise.reject(new Error(errorMsg)) + bot.context.replyWithPhoto = () => Promise.reject(new Error(errorMsg)) + + bot.context.deleteMessage = async () => t.pass() + bot.context.reply = async (text: any, extra: InlineExtra) => { + t.deepEqual(text, 'yaay') + t.deepEqual(extra.reply_markup.inline_keyboard, EXPECTED_KEYBOARD) + return true + } + + await bot.handleUpdate({callback_query: {data: 'a', message: {photo: []} as any}} as Update) +})