From 818ec3a4b6a8d6ec09c403f3ae334d5b4f6fdef2 Mon Sep 17 00:00:00 2001 From: roger Date: Thu, 27 Jul 2017 14:56:03 +0800 Subject: [PATCH] return promise --- __test__/zipcelx.spec.js | 5 ++--- lib/zipcelx.js | 4 ++-- src/zipcelx.js | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/__test__/zipcelx.spec.js b/__test__/zipcelx.spec.js index 8c4b407..3f2b210 100644 --- a/__test__/zipcelx.spec.js +++ b/__test__/zipcelx.spec.js @@ -7,10 +7,9 @@ console.error = jest.genMockFn(); describe('Zipcelx', () => { const rowsXML = `Test1000`; - it('Should console log error if validator fails', () => { + it('Should throw error if validator fails', () => { let config = Object.assign({}, baseConfig, { sheet: { data: [{test: 'demo'}] } }); - zipcelx(config); - expect(console.error).toBeCalled(); + expect(() => zipcelx(config)).toThrow(); }); it('Should map row arrays to XML markup', () => { diff --git a/lib/zipcelx.js b/lib/zipcelx.js index d178fb3..79e6a89 100644 --- a/lib/zipcelx.js +++ b/lib/zipcelx.js @@ -50,7 +50,7 @@ var generateXMLWorksheet = exports.generateXMLWorksheet = function generateXMLWo exports.default = function (config) { if (!(0, _validator2.default)(config)) { - return; + throw new Error('Validation failed.'); } var zip = new _jszip2.default(); @@ -63,7 +63,7 @@ exports.default = function (config) { var worksheet = generateXMLWorksheet(config.sheet.data); xl.file('worksheets/sheet1.xml', worksheet); - zip.generateAsync({ type: 'blob' }).then(function (blob) { + return zip.generateAsync({ type: 'blob' }).then(function (blob) { _fileSaver2.default.saveAs(blob, config.filename + '.xlsx'); }); }; \ No newline at end of file diff --git a/src/zipcelx.js b/src/zipcelx.js index ea18823..4eab5b3 100644 --- a/src/zipcelx.js +++ b/src/zipcelx.js @@ -17,7 +17,7 @@ export const generateXMLWorksheet = (rows) => { export default (config) => { if (!validator(config)) { - return; + throw new Error('Validation failed.'); } const zip = new JSZip(); @@ -30,7 +30,7 @@ export default (config) => { const worksheet = generateXMLWorksheet(config.sheet.data); xl.file('worksheets/sheet1.xml', worksheet); - zip.generateAsync({ type: 'blob' }) + return zip.generateAsync({ type: 'blob' }) .then((blob) => { FileSaver.saveAs(blob, `${config.filename}.xlsx`); });