Add tests for context of filters

This commit is contained in:
Samy Pessé
2016-02-22 15:34:35 +01:00
parent 642ba72a1d
commit 6653307fda
5 changed files with 58 additions and 9 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ function pluginCtx(plugin) {
error.deprecateField(ctx, 'options', book.config.dump(), '"options" property is deprecated, use config.get(key) instead');
// Loop for template filters/blocks
error.deprecateField(ctx, 'book', ctx, '"book" property is deprecated, this directly instead');
error.deprecateField(ctx, 'book', ctx, '"book" property is deprecated, use "this" directly instead');
return ctx;
}
+2 -8
View File
@@ -270,10 +270,7 @@ BookPlugin.prototype.getFilters = function() {
return _.mapValues(this.content.filters || {}, function(fn, filter) {
return function() {
var ctx = _.extend(
this,
pluginCtx(that)
);
var ctx = _.extend(pluginCtx(that), this);
return fn.apply(ctx, arguments);
};
@@ -289,10 +286,7 @@ BookPlugin.prototype.getBlocks = function() {
var fn = block.exec;
block.exec = function() {
var ctx = _.extend(
this,
pluginCtx(that)
);
var ctx = _.extend(pluginCtx(that), this);
return fn.apply(ctx, arguments);
};
+21
View File
@@ -0,0 +1,21 @@
var should = require('should');
module.exports = {
filters: {
// Simple test
hello: function(s) {
return 'Hello ' + s + '!';
},
// Test using the conetxt "this"
testContext: function(s) {
this.should.have.property('config');
this.should.have.property('log');
this.should.have.property('options');
this.should.have.property('resolve').which.is.a.function;
this.should.have.property('book').which.equal(this);
return 'Hello ' + s + '!';
}
}
};
+7
View File
@@ -0,0 +1,7 @@
{
"name": "gitbook-plugin-test-filters",
"version": "1.0.0",
"engines": {
"gitbook": "*"
}
}
+27
View File
@@ -1,3 +1,4 @@
var _ = require('lodash');
var path = require('path');
var mock = require('./mock');
@@ -124,5 +125,31 @@ describe('Plugins', function() {
});
});
});
describe('Filters', function() {
var plugin, filters;
before(function() {
plugin = new BookPlugin(book, 'test-filters');
return plugin.load(PLUGINS_ROOT)
.then(function() {
filters = plugin.getFilters();
});
});
it('should list all resources for website', function() {
_.size(filters).should.equal(2);
});
it('should correctly execute a filter', function() {
filters.hello('World').should.equal('Hello World!');
});
it('should correctly set contexts for filter', function() {
filters.testContext('Hello');
});
});
});