2021-03-14 15:16:20 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { CsvJson } from 'csvjson';
|
2022-03-13 20:56:42 +03:00
|
|
|
import { ReportExporter } from '../../../src/common/services/ReportExporter';
|
2021-03-14 15:16:20 +03:00
|
|
|
import { NormalizedVisit } from '../../../src/visits/types';
|
2021-09-19 11:57:36 +03:00
|
|
|
import { windowMock } from '../../mocks/WindowMock';
|
2022-03-13 21:07:33 +03:00
|
|
|
import { ExportableShortUrl } from '../../../src/short-urls/data';
|
2021-03-14 15:16:20 +03:00
|
|
|
|
2022-03-13 20:56:42 +03:00
|
|
|
describe('ReportExporter', () => {
|
2021-03-14 15:16:20 +03:00
|
|
|
const toCSV = jest.fn();
|
|
|
|
const csvToJsonMock = Mock.of<CsvJson>({ toCSV });
|
2022-03-13 20:56:42 +03:00
|
|
|
let exporter: ReportExporter;
|
2021-03-14 15:16:20 +03:00
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
beforeEach(() => {
|
|
|
|
(global as any).Blob = class Blob {}; // eslint-disable-line @typescript-eslint/no-extraneous-class
|
|
|
|
(global as any).URL = { createObjectURL: () => '' };
|
|
|
|
|
2022-03-13 20:56:42 +03:00
|
|
|
exporter = new ReportExporter(windowMock, csvToJsonMock);
|
2021-03-14 15:16:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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',
|
2021-06-13 12:54:51 +03:00
|
|
|
potentialBot: false,
|
2021-03-14 15:16:20 +03:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
exporter.exportVisits('my_visits.csv', visits);
|
|
|
|
|
2022-03-13 20:56:42 +03:00
|
|
|
expect(toCSV).toHaveBeenCalledWith(visits, { headers: 'key', wrap: true });
|
2021-03-14 15:16:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('skips execution when list of visits is empty', () => {
|
|
|
|
exporter.exportVisits('my_visits.csv', []);
|
|
|
|
|
|
|
|
expect(toCSV).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-03-13 21:07:33 +03:00
|
|
|
|
|
|
|
describe('exportShortUrls', () => {
|
|
|
|
it('parses provided short URLs to CSV', () => {
|
|
|
|
const shortUrls: ExportableShortUrl[] = [
|
|
|
|
{
|
|
|
|
shortUrl: 'shortUrl',
|
|
|
|
visits: 10,
|
|
|
|
title: '',
|
|
|
|
createdAt: '',
|
|
|
|
longUrl: '',
|
|
|
|
tags: '',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
exporter.exportShortUrls(shortUrls);
|
|
|
|
|
|
|
|
expect(toCSV).toHaveBeenCalledWith(shortUrls, { headers: 'key', wrap: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('skips execution when list of visits is empty', () => {
|
|
|
|
exporter.exportShortUrls([]);
|
|
|
|
|
|
|
|
expect(toCSV).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2021-03-14 15:16:20 +03:00
|
|
|
});
|