-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from telegram-ru/testing
Add tests
- Loading branch information
Showing
7 changed files
with
267 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import test from 'ava' | ||
import { Extra } from 'telegraf' | ||
|
||
import { Context } from '../../tests/telegraf' | ||
import installFeature from './index' | ||
|
||
/* eslint-disable no-magic-numbers */ | ||
|
||
test('!id command reply with chat id', async (t) => { | ||
t.plan(3) | ||
|
||
let testFn = null | ||
const bot = { | ||
hears(re, fn) { | ||
t.is(re.toString(), /^!id$/.toString()) | ||
t.true(typeof fn === 'function') | ||
testFn = fn | ||
}, | ||
} | ||
const context = Context.create() | ||
|
||
installFeature(bot) | ||
testFn(context) | ||
t.true(context.reply.calledWith(context.chat.id, Extra.inReplyTo(context.message.message_id))) | ||
}) |
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 |
---|---|---|
@@ -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) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import test from 'ava' | ||
import sinon from 'sinon' | ||
import text from './text' | ||
|
||
/* eslint-disable no-magic-numbers */ | ||
|
||
|
||
test('random(variants) should return from variant', (t) => { | ||
const variants = [() => 1, () => 2] | ||
const result = text.random(variants)() | ||
|
||
t.true(result === 1 || result === 2) | ||
}) | ||
|
||
test('random(variants)() should pass arguments', (t) => { | ||
const variants = [sinon.spy(), sinon.spy()] | ||
|
||
text.random(variants)(1) | ||
t.true(variants[0].calledWith(1) || variants[1].calledWith(1)) | ||
}) | ||
|
||
|
||
test('fullName(user) should get correct result', (t) => { | ||
t.is(text.fullName({ first_name: 'Foo' }), 'Foo') | ||
t.is(text.fullName({ last_name: 'Bar' }), 'Bar') | ||
t.is(text.fullName({ username: 'foobar' }), '(@foobar)') | ||
|
||
t.is(text.fullName({ first_name: 'Foo', last_name: 'Bar' }), 'Foo Bar') | ||
t.is(text.fullName({ first_name: 'Foo', username: 'foobar' }), 'Foo (@foobar)') | ||
t.is(text.fullName({ last_name: 'Bar', username: 'foobar' }), 'Bar (@foobar)') | ||
t.is(text.fullName({ first_name: 'Foo', last_name: 'Bar', username: 'foobar' }), 'Foo Bar (@foobar)') | ||
}) | ||
|
||
test('chatTitle(chat) should return correct name', (t) => { | ||
t.is(text.chatTitle({ type: 'private', id: 1 }), 'private:1') | ||
t.is(text.chatTitle({ type: 'private', id: 1, title: 'Chat' }), 'Chat') | ||
t.is(text.chatTitle({ type: 'private', id: 1, username: 'chat' }), '(@chat)') | ||
t.is(text.chatTitle({ type: 'private', id: 1, title: 'Chat', username: 'chat' }), 'Chat (@chat)') | ||
}) | ||
|
||
test('select(value, cases, defaultCase) should select from map', (t) => { | ||
const cases = { foo: 1, bar: 2, baz: 3, 555: 4 } | ||
|
||
t.is(text.select('foo', cases, 5), 1) | ||
t.is(text.select('bar', cases, 5), 2) | ||
t.is(text.select('baz', cases, 5), 3) | ||
t.is(text.select(555, cases, 5), 4) | ||
t.is(text.select('NOT', cases, 5), 5) | ||
}) | ||
|
||
test('column(1,2) should join with \n', (t) => { | ||
t.is(text.column('a', 'b', 'c'), 'a\nb\nc') | ||
t.is(text.column('a', 'b'), 'a\nb') | ||
t.is(text.column('a'), 'a') | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"import/no-extraneous-dependencies": "off" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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 | ||
} | ||
} | ||
|
||
class Message { | ||
constructor() { | ||
this.message_id = ++ID_MESSAGE | ||
this.date = Date.now() | ||
this.chat = new Chat() | ||
} | ||
|
||
$from() { | ||
this.from = new User() | ||
} | ||
} | ||
|
||
class Context { | ||
static create() { | ||
return new this() | ||
} | ||
|
||
constructor() { | ||
this.message = new Message() | ||
|
||
this.reply = sinon.stub().resolves() | ||
} | ||
|
||
get from() { | ||
return this.message.from | ||
} | ||
|
||
get chat() { | ||
return this.message.chat | ||
} | ||
} | ||
|
||
module.exports = { | ||
Context, | ||
Message, | ||
Chat, | ||
} |