shlink-web-client/shlink-web-component/test/utils/services/ReportExporter.test.ts

71 lines
1.8 KiB
TypeScript
Raw Normal View History

import type { ExportableShortUrl } from '../../../src/short-urls/data';
import { ReportExporter } from '../../../src/utils/services/ReportExporter';
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', () => {
2023-05-27 12:57:26 +03:00
const jsonToCsv = vi.fn();
2022-03-13 20:56:42 +03:00
let exporter: ReportExporter;
2021-03-14 15:16:20 +03:00
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: '',
2023-04-22 13:09:15 +03:00
shortCode: '',
2022-03-13 21:07:33 +03:00
},
];
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
});