shlink-web-client/test/utils/services/ShlinkApiClient.test.js

169 lines
4.8 KiB
JavaScript
Raw Normal View History

import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
2018-08-04 12:07:28 +03:00
describe('ShlinkApiClient', () => {
const createAxiosMock = (data) => () => Promise.resolve(data);
const createApiClient = (data) => new ShlinkApiClient(createAxiosMock(data));
2018-08-04 12:07:28 +03:00
describe('listShortUrls', () => {
it('properly returns short URLs list', async () => {
const expectedList = [ 'foo', 'bar' ];
2018-08-04 12:07:28 +03:00
const { listShortUrls } = createApiClient({
2018-08-22 23:38:05 +03:00
data: {
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);
});
});
2018-08-22 23:38:05 +03:00
describe('createShortUrl', () => {
const shortUrl = {
bar: 'foo',
};
it('returns create short URL', async () => {
const { createShortUrl } = createApiClient({ data: shortUrl });
const result = await createShortUrl({});
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 }));
const { createShortUrl } = new ShlinkApiClient(axiosSpy);
2018-08-22 23:38:05 +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 () => {
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,
},
},
}));
const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy);
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);
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', () => {
it('properly returns short URL', async () => {
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,
}));
const { getShortUrl } = new ShlinkApiClient(axiosSpy);
2018-08-22 23:38:05 +03:00
const result = await getShortUrl('abc123');
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({
url: '/short-urls/abc123',
method: 'GET',
}));
2018-08-22 23:38:05 +03:00
});
});
describe('updateShortUrlTags', () => {
it('properly updates short URL tags', async () => {
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 },
}));
const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy);
2018-08-22 23:38:05 +03:00
const result = await updateShortUrlTags('abc123', 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({
url: '/short-urls/abc123/tags',
method: 'PUT',
}));
2018-08-22 23:38:05 +03:00
});
});
describe('listTags', () => {
it('properly returns list of tags', async () => {
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: { data: expectedTags },
2018-08-22 23:38:05 +03:00
},
}));
const { listTags } = new ShlinkApiClient(axiosSpy);
2018-08-22 23:38:05 +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 () => {
const tags = [ 'foo', 'bar' ];
2019-04-19 13:41:59 +03:00
const axiosSpy = jest.fn(createAxiosMock({}));
const { deleteTags } = new ShlinkApiClient(axiosSpy);
2018-08-22 23:38:05 +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', () => {
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({}));
const { editTag } = new ShlinkApiClient(axiosSpy);
2018-08-22 23:38:05 +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
});
});
describe('deleteShortUrl', () => {
it('properly deletes provided short URL', async () => {
2019-04-19 13:41:59 +03:00
const axiosSpy = jest.fn(createAxiosMock({}));
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy);
await deleteShortUrl('abc123');
2019-04-19 13:41:59 +03:00
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
url: '/short-urls/abc123',
method: 'DELETE',
}));
});
});
2018-08-04 12:07:28 +03:00
});