Added tests for new logics

This commit is contained in:
Alejandro Celaya 2019-10-05 10:40:32 +02:00
parent 4120d09220
commit 6d996baf5d
4 changed files with 72 additions and 2 deletions

View file

@ -2,12 +2,13 @@ import qs from 'qs';
import { isEmpty, isNil, reject } from 'ramda'; import { isEmpty, isNil, reject } from 'ramda';
const API_VERSION = '1'; const API_VERSION = '1';
const buildRestUrl = (url) => url ? `${url}/rest/v${API_VERSION}` : '';
export const buildShlinkBaseUrl = (url) => url ? `${url}/rest/v${API_VERSION}` : '';
export default class ShlinkApiClient { export default class ShlinkApiClient {
constructor(axios, baseUrl, apiKey) { constructor(axios, baseUrl, apiKey) {
this.axios = axios; this.axios = axios;
this._baseUrl = buildRestUrl(baseUrl); this._baseUrl = buildShlinkBaseUrl(baseUrl);
this._apiKey = apiKey || ''; this._apiKey = apiKey || '';
} }

View file

@ -0,0 +1,43 @@
import React from 'react';
import { mount } from 'enzyme';
import ForVersion from '../../src/utils/ForVersion';
describe('<ForVersion />', () => {
let wrapped;
const renderComponent = (minVersion, currentServerVersion) => {
wrapped = mount(
<ForVersion minVersion={minVersion} currentServerVersion={currentServerVersion}>
<span>Hello</span>
</ForVersion>
);
return wrapped;
};
afterEach(() => wrapped && wrapped.unmount());
it('does not render children when current version is empty', () => {
const wrapped = renderComponent('1', '');
expect(wrapped.html()).toBeNull();
});
it('does not render children when current version is lower than min version', () => {
const wrapped = renderComponent('2.0.0', '1.8.3');
expect(wrapped.html()).toBeNull();
});
it('renders children when current version is equal min version', () => {
const wrapped = renderComponent('2.0.0', '2.0.0');
expect(wrapped.html()).toContain('<span>Hello</span>');
});
it('renders children when current version is higher than min version', () => {
const wrapped = renderComponent('2.0.0', '2.1.0');
expect(wrapped.html()).toContain('<span>Hello</span>');
});
});

View file

@ -165,4 +165,20 @@ describe('ShlinkApiClient', () => {
})); }));
}); });
}); });
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);
});
});
}); });

View file

@ -1,4 +1,5 @@
import buildShlinkApiClient from '../../../src/utils/services/ShlinkApiClientBuilder'; import buildShlinkApiClient from '../../../src/utils/services/ShlinkApiClientBuilder';
import { buildShlinkBaseUrl } from '../../../src/utils/services/ShlinkApiClient';
describe('ShlinkApiClientBuilder', () => { describe('ShlinkApiClientBuilder', () => {
const createBuilder = () => { const createBuilder = () => {
@ -33,4 +34,13 @@ describe('ShlinkApiClientBuilder', () => {
expect(firstApiClient).toBe(thirdApiClient); expect(firstApiClient).toBe(thirdApiClient);
expect(secondApiClient).toBe(thirdApiClient); expect(secondApiClient).toBe(thirdApiClient);
}); });
it('does not fetch from state when provided param is already selected server', async () => {
const url = 'url';
const apiKey = 'apiKey';
const apiClient = await buildShlinkApiClient({})({ url, apiKey });
expect(apiClient._baseUrl).toEqual(buildShlinkBaseUrl(url));
expect(apiClient._apiKey).toEqual(apiKey);
});
}); });