2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { RegularServer } from '../../../src/servers/data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { ServersImporter } from '../../../src/servers/services/ServersImporter';
|
2020-08-22 12:00:11 +03:00
|
|
|
|
|
|
|
describe('ServersImporter', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const servers: RegularServer[] = [fromPartial<RegularServer>({}), fromPartial<RegularServer>({})];
|
2023-05-27 12:57:26 +03:00
|
|
|
const csvjsonMock = vi.fn().mockResolvedValue(servers);
|
2023-09-02 20:44:29 +03:00
|
|
|
const text = vi.fn().mockReturnValue('');
|
|
|
|
const fileMock = () => fromPartial<File>({ text });
|
|
|
|
const importer = new ServersImporter(csvjsonMock);
|
2020-08-22 12:00:11 +03:00
|
|
|
|
|
|
|
describe('importServersFromFile', () => {
|
2023-09-02 20:44:29 +03:00
|
|
|
it.each([[null], [undefined]])('rejects with error if no file was provided', async (file) => {
|
|
|
|
await expect(importer.importServersFromFile(file)).rejects.toEqual(
|
2021-09-12 10:54:17 +03:00
|
|
|
new Error('No file provided'),
|
2020-08-22 12:00:11 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-12 10:54:17 +03:00
|
|
|
it('rejects with error if parsing the file fails', async () => {
|
|
|
|
const expectedError = new Error('Error parsing file');
|
|
|
|
|
2022-03-31 21:18:05 +03:00
|
|
|
csvjsonMock.mockRejectedValue(expectedError);
|
2021-09-12 10:54:17 +03:00
|
|
|
|
2023-09-02 20:44:29 +03:00
|
|
|
await expect(importer.importServersFromFile(fileMock())).rejects.toEqual(expectedError);
|
2021-09-12 10:54:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[{}],
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined],
|
2021-09-12 10:54:17 +03:00
|
|
|
[[{ foo: 'bar' }]],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
{
|
|
|
|
url: 1,
|
|
|
|
apiKey: 1,
|
|
|
|
name: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
{
|
|
|
|
url: 'foo',
|
|
|
|
apiKey: 'foo',
|
|
|
|
name: 'foo',
|
|
|
|
},
|
|
|
|
{ bar: 'foo' },
|
|
|
|
],
|
|
|
|
],
|
|
|
|
])('rejects with error if provided file does not parse to valid list of servers', async (parsedObject) => {
|
2022-03-31 21:18:05 +03:00
|
|
|
csvjsonMock.mockResolvedValue(parsedObject);
|
2021-09-12 10:54:17 +03:00
|
|
|
|
2023-09-02 20:44:29 +03:00
|
|
|
await expect(importer.importServersFromFile(fileMock())).rejects.toEqual(
|
2021-09-12 10:54:17 +03:00
|
|
|
new Error('Provided file does not have the right format.'),
|
2020-08-22 12:00:11 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-12 10:54:17 +03:00
|
|
|
it('reads file when a CSV containing valid servers is provided', async () => {
|
|
|
|
const expectedServers = [
|
|
|
|
{
|
|
|
|
url: 'foo',
|
|
|
|
apiKey: 'foo',
|
|
|
|
name: 'foo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: 'bar',
|
|
|
|
apiKey: 'bar',
|
|
|
|
name: 'bar',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-03-31 21:18:05 +03:00
|
|
|
csvjsonMock.mockResolvedValue(expectedServers);
|
2021-09-12 10:54:17 +03:00
|
|
|
|
2023-09-02 20:44:29 +03:00
|
|
|
const result = await importer.importServersFromFile(fileMock());
|
2020-08-22 12:00:11 +03:00
|
|
|
|
2021-09-12 10:54:17 +03:00
|
|
|
expect(result).toEqual(expectedServers);
|
2023-09-02 20:44:29 +03:00
|
|
|
expect(text).toHaveBeenCalledTimes(1);
|
2022-03-31 21:18:05 +03:00
|
|
|
expect(csvjsonMock).toHaveBeenCalledTimes(1);
|
2020-08-22 12:00:11 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|