Add Attachment.migrateDataToFileSystem

This commit is contained in:
Daniel Gasienica 2018-03-14 18:59:58 -04:00
parent 0fc2868f0e
commit ebe2a769c9
3 changed files with 118 additions and 0 deletions

View file

@ -1,5 +1,6 @@
require('mocha-testcheck').install();
const stringToArrayBuffer = require('string-to-arraybuffer');
const { assert } = require('chai');
const Attachment = require('../../../js/modules/types/attachment');
@ -101,4 +102,81 @@ describe('Attachment', () => {
assert.deepEqual(actual, expected);
});
});
describe('migrateDataToFileSystem', () => {
it('should write data to disk and store relative path to it', async () => {
const input = {
contentType: 'image/jpeg',
data: stringToArrayBuffer('Above us only sky'),
fileName: 'foo.jpg',
size: 1111,
};
const expected = {
contentType: 'image/jpeg',
path: 'abc/abcdefgh123456789',
fileName: 'foo.jpg',
size: 1111,
};
const expectedAttachmentData = stringToArrayBuffer('Above us only sky');
const writeAttachmentData = async (attachmentData) => {
assert.deepEqual(attachmentData, expectedAttachmentData);
return 'abc/abcdefgh123456789';
};
const actual = await Attachment.migrateDataToFileSystem(
input,
{ writeAttachmentData }
);
assert.deepEqual(actual, expected);
});
it('should skip over (invalid) attachments without data', async () => {
const input = {
contentType: 'image/jpeg',
fileName: 'foo.jpg',
size: 1111,
};
const expected = {
contentType: 'image/jpeg',
fileName: 'foo.jpg',
size: 1111,
};
const writeAttachmentData = async () =>
'abc/abcdefgh123456789';
const actual = await Attachment.migrateDataToFileSystem(
input,
{ writeAttachmentData }
);
assert.deepEqual(actual, expected);
});
it('should throw error if data is not valid', async () => {
const input = {
contentType: 'image/jpeg',
data: 42,
fileName: 'foo.jpg',
size: 1111,
};
const writeAttachmentData = async () =>
'abc/abcdefgh123456789';
try {
await Attachment.migrateDataToFileSystem(input, { writeAttachmentData });
} catch (error) {
assert.strictEqual(
error.message,
'Expected `attachment.data` to be an array buffer; got: number'
);
return;
}
assert.fail('Unreachable');
});
});
});