register
函数中声明了3个函数,因为它们需要hapi.js server
对象来调用其他插件server
函数中的register
对象上只有一个名为“ renderEmailBody”的函数。getFreshTemplateRenderFunc
的函数正在从数据库中获取数据。getFreshTemplateRenderFunc
使用sinon进行存根,以便在测试执行期间独立于数据库。getFreshTemplateRenderFunc
和在哪里存根。server
内部使用getFreshTemplateRenderFunc
对象。getFreshTemplateRenderFunc
“私有”。
////////////
// PLUGIN //
////////////
const register = async(server) => {
// TODO: Make getFreshTemplateRenderFunc stubbable with sinon.stub or .fake
const getFreshTemplateRenderFunc = async(templateName) => {
const {
db
} = server.mongo;
const {
on
} = server.plugins['alyneUtil'];
// needs server
// Calls db, does this and that
// returns an object, throws an error
};
const getTemplateRenderer = memoize(getFreshTemplateRenderFunc);
const renderEmailBody = async(templateName, payload) => {
// some things
const {
body
} = await getTemplateRenderer(templateName);
return await body(payload);
};
server.expose('renderEmailBody', renderEmailBody);
};
exports.plugin = {
name: 'templateManager',
version: '0.0.1',
register
};
//////////////
//UNIT TEST //
//////////////
describe('Templates', function() {
let server;
before(async() => {
// TODO: Stub getFreshTemplateRenderFunc with sinon.stub or .fake here ...
server = await require('../../app').init();
});
describe('Fetch templates', function() {
it('Should fetch a template renderer and render mail', async function() {
// ... or stub it here
const templateName = 'honky.tonk.en_GB';
const greetingPayload = {
greeting: 'Hello World'
};
const expectedBodyResult = '<p>Hello World</p>';
let renderedBody = await server.plugins[
'templateManager'
].renderEmailBody(templateName, greetingPayload);
expect(renderedBody).to.equal(expectedBodyResult);
});
});
});