Skip to content

Commit

Permalink
adding specs for watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jrsacks committed May 18, 2011
1 parent 1506de7 commit 1b35489
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/jezebel/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ function watchGivenFile (watch, callback) {
}

function watchFiles(path, callback) {
projectFiles = [];
var curriedCallback = function(curr, prev) {
callback(path, curr, prev);
};

fs.stat(path, function(err, stats){
if (err) {
sys.error('Error retrieving stats for file: ' + path);
} else {
}
else {
if (stats.isDirectory()) {
fs.readdir(path, function(err, fileNames) {
if(err) {
Expand All @@ -27,15 +31,12 @@ function watchFiles(path, callback) {
});
}
});
} else {
watchGivenFile(path, function (curr, prev) {
projectFiles.push(path);
callback(path, curr, prev);
});
}
else {
watchGivenFile(path, curriedCallback);
}
}
});
return projectFiles;
}

exports.watchFiles = watchFiles;
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ var path = require('path');
var binDir = path.dirname(fs.realpathSync(__filename));
var lib = path.join(binDir, '../lib');
require.paths.push(lib);

jasmine.Spy.prototype.invokeCallback = function(){
var argsToInvokeWith = arguments;
this.argsForCall.forEach(function(args){
args[args.length -1].apply(this, argsToInvokeWith);
});
this.reset();
};
45 changes: 45 additions & 0 deletions spec/watcher_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
describe('watcher', function() {
var watcher, fs;

beforeEach(function() {
watcher = require('jezebel/watcher');
fs = require('fs');
spyOn(fs, 'watchFile');
spyOn(fs, 'stat');
});

describe('watching a single file', function() {
var callback;
beforeEach(function() {
callback = jasmine.createSpy('callback');
watcher.watchFiles('filename', callback);
fs.stat.invokeCallback('', {isDirectory : function(){return false;}});
});

it('provides the correct filename and options to watchFile', function() {
expect(fs.watchFile.argsForCall[0][0]).toEqual('filename');
expect(fs.watchFile.argsForCall[0][1]).toEqual({persistent : true, interval : 500});
});

it('curries filename to the callback of watchFile', function() {
fs.watchFile.invokeCallback('curr', 'prev');
expect(callback).toHaveBeenCalledWith('filename', 'curr' , 'prev');
});
});

describe('watching a directory', function() {
it('calls watchFile on all files in the directory', function() {
spyOn(fs, 'readdir');
watcher.watchFiles('dirName', {});
fs.stat.invokeCallback('', {isDirectory : function(){return true;}});

fs.readdir.invokeCallback('', ['file0', 'file1']);
fs.stat.invokeCallback('', {isDirectory : function(){return false;}});

expect(fs.watchFile.callCount).toEqual(2);
expect(fs.watchFile.argsForCall[0][0]).toEqual('dirName/file0');
expect(fs.watchFile.argsForCall[1][0]).toEqual('dirName/file1');
});

});
});

0 comments on commit 1b35489

Please sign in to comment.