Skip to content

Commit

Permalink
test(mw/allow-chat): Add test from mw (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelomanov committed Dec 3, 2017
1 parent ffb40b0 commit dc2ca2c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/middlewares/allowed-chat.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava'
import { Context, next } from '../tests/telegraf'
import { allowWhiteListChat } from './allowed-chat'

/* eslint-disable no-param-reassign */

test.beforeEach((t) => {
t.context.ctx = Context.create()
})

test('test public chat not in the white list', async (t) => {
t.context.ctx.chat.$type('public')
t.context.ctx.chat.$inWhiteList(false)

t.is(await allowWhiteListChat(t.context.ctx, next), null)
})

test('test public chat in the white list', async (t) => {
t.context.ctx.chat.$type('public')
t.context.ctx.chat.$inWhiteList(true)

try {
await allowWhiteListChat(t.context.ctx, next(() => t.pass()))
}
catch (error) {
t.fail()
}
})

test('test private chat', async (t) => {
t.context.ctx.chat.$type('private')

try {
await allowWhiteListChat(t.context.ctx, next(() => t.pass()))
}
catch (error) {
t.fail()
}
})
16 changes: 16 additions & 0 deletions src/tests/telegraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let ID_USER = ID_USER_START
let ID_MESSAGE = ID_MESSAGE_START
let ID_CHAT = ID_CHAT_START

/* eslint-disable class-methods-use-this */

class User {
constructor() {
this.id = ++ID_USER
Expand All @@ -24,6 +26,7 @@ class Chat {
constructor() {
this.id = ++ID_CHAT
this.type = 'private'
this.inWhiteList = false
}

/**
Expand All @@ -35,6 +38,10 @@ class Chat {
return this
}

$inWhiteList(param) {
this.inWhiteList = param
}

/**
*
* @param {string} title
Expand Down Expand Up @@ -75,10 +82,19 @@ class Context {
get chat() {
return this.message.chat
}

isChatInWhiteList(chat) {
return chat.inWhiteList
}
}

function next(cb) {
return cb
}

module.exports = {
Context,
Message,
Chat,
next,
}

0 comments on commit dc2ca2c

Please sign in to comment.