Skip to content

Commit

Permalink
Merge pull request #514 from TunedMidja/fix_394
Browse files Browse the repository at this point in the history
Fixes #394
  • Loading branch information
thinkingserious authored Sep 5, 2018
2 parents b3cd553 + ffb918b commit 0c83ae9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/helpers/attachment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just a little file for testing attachments.
15 changes: 9 additions & 6 deletions packages/helpers/classes/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ class Attachment {
* Set content
*/
setContent(content) {
if (typeof content === 'undefined') {
return;
}
if (typeof content !== 'string') {
throw new Error('String expected for `content`');
//Duck type check toString on content if it's a Buffer as that's the method that will be called.
if (typeof content === 'string') {
this.content = content;
} else if (content instanceof Buffer && content.toString !== undefined) {
this.content = content.toString();
} else {
throw new Error('`content` expected to be either Buffer or string');
}
this.content = content;

return;
}

/**
Expand Down
43 changes: 43 additions & 0 deletions packages/helpers/classes/attachment.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const fs = require('fs');

/**
* Dependencies
*/
const Attachment = require('./attachment');

/**
* Tests
*/
describe('Attachment', function() {
let attachment;
beforeEach(function() {
attachment = new Attachment();
});

//Set content as string
describe('setContent(), string', function() {
it('should set string as content', function() {
attachment.setContent("Just a string.");
expect(attachment.content).to.equal('Just a string.');
});
});

//Set content as stream
describe('setContent(), stream', function() {
it('should convert stream to string and set as content', function() {
const fileData = fs.readFileSync('./packages/helpers/attachment.txt');
attachment.setContent(fileData);
expect(attachment.content).to.equal('Just a little file for testing attachments.');
});
});

//Set content as wrong type
describe('setContent(), wrong type', function() {
it('should not allow setting content of wrong type', function() {
expect(() => attachment.setContent(null)).to.throw('`content` expected to be either Buffer or string');
});
});

});

0 comments on commit 0c83ae9

Please sign in to comment.