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';
2023-02-18 13:11:01 +03:00
import type { ExportableShortUrl } from '../../../src/short-urls/data';
2023-02-18 12:40:37 +03:00
import type { NormalizedVisit } from '../../../src/visits/types';
2022-07-16 11:52:45 +03:00
import { windowMock } from '../../__mocks__/Window.mock';
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
});