import { Mock } from 'ts-mockery'; import { CsvJson } from 'csvjson'; import ServersImporter from '../../../src/servers/services/ServersImporter'; import { RegularServer } from '../../../src/servers/data'; describe('ServersImporter', () => { const servers: RegularServer[] = [ Mock.all(), Mock.all() ]; const toObject = jest.fn().mockReturnValue(servers); const csvjsonMock = Mock.of({ toObject }); const readAsText = jest.fn(); const fileReaderMock = Mock.of({ readAsText, addEventListener: (_eventName: string, listener: Function) => listener({ target: { result: '' } }), }); 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({ type: 'text/html' }))).rejects.toEqual( new Error('No file provided or file is not a CSV'), ); }); it.each([ [ 'text/csv' ], [ 'text/comma-separated-values' ], [ 'application/csv' ], ])('reads file when a CSV is provided', async (type) => { await importer.importServersFromFile(Mock.of({ type })); expect(readAsText).toHaveBeenCalledTimes(1); expect(toObject).toHaveBeenCalledTimes(1); }); }); });