Skip to content

Commit

Permalink
Tests for both new and old watch syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
robrich committed Jan 19, 2014
1 parent 4492c6e commit 742208a
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ coverage
*.orig
.idea
sandbox
test/out-fixtures/*
test/out-fixtures/*
test/watch-*.txt
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gulp.prototype.watch = function (glob, opt, fn) {
if (Array.isArray(fn) || typeof fn === 'string') {
task = fn;
fn = function () {
this.start(task);
inst.start(task);
};
}
return vfs.watch(glob, opt, fn);
Expand Down
128 changes: 128 additions & 0 deletions test/watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
'use strict';

var gulp = require('../');
var fs = require('fs');
var path = require('path');

var should = require('should');
require('mocha');

describe('gulp', function() {
describe('watch()', function() {
var tempFileContent = 'A test generated this file and it is safe to delete';

var writeTimeout = 125; // Wait for it to get to the filesystem
var writeFile = function (name, content, cb) {
fs.writeFile(name, content, cb);
};
var writeFileWait = function (name, content, cb) {
setTimeout(function () {
fs.writeFile(name, content, cb);
}, writeTimeout);
};

it('should call the function when file changes: no options', function(done) {

// arrange
var tempFile = './test/watch-func.txt';
writeFile(tempFile, tempFileContent, function () {

// assert: it works if it calls done
gulp.watch(tempFile, function (evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('changed');
evt.path.should.equal(path.resolve(tempFile));
done();
});

// act: change file
writeFileWait(tempFile, tempFileContent+' changed');
});
});

it('should call the function when file changes: w/ options', function(done) {
// arrange
var tempFile = './test/watch-func-options.txt';
writeFile(tempFile, tempFileContent, function () {

// assert: it works if it calls done
gulp.watch(tempFile, {debounceDelay:5}, function (evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('changed');
evt.path.should.equal(path.resolve(tempFile));
done();
});

// act: change file
writeFileWait(tempFile, tempFileContent+' changed');
});
});

it('should run a task by name: no options', function(done) {
// arrange
var tempFile = './test/watch-task.txt';
var taskName = 'run-task';
var a = 0;
var timeout = writeTimeout * 2.5;

writeFile(tempFile, tempFileContent, function () {

gulp.task(taskName, function () {
a++;
});

// assert
setTimeout(function () {
a.should.equal(1);

gulp.reset();
done();
}, timeout);

// it works if it calls the task
gulp.watch(tempFile, taskName);

// act: change file
writeFileWait(tempFile, tempFileContent+' changed');
});
});

it('should run many tasks: w/ options', function(done) {
// arrange
var tempFile = './test/watch-task-options.txt';
var task1 = 'task1';
var task2 = 'task2';
var a = 0;
var timeout = writeTimeout * 2.5;

writeFile(tempFile, tempFileContent, function () {

gulp.task(task1, function () {
a++;
});
gulp.task(task2, function () {
a += 10;
});

// assert
setTimeout(function () {
a.should.equal(11); // task1 and task2

gulp.reset();
done();
}, timeout);

// it works if it calls the task
gulp.watch(tempFile, {debounceDelay:timeout/2}, [task1,task2]);

// act: change file
writeFileWait(tempFile, tempFileContent+' changed');
});
});

});
});

0 comments on commit 742208a

Please sign in to comment.