2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-05-28 11:47:39 +03:00
|
|
|
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShlinkDomain, ShlinkVisits, ShlinkVisitsOverview } from '../../../src/api/types';
|
2022-11-15 14:19:21 +03:00
|
|
|
import { ErrorTypeV2, ErrorTypeV3 } from '../../../src/api/types/errors';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { HttpClient } from '../../../src/common/services/HttpClient';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrl, ShortUrlsOrder } from '../../../src/short-urls/data';
|
|
|
|
import type { OptionalString } from '../../../src/utils/utils';
|
2018-08-04 12:07:28 +03:00
|
|
|
|
|
|
|
describe('ShlinkApiClient', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const fetchJson = vi.fn().mockResolvedValue({});
|
|
|
|
const fetchEmpty = vi.fn().mockResolvedValue(undefined);
|
2023-04-13 22:48:29 +03:00
|
|
|
const httpClient = fromPartial<HttpClient>({ fetchJson, fetchEmpty });
|
2022-11-15 22:31:35 +03:00
|
|
|
const buildApiClient = () => new ShlinkApiClient(httpClient, '', '');
|
2022-11-15 01:06:06 +03:00
|
|
|
const shortCodesWithDomainCombinations: [string, OptionalString][] = [
|
2022-03-26 14:17:42 +03:00
|
|
|
['abc123', null],
|
|
|
|
['abc123', undefined],
|
|
|
|
['abc123', 'example.com'],
|
2020-02-08 11:07:55 +03:00
|
|
|
];
|
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
|
|
|
|
2021-12-15 01:01:19 +03:00
|
|
|
it('properly returns short URLs list', async () => {
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({ shortUrls: expectedList });
|
|
|
|
const { listShortUrls } = buildApiClient();
|
2018-08-04 12:07:28 +03:00
|
|
|
|
2019-01-10 21:17:15 +03:00
|
|
|
const actualList = await listShortUrls();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-04 12:07:28 +03:00
|
|
|
expect(expectedList).toEqual(actualList);
|
|
|
|
});
|
2021-12-15 01:01:19 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-11-15 01:06:06 +03:00
|
|
|
[{ field: 'visits', dir: 'DESC' } as ShortUrlsOrder, '?orderBy=visits-DESC'],
|
|
|
|
[{ field: 'longUrl', dir: 'ASC' } as ShortUrlsOrder, '?orderBy=longUrl-ASC'],
|
|
|
|
[{ field: 'longUrl', dir: undefined } as ShortUrlsOrder, ''],
|
2021-12-15 01:01:19 +03:00
|
|
|
])('parses orderBy in params', async (orderBy, expectedOrderBy) => {
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({ data: expectedList });
|
|
|
|
const { listShortUrls } = buildApiClient();
|
2021-12-15 01:01:19 +03:00
|
|
|
|
|
|
|
await listShortUrls({ orderBy });
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining(`/short-urls${expectedOrderBy}`),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
2021-12-15 01:01:19 +03:00
|
|
|
});
|
2022-12-29 00:58:47 +03:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
[{}, ''],
|
|
|
|
[{ excludeMaxVisitsReached: false }, ''],
|
|
|
|
[{ excludeMaxVisitsReached: true }, '?excludeMaxVisitsReached=true'],
|
|
|
|
[{ excludePastValidUntil: false }, ''],
|
|
|
|
[{ excludePastValidUntil: true }, '?excludePastValidUntil=true'],
|
|
|
|
[
|
|
|
|
{ excludePastValidUntil: true, excludeMaxVisitsReached: true },
|
|
|
|
'?excludeMaxVisitsReached=true&excludePastValidUntil=true',
|
|
|
|
],
|
|
|
|
])('parses disabled URLs params', async (params, expectedQuery) => {
|
|
|
|
fetchJson.mockResolvedValue({ data: expectedList });
|
|
|
|
const { listShortUrls } = buildApiClient();
|
|
|
|
|
|
|
|
await listShortUrls(params);
|
|
|
|
|
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining(`/short-urls${expectedQuery}`),
|
|
|
|
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 () => {
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(shortUrl);
|
|
|
|
const { createShortUrl } = buildApiClient();
|
2020-11-14 01:06:03 +03:00
|
|
|
const result = await createShortUrl({ longUrl: '' });
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-22 23:38:05 +03:00
|
|
|
expect(result).toEqual(shortUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes all empty options', async () => {
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({ data: shortUrl });
|
|
|
|
const { createShortUrl } = buildApiClient();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2021-03-27 11:49:47 +03:00
|
|
|
await createShortUrl({ longUrl: 'bar', customSlug: undefined, maxVisits: null });
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
|
2022-11-15 01:06:06 +03:00
|
|
|
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'];
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({
|
2022-11-15 01:06:06 +03:00
|
|
|
visits: {
|
|
|
|
data: expectedVisits,
|
2018-08-22 23:38:05 +03:00
|
|
|
},
|
2020-08-29 20:51:14 +03:00
|
|
|
});
|
2022-11-15 22:31:35 +03:00
|
|
|
const { getShortUrlVisits } = buildApiClient();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2019-01-10 21:17:15 +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);
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining('/short-urls/abc123/visits'),
|
|
|
|
expect.objectContaining({ 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'];
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({
|
2022-11-15 01:06:06 +03:00
|
|
|
visits: {
|
|
|
|
data: expectedVisits,
|
2020-05-10 20:02:58 +03:00
|
|
|
},
|
2020-08-29 20:51:14 +03:00
|
|
|
});
|
2022-11-15 22:31:35 +03:00
|
|
|
const { getTagVisits } = buildApiClient();
|
2020-05-10 20:02:58 +03:00
|
|
|
|
|
|
|
const actualVisits = await getTagVisits('foo', {});
|
|
|
|
|
|
|
|
expect({ data: expectedVisits }).toEqual(actualVisits);
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(expect.stringContaining('/tags/foo/visits'), expect.objectContaining({
|
2020-05-10 20:02:58 +03:00
|
|
|
method: 'GET',
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-24 19:54:44 +03:00
|
|
|
describe('getDomainVisits', () => {
|
|
|
|
it('properly returns domain visits', async () => {
|
|
|
|
const expectedVisits = ['foo', 'bar'];
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({
|
2022-11-15 01:06:06 +03:00
|
|
|
visits: {
|
|
|
|
data: expectedVisits,
|
2022-04-24 19:54:44 +03:00
|
|
|
},
|
|
|
|
});
|
2022-11-15 22:31:35 +03:00
|
|
|
const { getDomainVisits } = buildApiClient();
|
2022-04-24 19:54:44 +03:00
|
|
|
|
|
|
|
const actualVisits = await getDomainVisits('foo.com', {});
|
|
|
|
|
|
|
|
expect({ data: expectedVisits }).toEqual(actualVisits);
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining('/domains/foo.com/visits'),
|
|
|
|
expect.objectContaining({ method: 'GET' }),
|
|
|
|
);
|
2022-04-24 19:54:44 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-22 23:38:05 +03:00
|
|
|
describe('getShortUrl', () => {
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each(shortCodesWithDomainCombinations)('properly returns short URL', async (shortCode, domain) => {
|
2018-08-22 23:38:05 +03:00
|
|
|
const expectedShortUrl = { foo: 'bar' };
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(expectedShortUrl);
|
|
|
|
const { getShortUrl } = buildApiClient();
|
2022-11-15 01:06:06 +03:00
|
|
|
const expectedQuery = domain ? `?domain=${domain}` : '';
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2020-02-08 11:07:55 +03:00
|
|
|
const result = await getShortUrl(shortCode, domain);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
|
|
|
expect(expectedShortUrl).toEqual(result);
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
2022-11-15 01:06:06 +03:00
|
|
|
expect.stringContaining(`/short-urls/${shortCode}${expectedQuery}`),
|
|
|
|
expect.objectContaining({ method: 'GET' }),
|
|
|
|
);
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-27 11:49:47 +03:00
|
|
|
describe('updateShortUrl', () => {
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each(shortCodesWithDomainCombinations)('properly updates short URL meta', async (shortCode, domain) => {
|
2021-02-27 11:49:56 +03:00
|
|
|
const meta = {
|
2020-01-19 15:20:46 +03:00
|
|
|
maxVisits: 50,
|
|
|
|
validSince: '2025-01-01T10:00:00+01:00',
|
|
|
|
};
|
2023-04-13 22:48:29 +03:00
|
|
|
const expectedResp = fromPartial<ShortUrl>({});
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(expectedResp);
|
|
|
|
const { updateShortUrl } = buildApiClient();
|
2022-11-15 01:06:06 +03:00
|
|
|
const expectedQuery = domain ? `?domain=${domain}` : '';
|
2020-01-19 15:20:46 +03:00
|
|
|
|
2021-03-27 11:49:47 +03:00
|
|
|
const result = await updateShortUrl(shortCode, domain, meta);
|
2020-01-19 15:20:46 +03:00
|
|
|
|
2021-02-27 11:49:56 +03:00
|
|
|
expect(expectedResp).toEqual(result);
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
2022-11-15 01:06:06 +03:00
|
|
|
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'];
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({
|
2022-11-15 01:06:06 +03:00
|
|
|
tags: {
|
|
|
|
data: expectedTags,
|
2018-08-22 23:38:05 +03:00
|
|
|
},
|
2020-08-29 20:51:14 +03:00
|
|
|
});
|
2022-11-15 22:31:35 +03:00
|
|
|
const { listTags } = buildApiClient();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2023-03-18 18:32:04 +03:00
|
|
|
const result = await listTags();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2020-05-10 12:20:40 +03:00
|
|
|
expect({ tags: expectedTags }).toEqual(result);
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining('/tags'),
|
|
|
|
expect.objectContaining({ method: 'GET' }),
|
|
|
|
);
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
2023-03-18 18:32:04 +03:00
|
|
|
});
|
2023-03-18 18:26:28 +03:00
|
|
|
|
2023-03-18 18:32:04 +03:00
|
|
|
describe('tagsStats', () => {
|
2023-03-18 18:26:28 +03:00
|
|
|
it('can use /tags/stats endpoint', async () => {
|
|
|
|
const expectedTags = ['foo', 'bar'];
|
|
|
|
const expectedStats = expectedTags.map((tag) => ({ tag, shortUrlsCount: 10, visitsCount: 10 }));
|
|
|
|
|
|
|
|
fetchJson.mockResolvedValue({
|
|
|
|
tags: {
|
|
|
|
data: expectedStats,
|
|
|
|
},
|
|
|
|
});
|
2023-03-18 18:32:04 +03:00
|
|
|
const { tagsStats } = buildApiClient();
|
2023-03-18 18:26:28 +03:00
|
|
|
|
2023-03-18 18:32:04 +03:00
|
|
|
const result = await tagsStats();
|
2023-03-18 18:26:28 +03:00
|
|
|
|
|
|
|
expect({ tags: expectedTags, stats: expectedStats }).toEqual(result);
|
|
|
|
expect(fetchJson).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining('/tags/stats'),
|
|
|
|
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'];
|
2022-11-15 22:31:35 +03:00
|
|
|
const { deleteTags } = buildApiClient();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2019-01-10 21:17:15 +03:00
|
|
|
await deleteTags(tags);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2022-11-20 14:51:07 +03:00
|
|
|
expect(fetchEmpty).toHaveBeenCalledWith(
|
2022-11-15 01:06:06 +03:00
|
|
|
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', () => {
|
2019-01-10 21:17:15 +03:00
|
|
|
it('properly edits provided tag', async () => {
|
2018-08-22 23:38:05 +03:00
|
|
|
const oldName = 'foo';
|
|
|
|
const newName = 'bar';
|
2022-11-15 22:31:35 +03:00
|
|
|
const { editTag } = buildApiClient();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2019-01-10 21:17:15 +03:00
|
|
|
await editTag(oldName, newName);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2022-11-20 14:51:07 +03:00
|
|
|
expect(fetchEmpty).toHaveBeenCalledWith(expect.stringContaining('/tags'), expect.objectContaining({
|
2019-04-19 13:41:59 +03:00
|
|
|
method: 'PUT',
|
2022-11-15 01:06:06 +03:00
|
|
|
body: JSON.stringify({ oldName, newName }),
|
2019-04-19 13:41:59 +03:00
|
|
|
}));
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
|
|
|
});
|
2019-01-10 21:17:15 +03:00
|
|
|
|
|
|
|
describe('deleteShortUrl', () => {
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each(shortCodesWithDomainCombinations)('properly deletes provided short URL', async (shortCode, domain) => {
|
2022-11-15 22:31:35 +03:00
|
|
|
const { deleteShortUrl } = buildApiClient();
|
2022-11-15 01:06:06 +03:00
|
|
|
const expectedQuery = domain ? `?domain=${domain}` : '';
|
2019-01-10 21:17:15 +03:00
|
|
|
|
2020-02-08 11:07:55 +03:00
|
|
|
await deleteShortUrl(shortCode, domain);
|
2019-01-10 21:17:15 +03:00
|
|
|
|
2022-11-20 14:51:07 +03:00
|
|
|
expect(fetchEmpty).toHaveBeenCalledWith(
|
2022-11-15 01:06:06 +03:00
|
|
|
expect.stringContaining(`/short-urls/${shortCode}${expectedQuery}`),
|
|
|
|
expect.objectContaining({ method: 'DELETE' }),
|
|
|
|
);
|
2019-01-10 21:17:15 +03:00
|
|
|
});
|
|
|
|
});
|
2019-10-05 11:40:32 +03:00
|
|
|
|
|
|
|
describe('health', () => {
|
|
|
|
it('returns health data', async () => {
|
|
|
|
const expectedData = {
|
|
|
|
status: 'pass',
|
|
|
|
version: '1.19.0',
|
|
|
|
};
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(expectedData);
|
|
|
|
const { health } = buildApiClient();
|
2019-10-05 11:40:32 +03:00
|
|
|
|
|
|
|
const result = await health();
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2019-10-05 11:40:32 +03:00
|
|
|
expect(result).toEqual(expectedData);
|
|
|
|
});
|
|
|
|
});
|
2020-04-17 16:51:18 +03:00
|
|
|
|
|
|
|
describe('mercureInfo', () => {
|
|
|
|
it('returns mercure info', async () => {
|
|
|
|
const expectedData = {
|
|
|
|
token: 'abc.123.def',
|
|
|
|
mercureHubUrl: 'http://example.com/.well-known/mercure',
|
|
|
|
};
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(expectedData);
|
|
|
|
const { mercureInfo } = buildApiClient();
|
2020-04-17 16:51:18 +03:00
|
|
|
|
|
|
|
const result = await mercureInfo();
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2020-04-17 16:51:18 +03:00
|
|
|
expect(result).toEqual(expectedData);
|
|
|
|
});
|
|
|
|
});
|
2020-11-28 13:45:04 +03:00
|
|
|
|
|
|
|
describe('listDomains', () => {
|
|
|
|
it('returns domains', async () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const expectedData = { data: [fromPartial<ShlinkDomain>({}), fromPartial<ShlinkDomain>({})] };
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({ domains: expectedData });
|
|
|
|
const { listDomains } = buildApiClient();
|
2020-11-28 13:45:04 +03:00
|
|
|
|
|
|
|
const result = await listDomains();
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2020-11-28 13:45:04 +03:00
|
|
|
expect(result).toEqual(expectedData);
|
|
|
|
});
|
|
|
|
});
|
2020-12-07 14:12:39 +03:00
|
|
|
|
|
|
|
describe('getVisitsOverview', () => {
|
|
|
|
it('returns visits overview', async () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const expectedData = fromPartial<ShlinkVisitsOverview>({});
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue({ visits: expectedData });
|
|
|
|
const { getVisitsOverview } = buildApiClient();
|
2020-12-07 14:12:39 +03:00
|
|
|
|
|
|
|
const result = await getVisitsOverview();
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2020-12-07 14:12:39 +03:00
|
|
|
expect(result).toEqual(expectedData);
|
|
|
|
});
|
|
|
|
});
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
describe('getOrphanVisits', () => {
|
|
|
|
it('returns orphan visits', async () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
fetchJson.mockResolvedValue({ visits: fromPartial<ShlinkVisits>({ data: [] }) });
|
2022-11-15 22:31:35 +03:00
|
|
|
const { getOrphanVisits } = buildApiClient();
|
2021-02-27 22:03:51 +03:00
|
|
|
|
|
|
|
const result = await getOrphanVisits();
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2022-11-15 01:06:06 +03:00
|
|
|
expect(result).toEqual({ data: [] });
|
2021-02-27 22:03:51 +03:00
|
|
|
});
|
|
|
|
});
|
2021-08-22 10:11:14 +03:00
|
|
|
|
2022-02-05 15:37:49 +03:00
|
|
|
describe('getNonOrphanVisits', () => {
|
|
|
|
it('returns non-orphan visits', async () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
fetchJson.mockResolvedValue({ visits: fromPartial<ShlinkVisits>({ data: [] }) });
|
2022-11-15 22:31:35 +03:00
|
|
|
const { getNonOrphanVisits } = buildApiClient();
|
2022-02-05 15:37:49 +03:00
|
|
|
|
|
|
|
const result = await getNonOrphanVisits();
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2022-11-15 01:06:06 +03:00
|
|
|
expect(result).toEqual({ data: [] });
|
2022-02-05 15:37:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-22 10:11:14 +03:00
|
|
|
describe('editDomainRedirects', () => {
|
|
|
|
it('returns the redirects', async () => {
|
|
|
|
const resp = { baseUrlRedirect: null, regular404Redirect: 'foo', invalidShortUrlRedirect: 'bar' };
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(resp);
|
|
|
|
const { editDomainRedirects } = buildApiClient();
|
2021-08-22 10:11:14 +03:00
|
|
|
|
|
|
|
const result = await editDomainRedirects({ domain: 'foo' });
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalled();
|
2021-08-22 10:11:14 +03:00
|
|
|
expect(result).toEqual(resp);
|
|
|
|
});
|
2022-10-12 11:19:54 +03:00
|
|
|
|
2022-11-15 14:19:21 +03:00
|
|
|
it.each([
|
|
|
|
['NOT_FOUND'],
|
|
|
|
[ErrorTypeV2.NOT_FOUND],
|
|
|
|
[ErrorTypeV3.NOT_FOUND],
|
|
|
|
])('retries request if API version is not supported', async (type) => {
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson
|
|
|
|
.mockRejectedValueOnce({ type, detail: 'detail', title: 'title', status: 404 })
|
|
|
|
.mockResolvedValue({});
|
|
|
|
const { editDomainRedirects } = buildApiClient();
|
2022-10-12 11:19:54 +03:00
|
|
|
|
|
|
|
await editDomainRedirects({ domain: 'foo' });
|
|
|
|
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledTimes(2);
|
|
|
|
expect(fetchJson).toHaveBeenNthCalledWith(1, expect.stringContaining('/v3/'), expect.anything());
|
|
|
|
expect(fetchJson).toHaveBeenNthCalledWith(2, expect.stringContaining('/v2/'), expect.anything());
|
2022-10-12 11:19:54 +03:00
|
|
|
});
|
2021-08-22 10:11:14 +03:00
|
|
|
});
|
2018-08-04 12:07:28 +03:00
|
|
|
});
|