Skip to content

Commit

Permalink
test(menuPhoto): test menuPhoto
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Mar 19, 2019
1 parent 0f7920f commit 6e9b974
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
3 changes: 2 additions & 1 deletion test/helpers/telegraf-typing-overrides.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
121 changes: 121 additions & 0 deletions test/menu-photo.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 6e9b974

Please sign in to comment.