mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-02-26 19:56:41 +00:00
Moved common and utils services to their own service providers
This commit is contained in:
48
test/utils/services/ColorGenerator.test.js
Normal file
48
test/utils/services/ColorGenerator.test.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import * as sinon from 'sinon';
|
||||
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
|
||||
|
||||
describe('ColorGenerator', () => {
|
||||
let colorGenerator;
|
||||
const storageMock = {
|
||||
set: sinon.fake(),
|
||||
get: sinon.fake.returns(undefined),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
storageMock.set.resetHistory();
|
||||
storageMock.get.resetHistory();
|
||||
|
||||
colorGenerator = new ColorGenerator(storageMock);
|
||||
});
|
||||
|
||||
it('sets a color in the storage and makes it available after that', () => {
|
||||
const color = '#ff0000';
|
||||
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey('foo')).toEqual(color);
|
||||
expect(storageMock.set.callCount).toEqual(1);
|
||||
expect(storageMock.get.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('generates a random color when none is available for requested key', () => {
|
||||
expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/));
|
||||
expect(storageMock.set.callCount).toEqual(1);
|
||||
expect(storageMock.get.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('trims and lower cases keys before trying to match', () => {
|
||||
const color = '#ff0000';
|
||||
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey(' foo')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('foO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FoO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO ')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color);
|
||||
expect(storageMock.set.callCount).toEqual(1);
|
||||
expect(storageMock.get.callCount).toEqual(1);
|
||||
});
|
||||
});
|
||||
167
test/utils/services/ShlinkApiClient.test.js
Normal file
167
test/utils/services/ShlinkApiClient.test.js
Normal file
@@ -0,0 +1,167 @@
|
||||
import sinon from 'sinon';
|
||||
import { head, last } from 'ramda';
|
||||
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
||||
|
||||
describe('ShlinkApiClient', () => {
|
||||
const createAxiosMock = (extraData) => () =>
|
||||
Promise.resolve({
|
||||
headers: { authorization: 'Bearer abc123' },
|
||||
data: { token: 'abc123' },
|
||||
...extraData,
|
||||
});
|
||||
const createApiClient = (extraData) =>
|
||||
new ShlinkApiClient(createAxiosMock(extraData));
|
||||
|
||||
describe('listShortUrls', () => {
|
||||
it('properly returns short URLs list', async () => {
|
||||
const expectedList = [ 'foo', 'bar' ];
|
||||
|
||||
const apiClient = createApiClient({
|
||||
data: {
|
||||
shortUrls: expectedList,
|
||||
},
|
||||
});
|
||||
|
||||
const actualList = await apiClient.listShortUrls();
|
||||
|
||||
expect(expectedList).toEqual(actualList);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createShortUrl', () => {
|
||||
const shortUrl = {
|
||||
bar: 'foo',
|
||||
};
|
||||
|
||||
it('returns create short URL', async () => {
|
||||
const apiClient = createApiClient({ data: shortUrl });
|
||||
const result = await apiClient.createShortUrl({});
|
||||
|
||||
expect(result).toEqual(shortUrl);
|
||||
});
|
||||
|
||||
it('removes all empty options', async () => {
|
||||
const axiosSpy = sinon.spy(createAxiosMock({ data: shortUrl }));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await apiClient.createShortUrl(
|
||||
{ foo: 'bar', empty: undefined, anotherEmpty: null }
|
||||
);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.data).toEqual({ foo: 'bar' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('getShortUrlVisits', () => {
|
||||
it('properly returns short URL visits', async () => {
|
||||
const expectedVisits = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
data: {
|
||||
visits: {
|
||||
data: expectedVisits,
|
||||
},
|
||||
},
|
||||
}));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const actualVisits = await apiClient.getShortUrlVisits('abc123', {});
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedVisits).toEqual(actualVisits);
|
||||
expect(axiosArgs.url).toContain('/short-codes/abc123/visits');
|
||||
expect(axiosArgs.method).toEqual('GET');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getShortUrl', () => {
|
||||
it('properly returns short URL', async () => {
|
||||
const expectedShortUrl = { foo: 'bar' };
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
data: expectedShortUrl,
|
||||
}));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const result = await apiClient.getShortUrl('abc123');
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedShortUrl).toEqual(result);
|
||||
expect(axiosArgs.url).toContain('/short-codes/abc123');
|
||||
expect(axiosArgs.method).toEqual('GET');
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateShortUrlTags', () => {
|
||||
it('properly updates short URL tags', async () => {
|
||||
const expectedTags = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
data: { tags: expectedTags },
|
||||
}));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const result = await apiClient.updateShortUrlTags('abc123', expectedTags);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedTags).toEqual(result);
|
||||
expect(axiosArgs.url).toContain('/short-codes/abc123/tags');
|
||||
expect(axiosArgs.method).toEqual('PUT');
|
||||
});
|
||||
});
|
||||
|
||||
describe('listTags', () => {
|
||||
it('properly returns list of tags', async () => {
|
||||
const expectedTags = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
data: {
|
||||
tags: { data: expectedTags },
|
||||
},
|
||||
}));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const result = await apiClient.listTags();
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedTags).toEqual(result);
|
||||
expect(axiosArgs.url).toContain('/tags');
|
||||
expect(axiosArgs.method).toEqual('GET');
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteTags', () => {
|
||||
it('properly deletes provided tags', async () => {
|
||||
const tags = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({}));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await apiClient.deleteTags(tags);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.url).toContain('/tags');
|
||||
expect(axiosArgs.method).toEqual('DELETE');
|
||||
expect(axiosArgs.params).toEqual({ tags });
|
||||
});
|
||||
});
|
||||
|
||||
describe('editTag', () => {
|
||||
it('properly deletes provided tags', async () => {
|
||||
const oldName = 'foo';
|
||||
const newName = 'bar';
|
||||
const axiosSpy = sinon.spy(createAxiosMock({}));
|
||||
const apiClient = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await apiClient.editTag(oldName, newName);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.url).toContain('/tags');
|
||||
expect(axiosArgs.method).toEqual('PUT');
|
||||
expect(axiosArgs.data).toEqual({ oldName, newName });
|
||||
});
|
||||
});
|
||||
});
|
||||
26
test/utils/services/ShlinkApiClientBuilder.test.js
Normal file
26
test/utils/services/ShlinkApiClientBuilder.test.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import buildShlinkApiClient from '../../../src/utils/services/ShlinkApiClientBuilder';
|
||||
|
||||
describe('ShlinkApiClientBuilder', () => {
|
||||
const builder = buildShlinkApiClient({});
|
||||
|
||||
it('creates new instances when provided params are different', () => {
|
||||
const firstApiClient = builder({ url: 'foo', apiKey: 'bar' });
|
||||
const secondApiClient = builder({ url: 'bar', apiKey: 'bar' });
|
||||
const thirdApiClient = builder({ 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', () => {
|
||||
const params = { url: 'foo', apiKey: 'bar' };
|
||||
const firstApiClient = builder(params);
|
||||
const secondApiClient = builder(params);
|
||||
const thirdApiClient = builder(params);
|
||||
|
||||
expect(firstApiClient).toBe(secondApiClient);
|
||||
expect(firstApiClient).toBe(thirdApiClient);
|
||||
expect(secondApiClient).toBe(thirdApiClient);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user