mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-16 12:33:48 +00:00
Simplify ServersImporter using file.text() instead of a FileReader
This commit is contained in:
@@ -5,18 +5,13 @@ import { ServersImporter } from '../../../src/servers/services/ServersImporter';
|
||||
describe('ServersImporter', () => {
|
||||
const servers: RegularServer[] = [fromPartial<RegularServer>({}), fromPartial<RegularServer>({})];
|
||||
const csvjsonMock = vi.fn().mockResolvedValue(servers);
|
||||
const readAsText = vi.fn();
|
||||
const fileReaderMock = fromPartial<FileReader>({
|
||||
readAsText,
|
||||
addEventListener: ((_eventName: string, listener: (e: ProgressEvent<FileReader>) => void) => listener(
|
||||
fromPartial({ target: { result: '' } }),
|
||||
)) as any,
|
||||
});
|
||||
const importer = new ServersImporter(csvjsonMock, () => fileReaderMock);
|
||||
const text = vi.fn().mockReturnValue('');
|
||||
const fileMock = () => fromPartial<File>({ text });
|
||||
const importer = new ServersImporter(csvjsonMock);
|
||||
|
||||
describe('importServersFromFile', () => {
|
||||
it('rejects with error if no file was provided', async () => {
|
||||
await expect(importer.importServersFromFile()).rejects.toEqual(
|
||||
it.each([[null], [undefined]])('rejects with error if no file was provided', async (file) => {
|
||||
await expect(importer.importServersFromFile(file)).rejects.toEqual(
|
||||
new Error('No file provided'),
|
||||
);
|
||||
});
|
||||
@@ -26,7 +21,7 @@ describe('ServersImporter', () => {
|
||||
|
||||
csvjsonMock.mockRejectedValue(expectedError);
|
||||
|
||||
await expect(importer.importServersFromFile(fromPartial({ type: 'text/html' }))).rejects.toEqual(expectedError);
|
||||
await expect(importer.importServersFromFile(fileMock())).rejects.toEqual(expectedError);
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -55,7 +50,7 @@ describe('ServersImporter', () => {
|
||||
])('rejects with error if provided file does not parse to valid list of servers', async (parsedObject) => {
|
||||
csvjsonMock.mockResolvedValue(parsedObject);
|
||||
|
||||
await expect(importer.importServersFromFile(fromPartial({ type: 'text/html' }))).rejects.toEqual(
|
||||
await expect(importer.importServersFromFile(fileMock())).rejects.toEqual(
|
||||
new Error('Provided file does not have the right format.'),
|
||||
);
|
||||
});
|
||||
@@ -76,10 +71,10 @@ describe('ServersImporter', () => {
|
||||
|
||||
csvjsonMock.mockResolvedValue(expectedServers);
|
||||
|
||||
const result = await importer.importServersFromFile(fromPartial({}));
|
||||
const result = await importer.importServersFromFile(fileMock());
|
||||
|
||||
expect(result).toEqual(expectedServers);
|
||||
expect(readAsText).toHaveBeenCalledTimes(1);
|
||||
expect(text).toHaveBeenCalledTimes(1);
|
||||
expect(csvjsonMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user