From 2468c46d14e4ee2cf505d21bc4b1de0f19ae2538 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 16 Jan 2017 16:16:14 -0800 Subject: [PATCH] feat: what's your pirate name? (#2) --- commands/name.js | 26 ++++++++++++++++++++++++++ package.json | 2 +- server.js | 1 - test/commands/name.js | 17 +++++++++++++++++ test.js => test/server.js | 4 ++-- 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 commands/name.js create mode 100644 test/commands/name.js rename test.js => test/server.js (97%) diff --git a/commands/name.js b/commands/name.js new file mode 100644 index 0000000..29c04a5 --- /dev/null +++ b/commands/name.js @@ -0,0 +1,26 @@ +exports.suffixes = [ + 'the Red', + 'the Sour', + 'the Strong', + 'the Grumpy', + 'the Stout' +] + +exports.prefixes = [ + "Lil'", + 'Peg-legged', + 'Two-Legged', + 'One-Eyed', + 'Two-Eyed', + 'Four-Eyed', + 'Smelly' +] + +exports.command = 'name' +exports.describe = "what's yer pirate name?" +exports.builder = {} +exports.handler = function (argv) { + const prefix = exports.prefixes[parseInt(Math.random() * exports.prefixes.length)] + const suffix = exports.suffixes[parseInt(Math.random() * exports.suffixes.length)] + argv.respond(`${argv.user_name} your pirate name is, "${prefix} ${argv.user_name} ${suffix}"`) +} diff --git a/package.json b/package.json index 5126327..03424fe 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "server.js", "scripts": { "pretest": "standard", - "test": "nyc mocha test.js", + "test": "nyc mocha test/ test/commands/", "start": "./bin/pirate-joe.js", "coverage": "nyc report --reporter=text-lcov | coveralls" }, diff --git a/server.js b/server.js index 9b52c9c..49da5b7 100644 --- a/server.js +++ b/server.js @@ -31,7 +31,6 @@ app.post('/', function (req, res) { // provides a respond function that any yargs // command can use to respond to Slack. context.respond = buildResponder(req.body.response_url) - // run the yargs parser on the inbound slack command. parser.parse(req.body.text || '', context, (err, argv, output) => { if (err) logger.error(err.message) diff --git a/test/commands/name.js b/test/commands/name.js new file mode 100644 index 0000000..02b25e8 --- /dev/null +++ b/test/commands/name.js @@ -0,0 +1,17 @@ +/* global describe, it */ + +const name = require('../../commands/name') + +require('chai').should() + +describe('Name Command', () => { + it('generates a random pirate name', (done) => { + name.handler({ + user_name: 'bcoe', + respond: (msg) => { + msg.should.match(/bcoe your pirate name is/) + return done() + } + }) + }) +}) diff --git a/test.js b/test/server.js similarity index 97% rename from test.js rename to test/server.js index 18d4f43..bac5e26 100644 --- a/test.js +++ b/test/server.js @@ -4,11 +4,11 @@ // to the originating slack channel. const nock = require('nock') const request = require('request') -const startServer = require('./server') +const startServer = require('../server') require('chai').should() -describe('Pirate Joe', () => { +describe('Pirate Joe Server', () => { let server = null // all tests run against an actual running // instance of the server.