mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-24 11:26:31 +00:00
Add tests for context of filters
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
@@ -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
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "gitbook-plugin-test-filters",
|
||||
"version": "1.0.0",
|
||||
"engines": {
|
||||
"gitbook": "*"
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user