diff --git a/package.json b/package.json index 665cfb1..e6a90e9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -47,5 +51,10 @@ "pg-hstore": "^2.3.2", "sequelize": "^4.23.2", "telegraf": "^3.16.4" + }, + "ava": { + "files": [ + "src/**/*.test.js" + ] } } diff --git a/src/lib/text.js b/src/lib/text.js index e4f679a..70ce49a 100644 --- a/src/lib/text.js +++ b/src/lib/text.js @@ -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})`, diff --git a/src/lib/text.test.js b/src/lib/text.test.js new file mode 100644 index 0000000..7a3cdfd --- /dev/null +++ b/src/lib/text.test.js @@ -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') +})