gulp-function/test/test.ts

119 lines
3.3 KiB
TypeScript
Raw Normal View History

2016-10-18 23:10:45 +00:00
import 'typings-test'
let gulp = require('gulp');
import gulpFunction from '../dist/index'
let beautylog = require('beautylog')
let Q = require('q')
let myFunction = function () {
let done = Q.defer()
beautylog.log('Function executed');
2016-03-26 16:22:46 +00:00
done.resolve();
return done.promise;
};
2016-10-18 23:10:45 +00:00
let myFunction2 = function () {
let done = Q.defer();
beautylog.ok('Function2 executed');
2016-03-26 16:22:46 +00:00
done.resolve();
return done.promise;
};
2016-10-18 23:10:45 +00:00
let myFunction3 = function () {
let done = Q.defer();
beautylog.success('Function3 executed');
2016-03-26 16:22:46 +00:00
done.resolve();
return done.promise;
};
2016-10-18 23:10:45 +00:00
let beforeFunction = function () {
let done = Q.defer();
beautylog.success('beforeFunction executed');
2016-03-26 16:22:46 +00:00
done.resolve();
return done.promise;
};
2016-10-18 23:10:45 +00:00
let middleFunctionRun = false;
2016-03-26 16:22:46 +00:00
2016-10-18 23:10:45 +00:00
let middleFunction = function () {
let done = Q.defer();
beautylog.success('middleFunction executed');
2016-03-26 16:22:46 +00:00
setTimeout(function(){
2016-10-18 23:10:45 +00:00
beautylog.log('timeout fired');
2016-03-26 16:22:46 +00:00
middleFunctionRun = true;
done.resolve();
}, 500);
return done.promise;
};
2016-10-18 23:10:45 +00:00
let afterFunction = function () {
let done = Q.defer();
beautylog.success('afterFunction executed');
2016-03-26 16:22:46 +00:00
done.resolve();
return done.promise;
};
let timeoutFunction = function(){
2016-10-18 23:10:45 +00:00
let done = Q.defer();
2016-03-26 16:22:46 +00:00
setTimeout(function(){
2016-10-18 23:10:45 +00:00
beautylog.log('largeTimeout fired');
2016-03-26 16:22:46 +00:00
done.resolve();
},2000);
return done.promise;
};
2016-10-18 23:10:45 +00:00
describe('gulpFunction',function(){
it('should run through smoothly with ' + "'forEach'",function(done){
2016-03-26 16:22:46 +00:00
gulp.src('./test/*.md')
.pipe(gulpFunction(myFunction,'forEach'))
2016-10-18 23:10:45 +00:00
.pipe(gulp.dest('./test/result/'));
2016-03-26 16:22:46 +00:00
gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'forEach'))
2016-10-18 23:10:45 +00:00
.pipe(gulp.dest('./test/result/'))
.pipe(gulpFunction(done,'atEnd'));
2016-03-26 16:22:46 +00:00
});
2016-10-18 23:10:45 +00:00
it('should run through smoothly with ' + "'atEnd'",function(done){
2016-03-26 16:22:46 +00:00
gulp.src('./test/*.md')
.pipe(gulpFunction(myFunction,'atEnd'))
2016-10-18 23:10:45 +00:00
.pipe(gulp.dest('./test/result/'));
2016-03-26 16:22:46 +00:00
gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'atEnd'))
2016-10-18 23:10:45 +00:00
.pipe(gulp.dest('./test/result/'))
.pipe(gulpFunction(done,'atEnd'));
2016-03-26 16:22:46 +00:00
});
2016-10-18 23:10:45 +00:00
it('should run through smoothly once with ' + "'atFirst'",function(done){
2016-03-26 16:22:46 +00:00
gulp.src('./test/*.md')
.pipe(gulpFunction([myFunction2,myFunction3],'forFirst'))
2016-10-18 23:10:45 +00:00
.pipe(gulp.dest('./test/result/'))
.pipe(gulpFunction(done,'atEnd'));
2016-03-26 16:22:46 +00:00
});
2016-10-18 23:10:45 +00:00
it('should run in order',function(done){
2016-03-26 16:22:46 +00:00
this.timeout(5000);
let stream = gulp.src('./test/*.md')
.pipe(gulpFunction([beforeFunction,middleFunction,middleFunction],'atEnd'))
.pipe(gulpFunction(function(){
2016-10-18 23:10:45 +00:00
beautylog.log('stream progressed');
let done2 = Q.defer();
2016-03-26 16:22:46 +00:00
done2.resolve();
return done2.promise;
2016-10-18 23:10:45 +00:00
},'forEach'))
2016-03-26 16:22:46 +00:00
.pipe(gulpFunction(function(){
2016-10-18 23:10:45 +00:00
beautylog.log('nextStep');
2016-03-26 16:22:46 +00:00
}))
2016-10-18 23:10:45 +00:00
.pipe(gulpFunction(afterFunction,'atEnd'))
.pipe(gulpFunction(timeoutFunction,'atEnd'));
2016-03-26 16:22:46 +00:00
2016-10-18 23:10:45 +00:00
stream.on('finish',function(){
beautylog.info('stream finished');
2016-03-26 16:22:46 +00:00
done();
});
});
});