Skip to content

Commit

Permalink
test(lib/text): Add simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Nov 29, 2017
1 parent 6008c3a commit bd437dc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Telegram bot to manage spam and rules for @_ru community",
"main": "src/index.js",
"scripts": {
"test": "npm run test:lint",
"test": "npm run test:lint && npm run test:code",
"test:code": "ava",
"test:lint": "eslint .",
"dev": "cross-env DEBUG=rubot:* nodemon -e yaml,js,json -w locales -w src ./src",
"start": "pm2 startOrRestart ./process.config.js",
Expand All @@ -31,13 +32,16 @@
},
"homepage": "https://github.com/LestaD/ru_bot#readme",
"devDependencies": {
"ava": "^0.24.0",
"cross-env": "^5.1.1",
"eslint": "^4.12.0",
"eslint-config-atomix-base": "^5.0.0",
"husky": "^0.14.3",
"nodemon": "^1.12.1",
"nyc": "^11.3.0",
"pm2": "^2.8.0",
"sequelize-cli": "^3.1.0"
"sequelize-cli": "^3.1.0",
"sinon": "^4.1.2"
},
"dependencies": {
"botanio": "0.0.6",
Expand All @@ -47,5 +51,10 @@
"pg-hstore": "^2.3.2",
"sequelize": "^4.23.2",
"telegraf": "^3.16.4"
},
"ava": {
"files": [
"src/**/*.test.js"
]
}
}
4 changes: 1 addition & 3 deletions src/lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ function fullName({ first_name: first, last_name: last, username }) {
* @param {Chat} param0
* @return {string}
*/
function chatTitle({
title, username, id, type,
}) {
function chatTitle({ title, username, id, type }) {
const parts = [
title,
username && `(@${username})`,
Expand Down
55 changes: 55 additions & 0 deletions src/lib/text.test.js
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')
})

0 comments on commit bd437dc

Please sign in to comment.