mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-11 10:03:51 +00:00
Move shlink-web-component tests to their own folder
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { LocalStorage } from '../../../../src/utils/services/LocalStorage';
|
||||
import { MAIN_COLOR } from '../../../../src/utils/theme';
|
||||
import { ColorGenerator } from '../../../src/utils/services/ColorGenerator';
|
||||
|
||||
describe('ColorGenerator', () => {
|
||||
let colorGenerator: ColorGenerator;
|
||||
const storageMock = fromPartial<LocalStorage>({
|
||||
set: vi.fn(),
|
||||
get: vi.fn().mockImplementation(() => undefined),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
colorGenerator = new ColorGenerator(storageMock);
|
||||
});
|
||||
|
||||
it('sets a color in the storage and makes it available after that', () => {
|
||||
const color = '#ff0000';
|
||||
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey('foo')).toEqual(color);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('generates a random color when none is available for requested key', () => {
|
||||
expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/));
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('trims and lower cases keys before trying to match', () => {
|
||||
const color = '#ff0000';
|
||||
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey(' foo')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('foO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FoO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO ')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
describe('isColorLightForKey', () => {
|
||||
it.each([
|
||||
[MAIN_COLOR, true],
|
||||
['#8A661C', false],
|
||||
['#F7BE05', true],
|
||||
['#5A02D8', false],
|
||||
['#202786', false],
|
||||
])('returns that the color for a key is light based on the color assigned to that key', (color, isLight) => {
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(isLight).toEqual(colorGenerator.isColorLightForKey('foo'));
|
||||
expect(isLight).toEqual(colorGenerator.isColorLightForKey('foo')); // To cover when color is already calculated
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ImageDownloader } from '../../../src/utils/services/ImageDownloader';
|
||||
import { windowMock } from '../../__mocks__/Window.mock';
|
||||
|
||||
describe('ImageDownloader', () => {
|
||||
const fetch = vi.fn();
|
||||
let imageDownloader: ImageDownloader;
|
||||
|
||||
beforeEach(() => {
|
||||
(global as any).URL = { createObjectURL: () => '' };
|
||||
|
||||
imageDownloader = new ImageDownloader(fetch, windowMock);
|
||||
});
|
||||
|
||||
it('calls URL with response type blob', async () => {
|
||||
fetch.mockResolvedValue({ blob: () => new Blob() });
|
||||
|
||||
await imageDownloader.saveImage('/foo/bar.png', 'my-image.png');
|
||||
|
||||
expect(fetch).toHaveBeenCalledWith('/foo/bar.png');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import { LocalStorage } from '../../../src/utils/services/LocalStorage';
|
||||
|
||||
describe('LocalStorage', () => {
|
||||
const getItem = vi.fn((key) => (key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null));
|
||||
const setItem = vi.fn();
|
||||
const localStorageMock = fromPartial<Storage>({ getItem, setItem });
|
||||
let storage: LocalStorage;
|
||||
|
||||
beforeEach(() => {
|
||||
storage = new LocalStorage(localStorageMock);
|
||||
});
|
||||
|
||||
describe('set', () => {
|
||||
it('writes an stringified representation of provided value in local storage', () => {
|
||||
const value = { bar: 'baz' };
|
||||
|
||||
storage.set('foo', value);
|
||||
|
||||
expect(setItem).toHaveBeenCalledTimes(1);
|
||||
expect(setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('fetches item from local storage', () => {
|
||||
storage.get('foo');
|
||||
expect(getItem).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('returns parsed value when requested value is found in local storage', () => {
|
||||
const value = storage.get('foo');
|
||||
|
||||
expect(value).toEqual({ foo: 'bar' });
|
||||
});
|
||||
|
||||
it('returns undefined when requested value is not found in local storage', () => {
|
||||
const value = storage.get('bar');
|
||||
|
||||
expect(value).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { ExportableShortUrl } from '../../../src/short-urls/data';
|
||||
import { ReportExporter } from '../../../src/utils/services/ReportExporter';
|
||||
import type { NormalizedVisit } from '../../../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();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ColorGenerator } from '../../../../src/utils/services/ColorGenerator';
|
||||
|
||||
export const colorGeneratorMock = fromPartial<ColorGenerator>({
|
||||
getColorForKey: vi.fn(() => 'red'),
|
||||
setColorForKey: vi.fn(),
|
||||
isColorLightForKey: vi.fn(() => false),
|
||||
});
|
||||
Reference in New Issue
Block a user