2020-08-22 12:00:11 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-11-14 01:06:03 +03:00
|
|
|
import { CsvJson } from 'csvjson';
|
2020-08-22 12:00:11 +03:00
|
|
|
import ServersImporter from '../../../src/servers/services/ServersImporter';
|
|
|
|
import { RegularServer } from '../../../src/servers/data';
|
|
|
|
|
|
|
|
describe('ServersImporter', () => {
|
|
|
|
const servers: RegularServer[] = [ Mock.all<RegularServer>(), Mock.all<RegularServer>() ];
|
2020-11-14 01:06:03 +03:00
|
|
|
const toObject = jest.fn().mockReturnValue(servers);
|
|
|
|
const csvjsonMock = Mock.of<CsvJson>({ toObject });
|
2020-08-22 12:00:11 +03:00
|
|
|
const readAsText = jest.fn();
|
|
|
|
const fileReaderMock = Mock.of<FileReader>({
|
|
|
|
readAsText,
|
2021-02-28 20:48:36 +03:00
|
|
|
addEventListener: (_eventName: string, listener: (e: ProgressEvent<FileReader>) => void) => listener(
|
|
|
|
Mock.of<ProgressEvent<FileReader>>({ target: { result: '' } }),
|
|
|
|
),
|
2020-08-22 12:00:11 +03:00
|
|
|
});
|
|
|
|
const importer = new ServersImporter(csvjsonMock, () => fileReaderMock);
|
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
|
|
|
describe('importServersFromFile', () => {
|
|
|
|
it('rejects with error if no file was provided', async () => {
|
|
|
|
await expect(importer.importServersFromFile()).rejects.toEqual(
|
|
|
|
new Error('No file provided or file is not a CSV'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects with error if provided file is not a CSV', async () => {
|
|
|
|
await expect(importer.importServersFromFile(Mock.of<File>({ type: 'text/html' }))).rejects.toEqual(
|
|
|
|
new Error('No file provided or file is not a CSV'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-12-30 22:52:05 +03:00
|
|
|
it.each([
|
|
|
|
[ 'text/csv' ],
|
|
|
|
[ 'text/comma-separated-values' ],
|
|
|
|
[ 'application/csv' ],
|
|
|
|
])('reads file when a CSV is provided', async (type) => {
|
|
|
|
await importer.importServersFromFile(Mock.of<File>({ type }));
|
2020-08-22 12:00:11 +03:00
|
|
|
|
|
|
|
expect(readAsText).toHaveBeenCalledTimes(1);
|
2020-11-14 01:06:03 +03:00
|
|
|
expect(toObject).toHaveBeenCalledTimes(1);
|
2020-08-22 12:00:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|