Created
July 28, 2016 15:10
-
-
Save kbailles/fa5530094547292ffae0937c6e54e048 to your computer and use it in GitHub Desktop.
Sample node module with tests
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
const chokidar = require('chokidar'); | |
const fs = require('fs'); | |
const _ = require('lodash'); | |
const uuid = require('node-uuid'); | |
const messageTypes = require('./message.types.js'); | |
const basePath = '/ProgramData/XDMessagingv4/'; | |
const screensaverPath = '/FitnessOnDemand/Resources/Images/Screensaver' | |
var heartbeatTimer; | |
function watchChannel(channel, options) { | |
var defaults = { | |
ignoreInitial: true | |
} | |
var mergedOptions = _.defaults(defaults, options); | |
return chokidar.watch(basePath + channel, mergedOptions); | |
} | |
function readFile(src, callback) { | |
fs.readFile(src, 'utf8', (error, data) => { | |
if (callback) { | |
callback(error, data); | |
} | |
}); | |
} | |
function writeFile(dest, data, callback) { | |
fs.writeFile(dest, JSON.stringify(data), function(error) { | |
if (callback) { | |
callback(error); | |
} | |
}); | |
} | |
function startHeartbeat(channel) { | |
if (heartbeatTimer) { | |
return; | |
} | |
heartbeatTimer = setInterval(() => { | |
this.sendHeartbeat(channel); | |
}, 1000); | |
} | |
function sendHeartbeat(channel) { | |
var dest = basePath + channel + '/' + uuid.v4() + '.msg'; | |
this.writeFile(dest, { | |
channel: channel, | |
message: { | |
type: messageTypes.heartbeat | |
} | |
}); | |
} | |
function cancelHeartbeat() { | |
if (heartbeatTimer) { | |
clearInterval(heartbeatTimer); | |
heartbeatTimer = null; | |
} | |
} | |
function getScreensaverImages(callback) { | |
fs.readdir(screensaverPath, 'utf8', function(error, files) { | |
var fixedImagePaths = []; | |
if (files && files.length > 0) { | |
fixedImagePaths = files.map(function(path) { | |
return screensaverPath + '/' + path; | |
}); | |
} | |
if (callback) { | |
callback(error, fixedImagePaths); | |
} | |
}); | |
} | |
function getHeartbeatTimer() { | |
return heartbeatTimer; | |
} | |
function getScreenSaverPath() { | |
return screensaverPath; | |
} | |
module.exports = { | |
watchChannel: watchChannel, | |
readFile: readFile, | |
writeFile: writeFile, | |
startHeartbeat: startHeartbeat, | |
cancelHeartbeat: cancelHeartbeat, | |
sendHeartbeat: sendHeartbeat, | |
getScreensaverImages: getScreensaverImages, | |
getHeartbeatTimer: getHeartbeatTimer, | |
getScreenSaverPath: getScreenSaverPath | |
}; |
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
const expect = require('chai').expect; | |
const sinon = require('sinon'); | |
const chokidar = require('chokidar'); | |
const fs = require('fs'); | |
const ipcClient = require('../../../src/ipc/ipc.client.js'); | |
describe('ipc client', function() { | |
var sandbox; | |
beforeEach(function() { | |
sandbox = sinon.sandbox.create(); | |
}); | |
afterEach(function() { | |
sandbox.restore(); | |
}); | |
describe('method: registerChannel', function() { | |
it('should use chokidar to watch a directory', function() { | |
var spy = sandbox.spy(chokidar, 'watch'); | |
ipcClient.watchChannel('testChannel'); | |
expect(spy.calledOnce).to.be.true; | |
}); | |
it('should be watching the correct a folder inside XDMessaging directory', function() { | |
var channel = 'testChannel'; | |
var spy = sandbox.spy(chokidar, 'watch'); | |
ipcClient.watchChannel(channel); | |
expect(spy.calledWith('/ProgramData/XDMessagingv4/' + channel)).to.be.true; | |
}); | |
}); | |
describe('method: readFile', function() { | |
it('should use nodes filestream module to read the file', function() { | |
var spy = sandbox.spy(fs, 'readFile'); | |
ipcClient.readFile('/someSource', () => {}); | |
expect(spy.calledOnce).to.be.true; | |
}); | |
it('should call the passed callback function', function(done) { | |
var callback = function() { | |
done(); | |
} | |
ipcClient.readFile('/someSource', callback); | |
}); | |
}); | |
describe('method: writeFile', function() { | |
it('should use nodes filestream module to write the file', function() { | |
var spy = sandbox.spy(fs, 'writeFile'); | |
ipcClient.writeFile('/someDest', {}, () => {}); | |
expect(spy.calledOnce).to.be.true; | |
}); | |
it('should call the passed callback function', function(done) { | |
var callback = function() { | |
done(); | |
} | |
ipcClient.writeFile('/someDest', {}, callback); | |
}); | |
}); | |
describe('method: startHeartbeat', function() { | |
beforeEach(function() { | |
this.clock = sinon.useFakeTimers(); | |
}); | |
afterEach(function() { | |
this.clock.restore(); | |
}); | |
it('should send a heartbeat after 1 second', function() { | |
var spy = sandbox.spy(ipcClient, 'sendHeartbeat'); | |
ipcClient.startHeartbeat('testChannel', spy); | |
this.clock.tick(999); | |
expect(spy.called).to.be.false; | |
this.clock.tick(1000); | |
expect(spy.called).to.be.true; | |
}); | |
}); | |
describe('method: sendHeartbeat', function() { | |
it('should call writeFile to send the heartbeat', function() { | |
var spy = sandbox.spy(fs, 'writeFile'); | |
ipcClient.sendHeartbeat('testChannel'); | |
expect(spy.calledOnce).to.be.true; | |
}); | |
}); | |
describe("method: cancelHeartbeat", function() { | |
it("should cancel a heartbeat that has been started", function() { | |
ipcClient.startHeartbeat('testChannel'); | |
expect(ipcClient.getHeartbeatTimer()).to.not.be.null; | |
ipcClient.cancelHeartbeat(); | |
expect(ipcClient.getHeartbeatTimer()).to.be.null; | |
}); | |
}); | |
describe("method: getScreensaverImages", function() { | |
it("should should call readdir", function() { | |
var spy = sandbox.spy(fs, 'readdir'); | |
ipcClient.getScreensaverImages(); | |
expect(spy.calledOnce).to.be.true; | |
}); | |
it("should should return full path file", function() { | |
var sampleFileName = 'sample_file.mp4'; | |
sandbox.stub(fs, 'readdir', function(path, type, callback) { | |
callback('', [sampleFileName]); | |
}); | |
ipcClient.getScreensaverImages(function(error, images) { | |
expect(images[0]).to.eq(ipcClient.getScreenSaverPath() + '/' + sampleFileName); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment