Skip to content

Commit

Permalink
test(private-greetings): Add tests for /start in private
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Nov 30, 2017
1 parent 2196cfb commit d8180d9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cross-env": "^5.1.1",
"eslint": "^4.12.0",
"eslint-config-atomix-base": "^5.0.0",
"faker": "^4.1.0",
"husky": "^0.14.3",
"nodemon": "^1.12.1",
"nyc": "^11.3.0",
Expand Down
3 changes: 1 addition & 2 deletions src/features/get-id/get-id.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ test('!id command reply with chat id', async (t) => {
const bot = {
hears(re, fn) {
t.is(re.toString(), /^!id$/.toString())
t.true(typeof fn === 'function')
testFn = fn
},
}
const context = Context.create()

installFeature(bot)
t.true(typeof testFn === 'function')

testFn(context)
t.true(context.reply.calledWith(context.chat.id, Extra.inReplyTo(context.message.message_id)))
})
85 changes: 85 additions & 0 deletions src/features/private-greetings/private-greetings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import test from 'ava'

import { Context } from '../../tests/telegraf'
import installFeature from './index'

/* eslint-disable no-magic-numbers */

test('/start reply with greet in private', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()

installFeature(bot)

testFn(context)
t.regex(context.reply.getCall(0).args[0], new RegExp(`${context.message.from.first_name}`))
})

test('/start not reply in supergroup', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()
context.chat.$type('supergroup')

installFeature(bot)

testFn(context)
t.true(context.reply.notCalled)
})

test('/start not reply in group', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()
context.chat.$type('group')

installFeature(bot)

testFn(context)
t.true(context.reply.notCalled)
})

test('/start not reply in channel', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()
context.chat.$type('channel')

installFeature(bot)

testFn(context)
t.true(context.reply.notCalled)
})
34 changes: 33 additions & 1 deletion src/tests/telegraf.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
const sinon = require('sinon')
const faker = require('faker')


const ID_USER_START = 10000
const ID_MESSAGE_START = 1000000
const ID_CHAT_START = 1000

let ID_USER = ID_USER_START
let ID_MESSAGE = ID_MESSAGE_START
let ID_CHAT = ID_CHAT_START

class User {
constructor() {
this.id = ++ID_USER
this.is_bot = false
this.first_name = faker.name.firstName()
this.last_name = faker.name.lastName()
this.username = faker.internet.userName(this.first_name, this.last_name)
}
}

class Chat {
constructor() {
this.id = ++ID_CHAT
this.type = 'private'
}

/**
*
* @param {'private'|'group'|'supergroup'|'channel'} type
*/
$type(type) {
this.type = type
return this
}

/**
*
* @param {string} title
*/
$title(title) {
this.title = title
return this
Expand All @@ -30,6 +51,10 @@ class Message {
this.date = Date.now()
this.chat = new Chat()
}

$from() {
this.from = new User()
}
}

class Context {
Expand All @@ -39,10 +64,17 @@ class Context {

constructor() {
this.message = new Message()
this.chat = this.message.chat

this.reply = sinon.stub().resolves()
}

get from() {
return this.message.from
}

get chat() {
return this.message.chat
}
}

module.exports = {
Expand Down

0 comments on commit d8180d9

Please sign in to comment.