2018-12-18 22:19:22 +03:00
|
|
|
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
2018-08-04 12:07:28 +03:00
|
|
|
|
|
|
|
describe('ShlinkApiClient', () => {
|
2019-01-10 21:17:15 +03:00
|
|
|
const createAxiosMock = (data) => () => Promise.resolve(data);
|
|
|
|
const createApiClient = (data) => new ShlinkApiClient(createAxiosMock(data));
|
2020-02-08 11:07:55 +03:00
|
|
|
const shortCodesWithDomainCombinations = [
|
|
|
|
[ 'abc123', null ],
|
|
|
|
[ 'abc123', undefined ],
|
|
|
|
[ 'abc123', 'example.com' ],
|
|
|
|
];
|
2018-08-04 12:07:28 +03:00
|
|
|
|
|
|
|
describe('listShortUrls', () => {
|
|
|
|
it('properly returns short URLs list', async () => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const expectedList = [ 'foo', 'bar' ];
|
2018-08-04 12:07:28 +03:00
|
|
|
|
2019-01-10 21:17:15 +03:00
|
|
|
const { listShortUrls } = createApiClient({
|
2018-08-22 23:38:05 +03:00
|
|
|
data: {
|
|
|
|
shortUrls: expectedList,
|
|
|
|
},
|
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);
|
|
|
|
});
|
|
|
|
});
|
2018-08-22 23:38:05 +03:00
|
|
|
|
|
|
|
describe('createShortUrl', () => {
|
|
|
|
const shortUrl = {
|
|
|
|
bar: 'foo',
|
|
|
|
};
|
|
|
|
|
|
|
|
it('returns create short URL', async () => {
|
2019-01-10 21:17:15 +03:00
|
|
|
const { createShortUrl } = createApiClient({ data: shortUrl });
|
|
|
|
const result = await createShortUrl({});
|
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 () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({ data: shortUrl }));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { createShortUrl } = new ShlinkApiClient(axiosSpy);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2019-01-10 21:17:15 +03:00
|
|
|
await createShortUrl(
|
2018-08-22 23:38:05 +03:00
|
|
|
{ foo: 'bar', empty: undefined, anotherEmpty: null }
|
|
|
|
);
|
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ data: { foo: 'bar' } }));
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getShortUrlVisits', () => {
|
|
|
|
it('properly returns short URL visits', async () => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const expectedVisits = [ 'foo', 'bar' ];
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({
|
2018-08-22 23:38:05 +03:00
|
|
|
data: {
|
|
|
|
visits: {
|
|
|
|
data: expectedVisits,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy);
|
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);
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
|
|
url: '/short-urls/abc123/visits',
|
|
|
|
method: 'GET',
|
|
|
|
}));
|
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' };
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({
|
2018-08-22 23:38:05 +03:00
|
|
|
data: expectedShortUrl,
|
|
|
|
}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { getShortUrl } = new ShlinkApiClient(axiosSpy);
|
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);
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
2020-02-08 11:07:55 +03:00
|
|
|
url: `/short-urls/${shortCode}`,
|
2019-04-19 13:41:59 +03:00
|
|
|
method: 'GET',
|
2020-02-08 11:07:55 +03:00
|
|
|
params: domain ? { domain } : {},
|
2019-04-19 13:41:59 +03:00
|
|
|
}));
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateShortUrlTags', () => {
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each(shortCodesWithDomainCombinations)('properly updates short URL tags', async (shortCode, domain) => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const expectedTags = [ 'foo', 'bar' ];
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({
|
2018-08-22 23:38:05 +03:00
|
|
|
data: { tags: expectedTags },
|
|
|
|
}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2020-02-08 11:07:55 +03:00
|
|
|
const result = await updateShortUrlTags(shortCode, domain, expectedTags);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
|
|
|
expect(expectedTags).toEqual(result);
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
2020-02-08 11:07:55 +03:00
|
|
|
url: `/short-urls/${shortCode}/tags`,
|
2019-04-19 13:41:59 +03:00
|
|
|
method: 'PUT',
|
2020-02-08 11:07:55 +03:00
|
|
|
params: domain ? { domain } : {},
|
2019-04-19 13:41:59 +03:00
|
|
|
}));
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-19 15:20:46 +03:00
|
|
|
describe('updateShortUrlMeta', () => {
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each(shortCodesWithDomainCombinations)('properly updates short URL meta', async (shortCode, domain) => {
|
2020-01-19 15:20:46 +03:00
|
|
|
const expectedMeta = {
|
|
|
|
maxVisits: 50,
|
|
|
|
validSince: '2025-01-01T10:00:00+01:00',
|
|
|
|
};
|
|
|
|
const axiosSpy = jest.fn(createAxiosMock());
|
|
|
|
const { updateShortUrlMeta } = new ShlinkApiClient(axiosSpy);
|
|
|
|
|
2020-02-08 11:07:55 +03:00
|
|
|
const result = await updateShortUrlMeta(shortCode, domain, expectedMeta);
|
2020-01-19 15:20:46 +03:00
|
|
|
|
|
|
|
expect(expectedMeta).toEqual(result);
|
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
2020-02-08 11:07:55 +03:00
|
|
|
url: `/short-urls/${shortCode}`,
|
2020-01-19 15:20:46 +03:00
|
|
|
method: 'PATCH',
|
2020-02-08 11:07:55 +03:00
|
|
|
params: domain ? { domain } : {},
|
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 () => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const expectedTags = [ 'foo', 'bar' ];
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({
|
2018-08-22 23:38:05 +03:00
|
|
|
data: {
|
2018-08-26 00:39:27 +03:00
|
|
|
tags: { data: expectedTags },
|
2018-08-22 23:38:05 +03:00
|
|
|
},
|
|
|
|
}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { listTags } = new ShlinkApiClient(axiosSpy);
|
2018-08-22 23:38:05 +03:00
|
|
|
|
2019-01-10 21:17:15 +03:00
|
|
|
const result = await listTags();
|
2018-08-22 23:38:05 +03:00
|
|
|
|
|
|
|
expect(expectedTags).toEqual(result);
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ url: '/tags', method: 'GET' }));
|
2018-08-22 23:38:05 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deleteTags', () => {
|
|
|
|
it('properly deletes provided tags', async () => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const tags = [ 'foo', 'bar' ];
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { deleteTags } = new ShlinkApiClient(axiosSpy);
|
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
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
|
|
url: '/tags',
|
|
|
|
method: 'DELETE',
|
|
|
|
params: { tags },
|
|
|
|
}));
|
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';
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { editTag } = new ShlinkApiClient(axiosSpy);
|
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
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
|
|
url: '/tags',
|
|
|
|
method: 'PUT',
|
|
|
|
data: { oldName, newName },
|
|
|
|
}));
|
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) => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const axiosSpy = jest.fn(createAxiosMock({}));
|
2019-01-10 21:17:15 +03:00
|
|
|
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy);
|
|
|
|
|
2020-02-08 11:07:55 +03:00
|
|
|
await deleteShortUrl(shortCode, domain);
|
2019-01-10 21:17:15 +03:00
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
2020-02-08 11:07:55 +03:00
|
|
|
url: `/short-urls/${shortCode}`,
|
2019-04-19 13:41:59 +03:00
|
|
|
method: 'DELETE',
|
2020-02-08 11:07:55 +03:00
|
|
|
params: domain ? { domain } : {},
|
2019-04-19 13:41:59 +03:00
|
|
|
}));
|
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',
|
|
|
|
};
|
|
|
|
const axiosSpy = jest.fn(createAxiosMock({ data: expectedData }));
|
|
|
|
const { health } = new ShlinkApiClient(axiosSpy);
|
|
|
|
|
|
|
|
const result = await health();
|
|
|
|
|
|
|
|
expect(axiosSpy).toHaveBeenCalled();
|
|
|
|
expect(result).toEqual(expectedData);
|
|
|
|
});
|
|
|
|
});
|
2018-08-04 12:07:28 +03:00
|
|
|
});
|