shlink-web-client/test/common/services/ReportExporter.test.ts

71 lines
1.8 KiB
TypeScript
Raw Normal View History

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', () => {
2022-03-31 21:18:05 +03:00
const jsonToCsv = jest.fn();
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 {};
2021-03-14 15:16:20 +03:00
(global as any).URL = { createObjectURL: () => '' };
2022-03-31 21:18:05 +03:00
exporter = new ReportExporter(windowMock, jsonToCsv);
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-31 21:18:05 +03:00
expect(jsonToCsv).toHaveBeenCalledWith(visits);
2021-03-14 15:16:20 +03:00
});
it('skips execution when list of visits is empty', () => {
exporter.exportVisits('my_visits.csv', []);
2022-03-31 21:18:05 +03:00
expect(jsonToCsv).not.toHaveBeenCalled();
2021-03-14 15:16:20 +03:00
});
});
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);
2022-03-31 21:18:05 +03:00
expect(jsonToCsv).toHaveBeenCalledWith(shortUrls);
2022-03-13 21:07:33 +03:00
});
it('skips execution when list of visits is empty', () => {
exporter.exportShortUrls([]);
2022-03-31 21:18:05 +03:00
expect(jsonToCsv).not.toHaveBeenCalled();
2022-03-13 21:07:33 +03:00
});
});
2021-03-14 15:16:20 +03:00
});