2017-08-23 11:38:33 -05:00
|
|
|
const { ScreenSnippet, readResult } = require('../js/screenSnippet');
|
2017-04-11 13:58:35 -05:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const os = require('os');
|
|
|
|
|
2017-08-23 11:38:33 -05:00
|
|
|
const { isMac } = require('../js/utils/misc.js');
|
2017-04-11 17:17:15 -05:00
|
|
|
|
2017-04-11 13:58:35 -05:00
|
|
|
const snippetBase64 = require('./fixtures/snippet/snippet-base64.js');
|
|
|
|
|
|
|
|
// mock child_process used in ScreenSnippet
|
|
|
|
jest.mock('child_process', function() {
|
|
|
|
return {
|
|
|
|
execFile: mockedExecFile
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mock version of execFile just creates a copy of a test jpeg file.
|
|
|
|
*/
|
|
|
|
function mockedExecFile(util, args, doneCallback) {
|
|
|
|
let outputFileName = args[args.length - 1];
|
|
|
|
|
|
|
|
copyTestFile(outputFileName, function(copyTestFile) {
|
|
|
|
doneCallback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyTestFile(destFile, done) {
|
2017-08-23 11:38:33 -05:00
|
|
|
const testfile = path.join(__dirname,
|
2017-04-11 13:58:35 -05:00
|
|
|
'fixtures/snippet/ScreenSnippet.jpeg');
|
|
|
|
|
|
|
|
let reader = fs.createReadStream(testfile);
|
|
|
|
let writer = fs.createWriteStream(destFile);
|
|
|
|
|
|
|
|
writer.on('close', function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
reader.pipe(writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTestFile(done) {
|
|
|
|
let tmpDir = os.tmpdir();
|
|
|
|
const testFileName = path.join(tmpDir,
|
2017-08-23 11:38:33 -05:00
|
|
|
'ScreenSnippet-' + Date.now() + '.jpeg');
|
2017-04-11 13:58:35 -05:00
|
|
|
|
|
|
|
copyTestFile(testFileName, function() {
|
|
|
|
done(testFileName)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('Tests for ScreenSnippet', function() {
|
|
|
|
describe('when reading a valid jpeg file', function() {
|
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
// skip test for windows - until feature is supported
|
|
|
|
if (isMac) {
|
|
|
|
it('should match base64 output', function(done) {
|
|
|
|
let s = new ScreenSnippet();
|
|
|
|
s.capture().then(gotImage);
|
|
|
|
|
|
|
|
function gotImage(rsp) {
|
|
|
|
expect(rsp.type).toEqual('image/jpg;base64');
|
|
|
|
expect(rsp.data).toEqual(snippetBase64);
|
|
|
|
done();
|
2017-08-24 02:51:02 -05:00
|
|
|
}
|
2017-04-11 17:17:15 -05:00
|
|
|
});
|
|
|
|
}
|
2017-04-11 13:58:35 -05:00
|
|
|
|
|
|
|
it('should remove output file after completed', function(done) {
|
|
|
|
createTestFile(function(testfileName) {
|
2017-06-07 11:05:25 -05:00
|
|
|
readResult(testfileName, resolve);
|
2017-04-11 13:58:35 -05:00
|
|
|
|
|
|
|
function resolve() {
|
|
|
|
// should be long enough before file
|
|
|
|
// gets removed
|
|
|
|
setTimeout(function() {
|
|
|
|
let exists = fs.existsSync(testfileName);
|
|
|
|
expect(exists).toBe(false);
|
|
|
|
done();
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail if output file does not exist', function(done) {
|
2017-06-07 11:05:25 -05:00
|
|
|
let nonExistentFile = 'bogus.jpeg';
|
|
|
|
readResult(nonExistentFile, resolve, reject);
|
2017-04-11 13:58:35 -05:00
|
|
|
|
|
|
|
function resolve() {
|
|
|
|
// shouldn't get here
|
|
|
|
expect(true).toBe(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reject(err) {
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
// skip test for windows - until feature is supported
|
|
|
|
if (isMac) {
|
|
|
|
it('should fail if read file fails', function(done) {
|
2017-08-24 02:51:02 -05:00
|
|
|
const origFsReadFile = fs.readFile;
|
2017-04-11 13:58:35 -05:00
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
fs.readFile = jest.fn(mockedReadFile);
|
2017-04-11 13:58:35 -05:00
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
function mockedReadFile(filename, callback) {
|
|
|
|
callback(new Error('failed'));
|
|
|
|
}
|
2017-04-11 13:58:35 -05:00
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
let s = new ScreenSnippet();
|
|
|
|
s.capture().then(resolved).catch(rejected);
|
2017-04-11 13:58:35 -05:00
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
function resolved(err) {
|
|
|
|
cleanup();
|
|
|
|
// shouldn't get here
|
|
|
|
expect(true).toBe(false);
|
|
|
|
}
|
2017-04-11 13:58:35 -05:00
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
function rejected(err) {
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
cleanup();
|
|
|
|
done();
|
|
|
|
}
|
2017-04-11 13:58:35 -05:00
|
|
|
|
2017-04-11 17:17:15 -05:00
|
|
|
function cleanup() {
|
|
|
|
fs.readFile = origFsReadFile;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-08-23 11:38:33 -05:00
|
|
|
});
|