shlink-web-client/test/api/services/ShlinkApiClient.test.ts

333 lines
11 KiB
TypeScript
Raw Normal View History

import { Mock } from 'ts-mockery';
2022-05-28 11:47:39 +03:00
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
2020-08-29 20:51:14 +03:00
import { OptionalString } from '../../../src/utils/utils';
import { ShlinkDomain, ShlinkVisits, ShlinkVisitsOverview } from '../../../src/api/types';
import { ShortUrl, ShortUrlsOrder } from '../../../src/short-urls/data';
import { JsonFetch } from '../../../src/utils/types';
2018-08-04 12:07:28 +03:00
describe('ShlinkApiClient', () => {
const buildFetch = (data: any) => jest.fn().mockResolvedValue(data);
const buildRejectedFetch = (error: any) => jest.fn().mockRejectedValueOnce(error);
const buildApiClient = (fetch: JsonFetch) => new ShlinkApiClient(fetch, '', '');
const shortCodesWithDomainCombinations: [string, OptionalString][] = [
2022-03-26 14:17:42 +03:00
['abc123', null],
['abc123', undefined],
['abc123', 'example.com'],
];
2018-08-04 12:07:28 +03:00
describe('listShortUrls', () => {
2022-03-26 14:17:42 +03:00
const expectedList = ['foo', 'bar'];
2018-08-04 12:07:28 +03:00
it('properly returns short URLs list', async () => {
const { listShortUrls } = buildApiClient(buildFetch({
shortUrls: expectedList,
}));
2018-08-04 12:07:28 +03:00
const actualList = await listShortUrls();
2018-08-04 12:07:28 +03:00
expect(expectedList).toEqual(actualList);
});
it.each([
[{ field: 'visits', dir: 'DESC' } as ShortUrlsOrder, '?orderBy=visits-DESC'],
[{ field: 'longUrl', dir: 'ASC' } as ShortUrlsOrder, '?orderBy=longUrl-ASC'],
[{ field: 'longUrl', dir: undefined } as ShortUrlsOrder, ''],
])('parses orderBy in params', async (orderBy, expectedOrderBy) => {
const fetch = buildFetch({ data: expectedList });
const { listShortUrls } = buildApiClient(fetch);
await listShortUrls({ orderBy });
expect(fetch).toHaveBeenCalledWith(expect.stringContaining(`/short-urls${expectedOrderBy}`), expect.anything());
});
2018-08-04 12:07:28 +03:00
});
2018-08-22 23:38:05 +03:00
describe('createShortUrl', () => {
const shortUrl = {
bar: 'foo',
};
it('returns create short URL', async () => {
const { createShortUrl } = buildApiClient(buildFetch(shortUrl));
2020-11-14 01:06:03 +03:00
const result = await createShortUrl({ longUrl: '' });
2018-08-22 23:38:05 +03:00
expect(result).toEqual(shortUrl);
});
it('removes all empty options', async () => {
const fetch = buildFetch({ data: shortUrl });
const { createShortUrl } = buildApiClient(fetch);
2018-08-22 23:38:05 +03:00
await createShortUrl({ longUrl: 'bar', customSlug: undefined, maxVisits: null });
2018-08-22 23:38:05 +03:00
expect(fetch).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
body: JSON.stringify({ longUrl: 'bar' }),
}));
2018-08-22 23:38:05 +03:00
});
});
describe('getShortUrlVisits', () => {
it('properly returns short URL visits', async () => {
2022-03-26 14:17:42 +03:00
const expectedVisits = ['foo', 'bar'];
const fetch = buildFetch({
visits: {
data: expectedVisits,
2018-08-22 23:38:05 +03:00
},
2020-08-29 20:51:14 +03:00
});
const { getShortUrlVisits } = buildApiClient(fetch);
2018-08-22 23:38:05 +03:00
const actualVisits = await getShortUrlVisits('abc123', {});
2018-08-22 23:38:05 +03:00
2019-01-10 22:05:02 +03:00
expect({ data: expectedVisits }).toEqual(actualVisits);
expect(fetch).toHaveBeenCalledWith(expect.stringContaining('/short-urls/abc123/visits'), expect.objectContaining({
2019-04-19 13:41:59 +03:00
method: 'GET',
}));
2018-08-22 23:38:05 +03:00
});
});
2020-05-10 20:02:58 +03:00
describe('getTagVisits', () => {
it('properly returns tag visits', async () => {
2022-03-26 14:17:42 +03:00
const expectedVisits = ['foo', 'bar'];
const fetch = buildFetch({
visits: {
data: expectedVisits,
2020-05-10 20:02:58 +03:00
},
2020-08-29 20:51:14 +03:00
});
const { getTagVisits } = buildApiClient(fetch);
2020-05-10 20:02:58 +03:00
const actualVisits = await getTagVisits('foo', {});
expect({ data: expectedVisits }).toEqual(actualVisits);
expect(fetch).toHaveBeenCalledWith(expect.stringContaining('/tags/foo/visits'), expect.objectContaining({
2020-05-10 20:02:58 +03:00
method: 'GET',
}));
});
});
describe('getDomainVisits', () => {
it('properly returns domain visits', async () => {
const expectedVisits = ['foo', 'bar'];
const fetch = buildFetch({
visits: {
data: expectedVisits,
},
});
const { getDomainVisits } = buildApiClient(fetch);
const actualVisits = await getDomainVisits('foo.com', {});
expect({ data: expectedVisits }).toEqual(actualVisits);
expect(fetch).toHaveBeenCalledWith(expect.stringContaining('/domains/foo.com/visits'), expect.objectContaining({
method: 'GET',
}));
});
});
2018-08-22 23:38:05 +03:00
describe('getShortUrl', () => {
it.each(shortCodesWithDomainCombinations)('properly returns short URL', async (shortCode, domain) => {
2018-08-22 23:38:05 +03:00
const expectedShortUrl = { foo: 'bar' };
const fetch = buildFetch(expectedShortUrl);
const { getShortUrl } = buildApiClient(fetch);
const expectedQuery = domain ? `?domain=${domain}` : '';
2018-08-22 23:38:05 +03:00
const result = await getShortUrl(shortCode, domain);
2018-08-22 23:38:05 +03:00
expect(expectedShortUrl).toEqual(result);
expect(fetch).toHaveBeenCalledWith(
expect.stringContaining(`/short-urls/${shortCode}${expectedQuery}`),
expect.objectContaining({ method: 'GET' }),
);
2018-08-22 23:38:05 +03:00
});
});
describe('updateShortUrl', () => {
it.each(shortCodesWithDomainCombinations)('properly updates short URL meta', async (shortCode, domain) => {
const meta = {
2020-01-19 15:20:46 +03:00
maxVisits: 50,
validSince: '2025-01-01T10:00:00+01:00',
};
const expectedResp = Mock.of<ShortUrl>();
const fetch = buildFetch(expectedResp);
const { updateShortUrl } = buildApiClient(fetch);
const expectedQuery = domain ? `?domain=${domain}` : '';
2020-01-19 15:20:46 +03:00
const result = await updateShortUrl(shortCode, domain, meta);
2020-01-19 15:20:46 +03:00
expect(expectedResp).toEqual(result);
expect(fetch).toHaveBeenCalledWith(
expect.stringContaining(`/short-urls/${shortCode}${expectedQuery}`),
expect.objectContaining({ method: 'PATCH' }),
);
2020-01-19 15:20:46 +03:00
});
});
2018-08-22 23:38:05 +03:00
describe('listTags', () => {
it('properly returns list of tags', async () => {
2022-03-26 14:17:42 +03:00
const expectedTags = ['foo', 'bar'];
const fetch = buildFetch({
tags: {
data: expectedTags,
2018-08-22 23:38:05 +03:00
},
2020-08-29 20:51:14 +03:00
});
const { listTags } = buildApiClient(fetch);
2018-08-22 23:38:05 +03:00
const result = await listTags();
2018-08-22 23:38:05 +03:00
expect({ tags: expectedTags }).toEqual(result);
expect(fetch).toHaveBeenCalledWith(expect.stringContaining('/tags'), expect.objectContaining({ method: 'GET' }));
2018-08-22 23:38:05 +03:00
});
});
describe('deleteTags', () => {
it('properly deletes provided tags', async () => {
2022-03-26 14:17:42 +03:00
const tags = ['foo', 'bar'];
const fetch = buildFetch({});
const { deleteTags } = buildApiClient(fetch);
2018-08-22 23:38:05 +03:00
await deleteTags(tags);
2018-08-22 23:38:05 +03:00
expect(fetch).toHaveBeenCalledWith(
expect.stringContaining(`/tags?${tags.map((tag) => `tags%5B%5D=${tag}`).join('&')}`),
expect.objectContaining({ method: 'DELETE' }),
);
2018-08-22 23:38:05 +03:00
});
});
describe('editTag', () => {
it('properly edits provided tag', async () => {
2018-08-22 23:38:05 +03:00
const oldName = 'foo';
const newName = 'bar';
const fetch = buildFetch({});
const { editTag } = buildApiClient(fetch);
2018-08-22 23:38:05 +03:00
await editTag(oldName, newName);
2018-08-22 23:38:05 +03:00
expect(fetch).toHaveBeenCalledWith(expect.stringContaining('/tags'), expect.objectContaining({
2019-04-19 13:41:59 +03:00
method: 'PUT',
body: JSON.stringify({ oldName, newName }),
2019-04-19 13:41:59 +03:00
}));
2018-08-22 23:38:05 +03:00
});
});
describe('deleteShortUrl', () => {
it.each(shortCodesWithDomainCombinations)('properly deletes provided short URL', async (shortCode, domain) => {
const fetch = buildFetch({});
const { deleteShortUrl } = buildApiClient(fetch);
const expectedQuery = domain ? `?domain=${domain}` : '';
await deleteShortUrl(shortCode, domain);
expect(fetch).toHaveBeenCalledWith(
expect.stringContaining(`/short-urls/${shortCode}${expectedQuery}`),
expect.objectContaining({ method: 'DELETE' }),
);
});
});
2019-10-05 11:40:32 +03:00
describe('health', () => {
it('returns health data', async () => {
const expectedData = {
status: 'pass',
version: '1.19.0',
};
const fetch = buildFetch(expectedData);
const { health } = buildApiClient(fetch);
2019-10-05 11:40:32 +03:00
const result = await health();
expect(fetch).toHaveBeenCalled();
2019-10-05 11:40:32 +03:00
expect(result).toEqual(expectedData);
});
});
describe('mercureInfo', () => {
it('returns mercure info', async () => {
const expectedData = {
token: 'abc.123.def',
mercureHubUrl: 'http://example.com/.well-known/mercure',
};
const fetch = buildFetch(expectedData);
const { mercureInfo } = buildApiClient(fetch);
const result = await mercureInfo();
expect(fetch).toHaveBeenCalled();
expect(result).toEqual(expectedData);
});
});
2020-11-28 13:45:04 +03:00
describe('listDomains', () => {
it('returns domains', async () => {
2022-03-26 14:17:42 +03:00
const expectedData = { data: [Mock.all<ShlinkDomain>(), Mock.all<ShlinkDomain>()] };
const fetch = buildFetch({ domains: expectedData });
const { listDomains } = buildApiClient(fetch);
2020-11-28 13:45:04 +03:00
const result = await listDomains();
expect(fetch).toHaveBeenCalled();
2020-11-28 13:45:04 +03:00
expect(result).toEqual(expectedData);
});
});
describe('getVisitsOverview', () => {
it('returns visits overview', async () => {
const expectedData = Mock.all<ShlinkVisitsOverview>();
const fetch = buildFetch({ visits: expectedData });
const { getVisitsOverview } = buildApiClient(fetch);
const result = await getVisitsOverview();
expect(fetch).toHaveBeenCalled();
expect(result).toEqual(expectedData);
});
});
describe('getOrphanVisits', () => {
it('returns orphan visits', async () => {
const fetch = buildFetch({ visits: Mock.of<ShlinkVisits>({ data: [] }) });
const { getOrphanVisits } = buildApiClient(fetch);
const result = await getOrphanVisits();
expect(fetch).toHaveBeenCalled();
expect(result).toEqual({ data: [] });
});
});
describe('getNonOrphanVisits', () => {
it('returns non-orphan visits', async () => {
const fetch = buildFetch({ visits: Mock.of<ShlinkVisits>({ data: [] }) });
const { getNonOrphanVisits } = buildApiClient(fetch);
const result = await getNonOrphanVisits();
expect(fetch).toHaveBeenCalled();
expect(result).toEqual({ data: [] });
});
});
describe('editDomainRedirects', () => {
it('returns the redirects', async () => {
const resp = { baseUrlRedirect: null, regular404Redirect: 'foo', invalidShortUrlRedirect: 'bar' };
const fetch = buildFetch(resp);
const { editDomainRedirects } = buildApiClient(fetch);
const result = await editDomainRedirects({ domain: 'foo' });
expect(fetch).toHaveBeenCalled();
expect(result).toEqual(resp);
});
it('retries request if API version is not supported', async () => {
const fetch = buildRejectedFetch({ type: 'NOT_FOUND', status: 404 }).mockImplementation(buildFetch({}));
const { editDomainRedirects } = buildApiClient(fetch);
await editDomainRedirects({ domain: 'foo' });
expect(fetch).toHaveBeenCalledTimes(2);
expect(fetch).toHaveBeenNthCalledWith(1, expect.stringContaining('/v3/'), expect.anything());
expect(fetch).toHaveBeenNthCalledWith(2, expect.stringContaining('/v2/'), expect.anything());
});
});
2018-08-04 12:07:28 +03:00
});