Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
clean unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPilot committed Mar 27, 2015
1 parent d033646 commit 319d0da
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 138 deletions.
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -25,12 +25,12 @@
"author": "Dane Stuckel",
"license": "ISC",
"dependencies": {
"bluebird": "^2.9.3",
"gulp-util": "^3.0.2",
"handlebars": "^2.0.0",
"lodash": "^3.0.0",
"readable-stream": "^1.1.13",
"through2": "^0.6.3"
"bluebird": "^2.9",
"gulp-util": "^3.0",
"handlebars": ">=2",
"lodash": ">=2",
"readable-stream": "^1.1",
"through2": "^0.6"
},
"devDependencies": {
"blanket": "^1.1.6",
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -13,7 +13,7 @@ Reads data, partials and helpers from asynchronous sources like a databases, fil

[![NPM version](https://badge.fury.io/js/gulp-static-handlebars.svg)](http://badge.fury.io/js/gulp-static-handlebars)

##Example with any A+ compatible promises library:
## Example with any A+ compatible promises library:

```JavaScript

Expand All @@ -35,7 +35,7 @@ gulp.src('./app/index.hbs')

```

##Another example with vinyl pipes
## Another example with vinyl pipes

```JavaScript

Expand All @@ -48,20 +48,20 @@ gulp.src('./app/index.hbs')

```

##Install
## Install

```Sh

npm install gulp-static-handlebars

```

##Running Tests
## Running Tests

To run the basic tests, just run `mocha` normally.

This assumes you've already installed the local npm packages with `npm install`.

##To Do:
## To Do:

* Support more handlebars options
221 changes: 94 additions & 127 deletions test/test.js
Expand Up @@ -8,6 +8,9 @@ var bluebird = require('bluebird'),
stream = require('stream'),
gUtil = require('gulp-util');

/**
* Defer resolution for ES6 promises
*/
function deferES6Promise() {
var defer = {};
defer.promise = new es6Promise(function (resolve, reject) {
Expand All @@ -17,18 +20,35 @@ function deferES6Promise() {
return defer;
}

/**
* Clear away all the helpers and partials that we may create in our tests.
*/
function clearHandlebars() {
handlebars.Handlebars.unregisterPartial('test');
handlebars.Handlebars.unregisterHelper('helper-function-export');
handlebars.Handlebars.unregisterHelper('test');
}

/**
* Create a new null file
*/
function createNullFile() {
return new File({
cwd: "/",
base: "/test/",
path: "/test/whatever",
contents: null
});
}

describe('Gulp Static Handlebars', function () {
afterEach(function () {
handlebars.Handlebars.unregisterPartial('test');
handlebars.Handlebars.unregisterHelper('helper-function-export');
handlebars.Handlebars.unregisterHelper('test');
clearHandlebars();
});

describe('Helpers', function () {
afterEach(function () {
handlebars.Handlebars.unregisterPartial('test');
handlebars.Handlebars.unregisterHelper('helper-function-export');
handlebars.Handlebars.unregisterHelper('test');
clearHandlebars();
});

function promisesInObject(deferred, done) {
Expand Down Expand Up @@ -87,7 +107,7 @@ describe('Gulp Static Handlebars', function () {
promiseReturningObject(deferred, done);
});

it('es6 promise returning object should load', function (done) {
it('bluebird returning object should load', function (done) {
var deferred = bluebird.defer();

promiseReturningObject(deferred, done);
Expand All @@ -109,29 +129,21 @@ describe('Gulp Static Handlebars', function () {
});
});

it('should not fail on no helper files and no helper references', function (done) {
//arrange
var expectedContents = '<div>contents!</div>\n<div></div>\n<div></div>';
it('should ignore null files as helpers', function (done) {
var nullFile = createNullFile();

//act
gulp.src('./test/fixtures/test-data-with-helper.html')
.pipe(handlebars({contents: "contents!"}, {helpers: gulp.src('./test/fixtures/something/**/*')}))
.on('error', function (err) {
done(err);
})
.on('data', function (data) {
expect(data.contents.toString()).to.equal(expectedContents);
}).on('end', function () {
gulp.src('./test/fixtures/test-data.html')
.pipe(handlebars({contents: "contents!!"}, {helpers: nullFile}))
.on('data', function () {})
.on('end', function () {
done();
});
});
});

describe('Partials', function () {
afterEach(function () {
handlebars.Handlebars.unregisterPartial('test');
handlebars.Handlebars.unregisterHelper('helper-function-export');
handlebars.Handlebars.unregisterHelper('test');
clearHandlebars();
});

it('should load more than highWaterMark:17 files', function (done) {
Expand Down Expand Up @@ -194,6 +206,14 @@ describe('Gulp Static Handlebars', function () {
partials.push(null);
});

it('should not load partials from arrays (must have a name for each partial)', function (done) {
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: ['things']}))
.on('error', function () {
done();
});
});

function promisesInObject(deferred, done) {
//arrange
var partial = '<div>Partial</div>',
Expand Down Expand Up @@ -256,6 +276,34 @@ describe('Gulp Static Handlebars', function () {
promiseReturningObject(deferred, done);
});

it('plain mapped strings should load', function (done) {
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: {
'test': 'things'
}}))
.on('data', function (result) {
expect(result).to.be.instanceOf(File);
})
.on('end', function () {
done();
});
});

it('inline functions should load ', function (done) {
//arrange
var expectedContents = '<div>contents!!</div>\n<div>immediate data</div>';

//act
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: {
'test': function () { return 'immediate data'; }
}}))
.on('data', function (data) {
expect(data.contents.toString()).to.equal(expectedContents);
done();
});
});

it('pipe should load', function (done) {
//arrange
var expectedContents = '<div>contents!!</div>\n<div><test>partial 1 contents!!</test></div>';
Expand Down Expand Up @@ -306,13 +354,22 @@ describe('Gulp Static Handlebars', function () {
done(data);
});
});

it('should ignore null files as partials', function (done) {
var nullFile = createNullFile();

gulp.src('./test/fixtures/test-data.html')
.pipe(handlebars({contents: "contents!!"}, {partials: nullFile}))
.on('data', function () {})
.on('end', function () {
done();
});
});
});

describe('Data', function () {
afterEach(function () {
handlebars.Handlebars.unregisterPartial('test');
handlebars.Handlebars.unregisterHelper('helper-function-export');
handlebars.Handlebars.unregisterHelper('test');
clearHandlebars();
});

it('es6 promises should load', function (done) {
Expand Down Expand Up @@ -390,113 +447,23 @@ describe('Gulp Static Handlebars', function () {
});
deferred.resolve(data);
});
});

it('can load helper file exporting single function using filename as name of helper', function (done) {
//arrange
var expectedContents = '<div>contents!</div>\n<div></div>\n<div>Imported Single Helper</div>';

//act
gulp.src('./test/fixtures/test-data-with-helper.html')
.pipe(handlebars({contents: "contents!"}, {helpers: gulp.src('./test/fixtures/helpers/helper-function-export.js')}))
.on('data', function (data) {
expect(data.contents.toString()).to.equal(expectedContents);
}).on('end', function () {
done();
});
});

it('should load partials from data from inline functions', function (done) {
//arrange
var expectedContents = '<div>contents!!</div>\n<div>immediate data</div>';

//act
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: {
'test': function () { return 'immediate data'; }
}}))
.on('data', function (data) {
expect(data.contents.toString()).to.equal(expectedContents);
done();
});
});

it('should load partials from data from mapped promises', function (done) {
//arrange
var expectedContents = '<div>contents!!</div>\n<div>immediate data</div>';

//act
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: {
'test': bluebird.delay('immediate data', 50)
}}))
.on('data', function (data) {
expect(data.contents.toString()).to.equal(expectedContents);
done();
});
});

it('should not load partials from plain mapped strings (must have scope)', function (done) {
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: {
'test': 'things'
}}))
.on('data', function (result) {
expect(result).to.be.instanceOf(File);
})
.on('end', function () {
done();
});
});

it('should not load partials from arrays (must have a name for each partial)', function (done) {
gulp.src('./test/fixtures/test-data-with-partial.html')
.pipe(handlebars({contents: "contents!!"}, {partials: ['things']}))
.on('error', function () {
done();
});
});

it('should ignore null files as data', function (done) {
var f = new File();
gulp.src('./test/fixtures/test-data.html')
.pipe(handlebars(f))
.on('data', function () {})
.on('end', function () {
done();
});
});

it('should ignore null files as partials', function (done) {
var f = new File({
cwd: "/",
base: "/test/",
path: "/test/whatever",
contents: null
});
gulp.src('./test/fixtures/test-data.html')
.pipe(handlebars({contents: "contents!!"}, {partials: f}))
.on('data', function () {})
.on('end', function () {
done();
});
});
it('should ignore null files as data', function (done) {
var nullFile = createNullFile();

it('should ignore null files as helpers', function (done) {
var f = new File({
cwd: "/",
base: "/test/",
path: "/test/whatever",
contents: null
gulp.src('./test/fixtures/test-data.html')
.pipe(handlebars(nullFile))
.on('data', function () {})
.on('end', function () {
done();
});
});
gulp.src('./test/fixtures/test-data.html')
.pipe(handlebars({contents: "contents!!"}, {helpers: f}))
.on('data', function () {})
.on('end', function () {
done();
});
});

/**
* If we can use gulp-rename afterward, then we're passing all the correct data for vinyl files that other gulp
* plugins need.
*/
it('can rename files afterward', function (done) {
//arrange
var expectedContents = '<div>contents!</div>\n<div><test>partial 1 contents!</test></div>';
Expand Down

0 comments on commit 319d0da

Please sign in to comment.