shlink-web-client/test/servers/services/ServersImporter.test.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-08-24 11:34:32 +03:00
import sinon from 'sinon';
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 = {
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 = 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' });
expect(readAsText.callCount).toEqual(1);
expect(csvjsonMock.toObject.callCount).toEqual(1);
});
});
});