From 53a4240219f007b71367a69b97ef2caf5935b045 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 24 Aug 2018 10:34:32 +0200 Subject: [PATCH] Created ServersImporter test --- src/servers/services/ServersImporter.js | 4 +- test/servers/services/ServersImporter.test.js | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/servers/services/ServersImporter.test.js diff --git a/src/servers/services/ServersImporter.js b/src/servers/services/ServersImporter.js index 47ed4811..c60770a1 100644 --- a/src/servers/services/ServersImporter.js +++ b/src/servers/services/ServersImporter.js @@ -17,12 +17,12 @@ export class ServersImporter { const reader = new FileReader(); return new Promise(resolve => { - reader.onloadend = e => { + reader.addEventListener('loadend', e => { const content = e.target.result; const servers = this.csvjson.toObject(content); resolve(servers); - }; + }); reader.readAsText(file); }); }; diff --git a/test/servers/services/ServersImporter.test.js b/test/servers/services/ServersImporter.test.js new file mode 100644 index 00000000..0d10df19 --- /dev/null +++ b/test/servers/services/ServersImporter.test.js @@ -0,0 +1,44 @@ +import { ServersImporter } from '../../../src/servers/services/ServersImporter'; +import sinon from 'sinon'; + +describe('ServersImporter', () => { + const servers = [{ name: 'foo' }, { name: 'bar' }]; + const csvjsonMock = { + toObject: sinon.fake.returns(servers), + }; + const importer = new ServersImporter(csvjsonMock); + + beforeEach(() => csvjsonMock.toObject.resetHistory()); + + describe('importServersFromFile', () => { + it('rejects with error if no file was provided', async () => { + try { + await importer.importServersFromFile(); + } catch (e) { + expect(e).toEqual('No file provided or file is not a CSV'); + } + }); + + it('rejects with error if provided file is not a CSV', async () => { + try { + await importer.importServersFromFile({ type: 'text/html' }); + } catch (e) { + expect(e).toEqual('No file provided or file is not a CSV'); + } + }); + + it('reads file when a CSV is provided', async () => { + const readAsText = sinon.fake.returns(''); + global.FileReader = function FileReader() { + this.readAsText = readAsText; + this.addEventListener = (eventName, listener) => + listener({ target: { result: '' } }); + }; + + await importer.importServersFromFile({ type: 'text/csv' }); + + expect(readAsText.callCount).toEqual(1); + expect(csvjsonMock.toObject.callCount).toEqual(1); + }); + }); +});