Skip to content

Commit

Permalink
Add mocha tests (for browser)
Browse files Browse the repository at this point in the history
  • Loading branch information
at0g committed Mar 24, 2015
1 parent 3a4bec9 commit 46ff5e3
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .npmignore
@@ -1,2 +1,3 @@
examples
node_modules
node_modules
test
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"description": "A nunjucks loader for webpack.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "webpack-dev-server --config test/webpack.config.js"
},
"repository": {
"type": "git",
Expand All @@ -25,5 +25,11 @@
"homepage": "https://github.com/at0g/nunjucks-loader",
"peerDependencies": {
"nunjucks": "1.x"
},
"devDependencies": {
"chai": "^2.1.2",
"mocha": "^2.2.1",
"mocha-loader": "^0.7.1",
"nunjucks": "^1.1.0"
}
}
1 change: 1 addition & 0 deletions test/fixtures/templates/async-filter.nunj
@@ -0,0 +1 @@
{{ number | square }}
4 changes: 4 additions & 0 deletions test/fixtures/templates/child.nunj
@@ -0,0 +1,4 @@
{% extends './parent.nunj' %}
{% block content %}
Child says: hello {{ name | default('world') }}
{% endblock %}
3 changes: 3 additions & 0 deletions test/fixtures/templates/parent.nunj
@@ -0,0 +1,3 @@
<div class="content">
{% block content %}{% endblock %}
</div>
1 change: 1 addition & 0 deletions test/fixtures/templates/standard-filter.nunj
@@ -0,0 +1 @@
{{ number | double }}
6 changes: 6 additions & 0 deletions test/index.html
@@ -0,0 +1,6 @@
<html>
<head></head>
<body>
<script src="/bundle.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
14 changes: 14 additions & 0 deletions test/nunjucks.config.js
@@ -0,0 +1,14 @@
module.exports = function(env) {

env.addFilter('double', function(input) {
return input * 2;
});

env.addFilter('square', function(input, done) {
var result = input * input;
setTimeout(function() {
done(null, result);
}, 10);
}, true)

}
89 changes: 89 additions & 0 deletions test/web.js
@@ -0,0 +1,89 @@
var chai = require('chai');
var should = chai.should();

describe('path resolution', function() {

it('should resolve paths using webpack resolve', function() {
var tpl1 = require('./fixtures/templates/child.nunj');
var tpl2 = require('child.nunj');

tpl1.render.should.be.a.Function;
tpl2.render.should.be.a.Function;
tpl1.render().should.equal(tpl2.render());
});

});


describe('template inheritance', function() {

beforeEach(function() {
this.tpl = require('child.nunj');
});

it('should inherit from parent template', function() {
this.tpl.render.should.be.a.Function;
});

it('should render a default argument', function() {
var result = this.tpl.render();
result.should.be.a.String
result.should.contain('hello world');
result.should.contain('<div class="content">')
});

it('should render using the data context', function() {
var context = { name: 'everyone'};
var result = this.tpl.render(context);
result.should.be.a.String
result.should.contain('hello ' + context.name);
});

});


describe('environment config', function() {

describe('filters', function() {

before(function() {
this.tpl = require('standard-filter.nunj');
});

it('should register "double" filter', function() {
this.tpl.render.should.be.a.Function;
});

it('should render (a)sync', function(done) {
this.tpl.render({number: 2}).should.equal('4');
this.tpl.render({number: 20}, function(err, result) {
should.not.exist(err);
result.should.equal('40');
done();
});
});

});

describe('filters [async]', function() {

before(function() {
this.tpl = require('async-filter.nunj');
});

it('should add "square" async filter', function() {
this.tpl.render.should.be.a.Function;
});

it('should render async only', function(done) {
should.not.exist(this.tpl.render());
this.tpl.render({ number: 2 }, function(err, result) {
should.not.exist(err);
result.should.be.a.Number;
done();
});
});

});

});
35 changes: 35 additions & 0 deletions test/webpack.config.js
@@ -0,0 +1,35 @@
module.exports = {

context: __dirname,

entry: 'mocha!./web.js',

output: {
path: __dirname,
filename: 'bundle.js'
},

module: {
loaders: [
{
test: /\.(nunj|nunjucks)$/,
loader: 'index',
query: {
config: __dirname + '/nunjucks.config.js'
}
}
]
},

resolve: {
root: [
__dirname,
__dirname + '/fixtures/templates'
]
},

resolveLoader: {
modulesDirectories: ['web_loaders', 'web_modules', 'node_loaders', 'node_modules', '../']
}

};

0 comments on commit 46ff5e3

Please sign in to comment.