mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 18:27:25 +03:00
Created VisitsExporter test
This commit is contained in:
parent
03f63e3ee3
commit
482489599e
3 changed files with 70 additions and 15 deletions
|
@ -1,5 +1,4 @@
|
|||
import { CsvJson } from 'csvjson';
|
||||
import { head, keys } from 'ramda';
|
||||
import { NormalizedVisit } from '../types';
|
||||
import { saveCsv } from '../../utils/helpers/csv';
|
||||
|
||||
|
@ -10,15 +9,15 @@ export class VisitsExporter {
|
|||
) {}
|
||||
|
||||
public readonly exportVisits = (filename: string, visits: NormalizedVisit[]) => {
|
||||
try {
|
||||
const csv = this.csvjson.toCSV(visits, {
|
||||
headers: keys(head(visits)).join(','),
|
||||
});
|
||||
|
||||
saveCsv(this.window, csv, filename);
|
||||
} catch (e) {
|
||||
// FIXME Handle error
|
||||
console.error(e);
|
||||
if (!visits.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [ firstVisit ] = visits;
|
||||
const csv = this.csvjson.toCSV(visits, {
|
||||
headers: Object.keys(firstVisit).join(','),
|
||||
});
|
||||
|
||||
saveCsv(this.window, csv, filename);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ describe('ServersExporter', () => {
|
|||
});
|
||||
const appendChild = jest.fn();
|
||||
const removeChild = jest.fn();
|
||||
const createWindowMock = () => Mock.of<Window>({
|
||||
const windowMock = Mock.of<Window>({
|
||||
document: {
|
||||
createElement: jest.fn(() => createLinkMock()),
|
||||
createElement: jest.fn(createLinkMock),
|
||||
body: { appendChild, removeChild },
|
||||
},
|
||||
});
|
||||
|
@ -50,12 +50,11 @@ describe('ServersExporter', () => {
|
|||
});
|
||||
afterEach(() => {
|
||||
global.console = originalConsole;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('logs an error if something fails', () => {
|
||||
const csvjsonMock = createCsvjsonMock(true);
|
||||
const exporter = new ServersExporter(storageMock, createWindowMock(), csvjsonMock);
|
||||
const exporter = new ServersExporter(storageMock, windowMock, csvjsonMock);
|
||||
|
||||
exporter.exportServers();
|
||||
|
||||
|
@ -64,7 +63,6 @@ describe('ServersExporter', () => {
|
|||
});
|
||||
|
||||
it('makes use of download link API', () => {
|
||||
const windowMock = createWindowMock();
|
||||
const exporter = new ServersExporter(storageMock, windowMock, createCsvjsonMock());
|
||||
const { document: { createElement } } = windowMock;
|
||||
|
||||
|
|
58
test/visits/services/VisitsExporter.test.ts
Normal file
58
test/visits/services/VisitsExporter.test.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { Mock } from 'ts-mockery';
|
||||
import { CsvJson } from 'csvjson';
|
||||
import { VisitsExporter } from '../../../src/visits/services/VisitsExporter';
|
||||
import { NormalizedVisit } from '../../../src/visits/types';
|
||||
|
||||
describe('VisitsExporter', () => {
|
||||
const createLinkMock = () => ({
|
||||
setAttribute: jest.fn(),
|
||||
click: jest.fn(),
|
||||
style: {},
|
||||
});
|
||||
const windowMock = Mock.of<Window>({
|
||||
document: {
|
||||
createElement: jest.fn(createLinkMock),
|
||||
body: { appendChild: jest.fn(), removeChild: jest.fn() },
|
||||
},
|
||||
});
|
||||
const toCSV = jest.fn();
|
||||
const csvToJsonMock = Mock.of<CsvJson>({ toCSV });
|
||||
let exporter: VisitsExporter;
|
||||
|
||||
beforeEach(jest.clearAllMocks);
|
||||
beforeEach(() => {
|
||||
(global as any).Blob = class Blob {}; // eslint-disable-line @typescript-eslint/no-extraneous-class
|
||||
(global as any).URL = { createObjectURL: () => '' };
|
||||
|
||||
exporter = new VisitsExporter(windowMock, csvToJsonMock);
|
||||
});
|
||||
|
||||
describe('exportVisits', () => {
|
||||
it('parses provided visits to CSV', () => {
|
||||
const visits: NormalizedVisit[] = [
|
||||
{
|
||||
browser: 'browser',
|
||||
city: 'city',
|
||||
country: 'country',
|
||||
date: 'date',
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
os: 'os',
|
||||
referer: 'referer',
|
||||
},
|
||||
];
|
||||
|
||||
exporter.exportVisits('my_visits.csv', visits);
|
||||
|
||||
expect(toCSV).toHaveBeenCalledWith(visits, {
|
||||
headers: 'browser,city,country,date,latitude,longitude,os,referer',
|
||||
});
|
||||
});
|
||||
|
||||
it('skips execution when list of visits is empty', () => {
|
||||
exporter.exportVisits('my_visits.csv', []);
|
||||
|
||||
expect(toCSV).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue