2018-12-18 21:59:50 +03:00
|
|
|
import ServersImporter from '../../../src/servers/services/ServersImporter';
|
2018-08-24 11:34:32 +03:00
|
|
|
|
|
|
|
describe('ServersImporter', () => {
|
|
|
|
const servers = [{ name: 'foo' }, { name: 'bar' }];
|
|
|
|
const csvjsonMock = {
|
2019-04-19 11:29:49 +03:00
|
|
|
toObject: jest.fn(() => servers),
|
2018-08-24 11:34:32 +03:00
|
|
|
};
|
|
|
|
const importer = new ServersImporter(csvjsonMock);
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
beforeEach(() => csvjsonMock.toObject.mockClear());
|
2018-08-24 11:34:32 +03:00
|
|
|
|
|
|
|
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 () => {
|
2019-04-19 11:29:49 +03:00
|
|
|
const readAsText = jest.fn(() => '');
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
global.FileReader = class FileReader {
|
|
|
|
constructor() {
|
|
|
|
this.readAsText = readAsText;
|
|
|
|
this.addEventListener = (eventName, listener) =>
|
|
|
|
listener({ target: { result: '' } });
|
|
|
|
}
|
2018-08-24 11:34:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await importer.importServersFromFile({ type: 'text/csv' });
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(readAsText).toHaveBeenCalledTimes(1);
|
|
|
|
expect(csvjsonMock.toObject).toHaveBeenCalledTimes(1);
|
2018-08-24 11:34:32 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|