Skip to content

Commit

Permalink
Allow absolute template paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pke committed Oct 4, 2018
1 parent a296f10 commit 1b279e8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Expand Up @@ -106,13 +106,16 @@ class Email {
}

// a simple helper function that gets the actual file path for the template
async getTemplatePath(view) {
async getTemplatePath(template) {
const [root, view] = path.isAbsolute(template)
? [path.dirname(template), path.basename(template)]
: [this.config.views.root, template];
const paths = await getPaths(
this.config.views.root,
root,
view,
this.config.views.options.extension
);
const filePath = path.resolve(this.config.views.root, paths.rel);
const filePath = path.resolve(root, paths.rel);
return { filePath, paths };
}

Expand Down
42 changes: 42 additions & 0 deletions test/test.js
Expand Up @@ -101,6 +101,48 @@ test('send email', async t => {
t.regex(message.text, /This is just a text test/);
});

test('throws with non-existing absolute template path', async t => {
const email = new Email({
transport: {
jsonTransport: true
},
juiceResources: {
webResources: {
relativeTo: root
}
}
});
const nonExistingAbsolutePath = path.join(
__dirname,
'fixtures',
'emails',
'tests'
);
const error = await t.throws(email.render(nonExistingAbsolutePath));
t.regex(error.message, /no such file or directory/);
});

test('sends with absolute template path', async t => {
const email = new Email({
transport: {
jsonTransport: true
},
juiceResources: {
webResources: {
relativeTo: root
}
}
});
const absolutePath = path.join(__dirname, 'fixtures', 'emails', 'test');
const res = await email.send({ template: absolutePath });
t.true(_.isObject(res));
const message = JSON.parse(res.message);
t.true(_.has(message, 'html'));
t.regex(message.html, /This is just a html test/);
t.true(_.has(message, 'text'));
t.regex(message.text, /This is just a text test/);
});

test('send email with subject prefix', async t => {
const email = new Email({
views: { root },
Expand Down

0 comments on commit 1b279e8

Please sign in to comment.