Moved Shlink API services to api module

This commit is contained in:
Alejandro Celaya
2020-12-22 09:55:39 +01:00
parent 8c6eaf2f1d
commit b2abfd543e
35 changed files with 43 additions and 40 deletions

View File

@@ -0,0 +1,286 @@
import { AxiosInstance, AxiosRequestConfig } from 'axios';
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
import { OptionalString } from '../../../src/utils/utils';
import { Mock } from 'ts-mockery';
import { ShlinkDomain, ShlinkVisitsOverview } from '../../../src/api/types';
describe('ShlinkApiClient', () => {
const createAxios = (data: AxiosRequestConfig) => (async () => Promise.resolve(data)) as unknown as AxiosInstance;
const createAxiosMock = (data: AxiosRequestConfig = {}) => jest.fn(createAxios(data)) as unknown as AxiosInstance;
const createApiClient = (data: AxiosRequestConfig) => new ShlinkApiClient(createAxios(data), '', '');
const shortCodesWithDomainCombinations: [ string, OptionalString ][] = [
[ 'abc123', null ],
[ 'abc123', undefined ],
[ 'abc123', 'example.com' ],
];
describe('listShortUrls', () => {
it('properly returns short URLs list', async () => {
const expectedList = [ 'foo', 'bar' ];
const { listShortUrls } = createApiClient({
data: {
shortUrls: expectedList,
},
});
const actualList = await listShortUrls();
expect(expectedList).toEqual(actualList);
});
});
describe('createShortUrl', () => {
const shortUrl = {
bar: 'foo',
};
it('returns create short URL', async () => {
const { createShortUrl } = createApiClient({ data: shortUrl });
const result = await createShortUrl({ longUrl: '' });
expect(result).toEqual(shortUrl);
});
it('removes all empty options', async () => {
const axiosSpy = createAxiosMock({ data: shortUrl });
const { createShortUrl } = new ShlinkApiClient(axiosSpy, '', '');
await createShortUrl(
// @ts-expect-error
{ longUrl: 'bar', customSlug: undefined, maxVisits: null },
);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ data: { longUrl: 'bar' } }));
});
});
describe('getShortUrlVisits', () => {
it('properly returns short URL visits', async () => {
const expectedVisits = [ 'foo', 'bar' ];
const axiosSpy = createAxiosMock({
data: {
visits: {
data: expectedVisits,
},
},
});
const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy, '', '');
const actualVisits = await getShortUrlVisits('abc123', {});
expect({ data: expectedVisits }).toEqual(actualVisits);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: '/short-urls/abc123/visits',
method: 'GET',
}));
});
});
describe('getTagVisits', () => {
it('properly returns tag visits', async () => {
const expectedVisits = [ 'foo', 'bar' ];
const axiosSpy = createAxiosMock({
data: {
visits: {
data: expectedVisits,
},
},
});
const { getTagVisits } = new ShlinkApiClient(axiosSpy, '', '');
const actualVisits = await getTagVisits('foo', {});
expect({ data: expectedVisits }).toEqual(actualVisits);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: '/tags/foo/visits',
method: 'GET',
}));
});
});
describe('getShortUrl', () => {
it.each(shortCodesWithDomainCombinations)('properly returns short URL', async (shortCode, domain) => {
const expectedShortUrl = { foo: 'bar' };
const axiosSpy = createAxiosMock({
data: expectedShortUrl,
});
const { getShortUrl } = new ShlinkApiClient(axiosSpy, '', '');
const result = await getShortUrl(shortCode, domain);
expect(expectedShortUrl).toEqual(result);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: `/short-urls/${shortCode}`,
method: 'GET',
params: domain ? { domain } : {},
}));
});
});
describe('updateShortUrlTags', () => {
it.each(shortCodesWithDomainCombinations)('properly updates short URL tags', async (shortCode, domain) => {
const expectedTags = [ 'foo', 'bar' ];
const axiosSpy = createAxiosMock({
data: { tags: expectedTags },
});
const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy, '', '');
const result = await updateShortUrlTags(shortCode, domain, expectedTags);
expect(expectedTags).toEqual(result);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: `/short-urls/${shortCode}/tags`,
method: 'PUT',
params: domain ? { domain } : {},
}));
});
});
describe('updateShortUrlMeta', () => {
it.each(shortCodesWithDomainCombinations)('properly updates short URL meta', async (shortCode, domain) => {
const expectedMeta = {
maxVisits: 50,
validSince: '2025-01-01T10:00:00+01:00',
};
const axiosSpy = createAxiosMock();
const { updateShortUrlMeta } = new ShlinkApiClient(axiosSpy, '', '');
const result = await updateShortUrlMeta(shortCode, domain, expectedMeta);
expect(expectedMeta).toEqual(result);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: `/short-urls/${shortCode}`,
method: 'PATCH',
params: domain ? { domain } : {},
}));
});
});
describe('listTags', () => {
it('properly returns list of tags', async () => {
const expectedTags = [ 'foo', 'bar' ];
const axiosSpy = createAxiosMock({
data: {
tags: { data: expectedTags },
},
});
const { listTags } = new ShlinkApiClient(axiosSpy, '', '');
const result = await listTags();
expect({ tags: expectedTags }).toEqual(result);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ url: '/tags', method: 'GET' }));
});
});
describe('deleteTags', () => {
it('properly deletes provided tags', async () => {
const tags = [ 'foo', 'bar' ];
const axiosSpy = createAxiosMock();
const { deleteTags } = new ShlinkApiClient(axiosSpy, '', '');
await deleteTags(tags);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: '/tags',
method: 'DELETE',
params: { tags },
}));
});
});
describe('editTag', () => {
it('properly edits provided tag', async () => {
const oldName = 'foo';
const newName = 'bar';
const axiosSpy = createAxiosMock();
const { editTag } = new ShlinkApiClient(axiosSpy, '', '');
await editTag(oldName, newName);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: '/tags',
method: 'PUT',
data: { oldName, newName },
}));
});
});
describe('deleteShortUrl', () => {
it.each(shortCodesWithDomainCombinations)('properly deletes provided short URL', async (shortCode, domain) => {
const axiosSpy = createAxiosMock({});
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy, '', '');
await deleteShortUrl(shortCode, domain);
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: `/short-urls/${shortCode}`,
method: 'DELETE',
params: domain ? { domain } : {},
}));
});
});
describe('health', () => {
it('returns health data', async () => {
const expectedData = {
status: 'pass',
version: '1.19.0',
};
const axiosSpy = createAxiosMock({ data: expectedData });
const { health } = new ShlinkApiClient(axiosSpy, '', '');
const result = await health();
expect(axiosSpy).toHaveBeenCalled();
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 axiosSpy = createAxiosMock({ data: expectedData });
const { mercureInfo } = new ShlinkApiClient(axiosSpy, '', '');
const result = await mercureInfo();
expect(axiosSpy).toHaveBeenCalled();
expect(result).toEqual(expectedData);
});
});
describe('listDomains', () => {
it('returns domains', async () => {
const expectedData = [Mock.all<ShlinkDomain>(), Mock.all<ShlinkDomain>()];
const resp = {
domains: { data: expectedData },
};
const axiosSpy = createAxiosMock({ data: resp });
const { listDomains } = new ShlinkApiClient(axiosSpy, '', '');
const result = await listDomains();
expect(axiosSpy).toHaveBeenCalled();
expect(result).toEqual(expectedData);
});
});
describe('getVisitsOverview', () => {
it('returns visits overview', async () => {
const expectedData = Mock.all<ShlinkVisitsOverview>();
const resp = { visits: expectedData };
const axiosSpy = createAxiosMock({ data: resp });
const { getVisitsOverview } = new ShlinkApiClient(axiosSpy, '', '');
const result = await getVisitsOverview();
expect(axiosSpy).toHaveBeenCalled();
expect(result).toEqual(expectedData);
});
});
});

View File

@@ -0,0 +1,52 @@
import { Mock } from 'ts-mockery';
import { AxiosInstance } from 'axios';
import buildShlinkApiClient from '../../../src/api/services/ShlinkApiClientBuilder';
import { ReachableServer, SelectedServer } from '../../../src/servers/data';
import { ShlinkState } from '../../../src/container/types';
describe('ShlinkApiClientBuilder', () => {
const axiosMock = Mock.all<AxiosInstance>();
const server = (data: Partial<ReachableServer>) => Mock.of<ReachableServer>(data);
const createBuilder = () => {
const builder = buildShlinkApiClient(axiosMock);
return (selectedServer: SelectedServer) => builder(() => Mock.of<ShlinkState>({ selectedServer }));
};
it('creates new instances when provided params are different', async () => {
const builder = createBuilder();
const [ firstApiClient, secondApiClient, thirdApiClient ] = await Promise.all([
builder(server({ url: 'foo', apiKey: 'bar' })),
builder(server({ url: 'bar', apiKey: 'bar' })),
builder(server({ url: 'bar', apiKey: 'foo' })),
]);
expect(firstApiClient).not.toBe(secondApiClient);
expect(firstApiClient).not.toBe(thirdApiClient);
expect(secondApiClient).not.toBe(thirdApiClient);
});
it('returns existing instances when provided params are the same', async () => {
const builder = createBuilder();
const selectedServer = server({ url: 'foo', apiKey: 'bar' });
const [ firstApiClient, secondApiClient, thirdApiClient ] = await Promise.all([
builder(selectedServer),
builder(selectedServer),
builder(selectedServer),
]);
expect(firstApiClient).toBe(secondApiClient);
expect(firstApiClient).toBe(thirdApiClient);
expect(secondApiClient).toBe(thirdApiClient);
});
it('does not fetch from state when provided param is already selected server', () => {
const url = 'url';
const apiKey = 'apiKey';
const apiClient = buildShlinkApiClient(axiosMock)(server({ url, apiKey }));
expect(apiClient['baseUrl']).toEqual(url); // eslint-disable-line dot-notation
expect(apiClient['apiKey']).toEqual(apiKey); // eslint-disable-line dot-notation
});
});