mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-20 05:26:20 +00:00
Replaced unmaintained dependency
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { CsvJson } from 'csvjson';
|
||||
import { ReportExporter } from '../../../src/common/services/ReportExporter';
|
||||
import { NormalizedVisit } from '../../../src/visits/types';
|
||||
import { windowMock } from '../../mocks/WindowMock';
|
||||
import { ExportableShortUrl } from '../../../src/short-urls/data';
|
||||
|
||||
describe('ReportExporter', () => {
|
||||
const toCSV = jest.fn();
|
||||
const csvToJsonMock = Mock.of<CsvJson>({ toCSV });
|
||||
const jsonToCsv = jest.fn();
|
||||
let exporter: ReportExporter;
|
||||
|
||||
beforeEach(jest.clearAllMocks);
|
||||
@@ -15,7 +12,7 @@ describe('ReportExporter', () => {
|
||||
(global as any).Blob = class Blob {};
|
||||
(global as any).URL = { createObjectURL: () => '' };
|
||||
|
||||
exporter = new ReportExporter(windowMock, csvToJsonMock);
|
||||
exporter = new ReportExporter(windowMock, jsonToCsv);
|
||||
});
|
||||
|
||||
describe('exportVisits', () => {
|
||||
@@ -36,13 +33,13 @@ describe('ReportExporter', () => {
|
||||
|
||||
exporter.exportVisits('my_visits.csv', visits);
|
||||
|
||||
expect(toCSV).toHaveBeenCalledWith(visits, { headers: 'key', wrap: true });
|
||||
expect(jsonToCsv).toHaveBeenCalledWith(visits);
|
||||
});
|
||||
|
||||
it('skips execution when list of visits is empty', () => {
|
||||
exporter.exportVisits('my_visits.csv', []);
|
||||
|
||||
expect(toCSV).not.toHaveBeenCalled();
|
||||
expect(jsonToCsv).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -61,13 +58,13 @@ describe('ReportExporter', () => {
|
||||
|
||||
exporter.exportShortUrls(shortUrls);
|
||||
|
||||
expect(toCSV).toHaveBeenCalledWith(shortUrls, { headers: 'key', wrap: true });
|
||||
expect(jsonToCsv).toHaveBeenCalledWith(shortUrls);
|
||||
});
|
||||
|
||||
it('skips execution when list of visits is empty', () => {
|
||||
exporter.exportShortUrls([]);
|
||||
|
||||
expect(toCSV).not.toHaveBeenCalled();
|
||||
expect(jsonToCsv).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { CsvJson } from 'csvjson';
|
||||
import ServersExporter from '../../../src/servers/services/ServersExporter';
|
||||
import LocalStorage from '../../../src/utils/services/LocalStorage';
|
||||
import { appendChild, removeChild, windowMock } from '../../mocks/WindowMock';
|
||||
@@ -22,9 +21,7 @@ describe('ServersExporter', () => {
|
||||
const erroneousToCsv = jest.fn(() => {
|
||||
throw new Error('');
|
||||
});
|
||||
const createCsvjsonMock = (throwError = false) => Mock.of<CsvJson>({
|
||||
toCSV: throwError ? erroneousToCsv : jest.fn(() => ''),
|
||||
});
|
||||
const createCsvjsonMock = (throwError = false) => (throwError ? erroneousToCsv : jest.fn(() => ''));
|
||||
|
||||
beforeEach(jest.clearAllMocks);
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
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<RegularServer>(), Mock.all<RegularServer>()];
|
||||
const toObject = jest.fn().mockReturnValue(servers);
|
||||
const csvjsonMock = Mock.of<CsvJson>({ toObject });
|
||||
const csvjsonMock = jest.fn().mockResolvedValue(servers);
|
||||
const readAsText = jest.fn();
|
||||
const fileReaderMock = Mock.of<FileReader>({
|
||||
readAsText,
|
||||
@@ -28,9 +26,7 @@ describe('ServersImporter', () => {
|
||||
it('rejects with error if parsing the file fails', async () => {
|
||||
const expectedError = new Error('Error parsing file');
|
||||
|
||||
toObject.mockImplementation(() => {
|
||||
throw expectedError;
|
||||
});
|
||||
csvjsonMock.mockRejectedValue(expectedError);
|
||||
|
||||
await expect(importer.importServersFromFile(Mock.of<File>({ type: 'text/html' }))).rejects.toEqual(expectedError);
|
||||
});
|
||||
@@ -59,7 +55,7 @@ describe('ServersImporter', () => {
|
||||
],
|
||||
],
|
||||
])('rejects with error if provided file does not parse to valid list of servers', async (parsedObject) => {
|
||||
toObject.mockReturnValue(parsedObject);
|
||||
csvjsonMock.mockResolvedValue(parsedObject);
|
||||
|
||||
await expect(importer.importServersFromFile(Mock.of<File>({ type: 'text/html' }))).rejects.toEqual(
|
||||
new Error('Provided file does not have the right format.'),
|
||||
@@ -80,13 +76,13 @@ describe('ServersImporter', () => {
|
||||
},
|
||||
];
|
||||
|
||||
toObject.mockReturnValue(expectedServers);
|
||||
csvjsonMock.mockResolvedValue(expectedServers);
|
||||
|
||||
const result = await importer.importServersFromFile(Mock.all<File>());
|
||||
|
||||
expect(result).toEqual(expectedServers);
|
||||
expect(readAsText).toHaveBeenCalledTimes(1);
|
||||
expect(toObject).toHaveBeenCalledTimes(1);
|
||||
expect(csvjsonMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user