From a9026ac8a5bf6d7566bffae6e2090116da271818 Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Mon, 17 Nov 2014 22:14:28 +0200 Subject: [PATCH] add basic environment for unit testing with mocha --- package.json | 6 ++++-- test/plugins/link.js | 37 +++++++++++++++++++++++++++++++++++++ test/util.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test/plugins/link.js create mode 100644 test/util.js diff --git a/package.json b/package.json index 7920b24..3a2a8e9 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "url": "https://github.com/erming/shout.git" }, "scripts": { - "start": "node index" + "start": "node index", + "test": "NODE_ENV=test mocha test/**/*.js" }, "keywords": [ "browser", @@ -40,6 +41,7 @@ "grunt": "~0.4.5", "grunt-contrib-uglify": "~0.5.0", "grunt-contrib-watch": "^0.6.1", - "handlebars": "^2.0.0" + "handlebars": "^2.0.0", + "mocha": "~2.0.1" } } diff --git a/test/plugins/link.js b/test/plugins/link.js new file mode 100644 index 0000000..6cef635 --- /dev/null +++ b/test/plugins/link.js @@ -0,0 +1,37 @@ +var assert = require('assert'); + +var util = require('../util'); +var link = require('../../src/plugins/irc-events/link.js'); + +describe('Link plugin', function() { + before(function(done) { + this.app = util.createWebserver(); + this.connection = this.app.listen(9002, done); + }); + + after(function(done) { + this.connection.close(done); + }); + + beforeEach(function() { + this.irc = util.createClient(); + this.network = util.createNetwork(); + }); + + it('should be able to fetch basic information about URLs', function(done) { + var plugin = link.call(this.irc, this.irc, this.network); + + this.app.get('/basic', function(req, res) { + res.send('test'); + }); + + this.irc.createMessage({ + message: 'http://localhost:9002/basic' + }); + + this.irc.once('toggle', function(data) { + assert.equal(data.head, 'test'); + done(); + }) + }); +}); diff --git a/test/util.js b/test/util.js new file mode 100644 index 0000000..b7adeee --- /dev/null +++ b/test/util.js @@ -0,0 +1,43 @@ +var EventEmitter = require('events').EventEmitter; +var util = require('util'); +var _ = require('lodash'); +var express = require('express'); + +function MockClient(opts) { + this.me = 'test-user'; + + for(k in opts) { + this[k] = opts[k]; + } +} +util.inherits(MockClient, EventEmitter); + +MockClient.prototype.createMessage = function(opts) { + + var message = _.extend({ + message: 'dummy message', + from: 'test-user', + to: 'test-channel' + }, opts); + + this.emit('message', message); +} + +module.exports = { + createClient: function() { + return new MockClient(); + }, + createNetwork: function() { + return { + channels: [{ + name: 'test-channel', + messages: [] + }] + } + }, + createWebserver: function() { + return express(); + } +} + +