2021-03-14 15:16:20 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { CsvJson } from 'csvjson';
|
|
|
|
import { VisitsExporter } from '../../../src/visits/services/VisitsExporter';
|
|
|
|
import { NormalizedVisit } from '../../../src/visits/types';
|
2021-09-19 11:57:36 +03:00
|
|
|
import { windowMock } from '../../mocks/WindowMock';
|
2021-03-14 15:16:20 +03:00
|
|
|
|
|
|
|
describe('VisitsExporter', () => {
|
|
|
|
const toCSV = jest.fn();
|
|
|
|
const csvToJsonMock = Mock.of<CsvJson>({ toCSV });
|
|
|
|
let exporter: VisitsExporter;
|
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
beforeEach(() => {
|
|
|
|
(global as any).Blob = class Blob {}; // eslint-disable-line @typescript-eslint/no-extraneous-class
|
|
|
|
(global as any).URL = { createObjectURL: () => '' };
|
|
|
|
|
|
|
|
exporter = new VisitsExporter(windowMock, csvToJsonMock);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-03-14 20:12:10 +03:00
|
|
|
expect(toCSV).toHaveBeenCalledWith(visits, { headers: 'key' });
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|