shlink-web-client/test/common/services/ReportExporter.test.ts
2023-08-03 09:13:11 +02:00

70 lines
1.9 KiB
TypeScript

import type { ExportableShortUrl } from '../../../shlink-web-component/src/short-urls/data';
import { ReportExporter } from '../../../shlink-web-component/src/utils/services/ReportExporter';
import type { NormalizedVisit } from '../../../shlink-web-component/src/visits/types';
import { windowMock } from '../../__mocks__/Window.mock';
describe('ReportExporter', () => {
const jsonToCsv = vi.fn();
let exporter: ReportExporter;
beforeEach(() => {
(global as any).Blob = class Blob {};
(global as any).URL = { createObjectURL: () => '' };
exporter = new ReportExporter(windowMock, jsonToCsv);
});
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',
potentialBot: false,
},
];
exporter.exportVisits('my_visits.csv', visits);
expect(jsonToCsv).toHaveBeenCalledWith(visits);
});
it('skips execution when list of visits is empty', () => {
exporter.exportVisits('my_visits.csv', []);
expect(jsonToCsv).not.toHaveBeenCalled();
});
});
describe('exportShortUrls', () => {
it('parses provided short URLs to CSV', () => {
const shortUrls: ExportableShortUrl[] = [
{
shortUrl: 'shortUrl',
visits: 10,
title: '',
createdAt: '',
longUrl: '',
tags: '',
shortCode: '',
},
];
exporter.exportShortUrls(shortUrls);
expect(jsonToCsv).toHaveBeenCalledWith(shortUrls);
});
it('skips execution when list of visits is empty', () => {
exporter.exportShortUrls([]);
expect(jsonToCsv).not.toHaveBeenCalled();
});
});
});