Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Download and Upload files to Nylas using Streams #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions __tests__/file-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import Promise from 'bluebird';
import request from 'request';
import _ from 'underscore';

import {
Readable as ReadableStream,
Writable as WritableStream
} from 'stream';

import Nylas from '../src/nylas';
import NylasConnection from '../src/nylas-connection';
import File from '../src/models/file';
Expand Down Expand Up @@ -180,6 +185,105 @@ describe('File', () => {
});
});

describe('getReadableStream', () => {
test('should do a GET request', done => {
testContext.file.getReadableStream().then(() => {
expect(testContext.connection.request).toHaveBeenCalledWith({
path: '/files/fileId/download',
encoding: null,
downloadRequest: true,
});
done();
});
});

describe('when the request succeeds', () => {
beforeEach(() => {
testContext.connection.request = jest.fn(() => {
const data = 'we are your friends'.split(' ').reverse();
const response = new ReadableStream();

response._read = function() {
if (data.length > 0) {
this.push(data.pop());
} else {
this.push(null);
}
};

return Promise.resolve(response);
});
});

test('should resolve with a readable stream of the file', done => {
testContext.file.getReadableStream().then(fileStream => {
const data = [];
const outStream = new WritableStream();

outStream._write = function(chunk, encoding, _done) {
data.push(chunk.toString('utf-8'));
_done();
};

outStream.on('finish', () => {
const finalData = data.join(' ');
expect(finalData).toEqual('we are your friends');
done();
});

fileStream.pipe(outStream);
});
});

test('should call the callback with the readable stream of the file', done => {
testContext.file.getReadableStream((err, fileStream) => {
const data = [];
const outStream = new WritableStream();

outStream._write = function (chunk, encoding, _done) {
data.push(chunk.toString('utf-8'));
_done();
};

outStream
outStream.on('finish', () => {
const finalData = data.join(' ');
expect(finalData).toEqual('we are your friends');
done();
});

fileStream.pipe(outStream);
});
});
});

describe('when the request fails', () => {
beforeEach(() => {
testContext.error = new Error('Network error');
testContext.connection.request = jest.fn(() => {
return Promise.reject(testContext.error);
});
});

test('should reject with the error', done => {
testContext.file.getReadableStream().catch(err => {
expect(err).toBe(testContext.error);
done();
});
});

test('should call the callback with the error', done => {
testContext.file
.getReadableStream((err, file) => {
expect(err).toBe(testContext.error);
expect(file).toBe(undefined);
done();
})
.catch(() => { });
});
});
});

describe('metadata', () => {
test('should do a GET request', () => {
testContext.file.metadata();
Expand Down
Loading