From 6d996baf5db67a6cb10f310565fb77c40892eb25 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 5 Oct 2019 10:40:32 +0200 Subject: [PATCH] Added tests for new logics --- src/utils/services/ShlinkApiClient.js | 5 ++- test/utils/ForVersion.test.js | 43 +++++++++++++++++++ test/utils/services/ShlinkApiClient.test.js | 16 +++++++ .../services/ShlinkApiClientBuilder.test.js | 10 +++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 test/utils/ForVersion.test.js diff --git a/src/utils/services/ShlinkApiClient.js b/src/utils/services/ShlinkApiClient.js index 2884b721..ac0b6fbf 100644 --- a/src/utils/services/ShlinkApiClient.js +++ b/src/utils/services/ShlinkApiClient.js @@ -2,12 +2,13 @@ import qs from 'qs'; import { isEmpty, isNil, reject } from 'ramda'; 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 { constructor(axios, baseUrl, apiKey) { this.axios = axios; - this._baseUrl = buildRestUrl(baseUrl); + this._baseUrl = buildShlinkBaseUrl(baseUrl); this._apiKey = apiKey || ''; } diff --git a/test/utils/ForVersion.test.js b/test/utils/ForVersion.test.js new file mode 100644 index 00000000..83be6fc5 --- /dev/null +++ b/test/utils/ForVersion.test.js @@ -0,0 +1,43 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import ForVersion from '../../src/utils/ForVersion'; + +describe('', () => { + let wrapped; + + const renderComponent = (minVersion, currentServerVersion) => { + wrapped = mount( + + Hello + + ); + + 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('Hello'); + }); + + it('renders children when current version is higher than min version', () => { + const wrapped = renderComponent('2.0.0', '2.1.0'); + + expect(wrapped.html()).toContain('Hello'); + }); +}); diff --git a/test/utils/services/ShlinkApiClient.test.js b/test/utils/services/ShlinkApiClient.test.js index 024febf3..c2ef2f2a 100644 --- a/test/utils/services/ShlinkApiClient.test.js +++ b/test/utils/services/ShlinkApiClient.test.js @@ -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); + }); + }); }); diff --git a/test/utils/services/ShlinkApiClientBuilder.test.js b/test/utils/services/ShlinkApiClientBuilder.test.js index c6875ed0..02ef8cd8 100644 --- a/test/utils/services/ShlinkApiClientBuilder.test.js +++ b/test/utils/services/ShlinkApiClientBuilder.test.js @@ -1,4 +1,5 @@ import buildShlinkApiClient from '../../../src/utils/services/ShlinkApiClientBuilder'; +import { buildShlinkBaseUrl } from '../../../src/utils/services/ShlinkApiClient'; describe('ShlinkApiClientBuilder', () => { const createBuilder = () => { @@ -33,4 +34,13 @@ describe('ShlinkApiClientBuilder', () => { expect(firstApiClient).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); + }); });