diff --git a/src/servers/data/index.ts b/src/servers/data/index.ts index 61f86009..0a89ae2c 100644 --- a/src/servers/data/index.ts +++ b/src/servers/data/index.ts @@ -35,3 +35,6 @@ export const isReachableServer = (server: SelectedServer): server is ReachableSe export const isServerWithId = (server: SelectedServer | ServerWithId): server is ServerWithId => !!server?.hasOwnProperty('id'); + +export const isNotFoundServer = (server: SelectedServer): server is NotFoundServer => + !!server?.hasOwnProperty('serverNotFound'); diff --git a/src/servers/helpers/withSelectedServer.tsx b/src/servers/helpers/withSelectedServer.tsx index 2215ded3..f308d6e0 100644 --- a/src/servers/helpers/withSelectedServer.tsx +++ b/src/servers/helpers/withSelectedServer.tsx @@ -1,7 +1,7 @@ import React, { FC, useEffect } from 'react'; import { RouteChildrenProps } from 'react-router'; import Message from '../../utils/Message'; -import { isReachableServer, SelectedServer } from '../data'; +import { isNotFoundServer, SelectedServer } from '../data'; interface WithSelectedServerProps extends RouteChildrenProps<{ serverId: string }> { selectServer: (serverId: string) => void; @@ -21,7 +21,7 @@ export const withSelectedServer = (WrappedComponent: FC return ; } - if (!isReachableServer(selectedServer)) { + if (isNotFoundServer(selectedServer)) { return ; } diff --git a/src/utils/services/ColorGenerator.js b/src/utils/services/ColorGenerator.ts similarity index 56% rename from src/utils/services/ColorGenerator.js rename to src/utils/services/ColorGenerator.ts index 1a15f2a4..5f79b0b8 100644 --- a/src/utils/services/ColorGenerator.js +++ b/src/utils/services/ColorGenerator.ts @@ -1,20 +1,21 @@ import PropTypes from 'prop-types'; import { rangeOf } from '../utils'; +import LocalStorage from './LocalStorage'; const HEX_COLOR_LENGTH = 6; const { floor, random } = Math; const letters = '0123456789ABCDEF'; -const buildRandomColor = () => - `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`; -const normalizeKey = (key) => key.toLowerCase().trim(); +const buildRandomColor = () => `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`; +const normalizeKey = (key: string) => key.toLowerCase().trim(); export default class ColorGenerator { - constructor(storage) { - this.storage = storage; - this.colors = this.storage.get('colors') || {}; + private readonly colors: Record; + + public constructor(private readonly storage: LocalStorage) { + this.colors = this.storage.get>('colors') || {}; } - getColorForKey = (key) => { + public readonly getColorForKey = (key: string) => { const normalizedKey = normalizeKey(key); const color = this.colors[normalizedKey]; @@ -26,7 +27,7 @@ export default class ColorGenerator { return color; }; - setColorForKey = (key, color) => { + public readonly setColorForKey = (key: string, color: string) => { const normalizedKey = normalizeKey(key); this.colors[normalizedKey] = color; @@ -36,6 +37,7 @@ export default class ColorGenerator { }; } +/** @deprecated Use ColorGenerator class instead */ export const colorGeneratorType = PropTypes.shape({ getColorForKey: PropTypes.func, setColorForKey: PropTypes.func, diff --git a/test/servers/EditServer.test.js b/test/servers/EditServer.test.js index 0dd0f155..366dd24f 100644 --- a/test/servers/EditServer.test.js +++ b/test/servers/EditServer.test.js @@ -16,8 +16,6 @@ describe('', () => { name: 'name', url: 'url', apiKey: 'apiKey', - printableVersion: 'v1.2.0', - version: '1.2.0' }; beforeEach(() => { diff --git a/test/utils/services/ColorGenerator.test.js b/test/utils/services/ColorGenerator.test.ts similarity index 87% rename from test/utils/services/ColorGenerator.test.js rename to test/utils/services/ColorGenerator.test.ts index 71546d17..990b268e 100644 --- a/test/utils/services/ColorGenerator.test.js +++ b/test/utils/services/ColorGenerator.test.ts @@ -1,16 +1,16 @@ +import { Mock } from 'ts-mockery'; import ColorGenerator from '../../../src/utils/services/ColorGenerator'; +import LocalStorage from '../../../src/utils/services/LocalStorage'; describe('ColorGenerator', () => { - let colorGenerator; - const storageMock = { + let colorGenerator: ColorGenerator; + const storageMock = Mock.of({ set: jest.fn(), get: jest.fn(), - }; + }); beforeEach(() => { - storageMock.set.mockReset(); - storageMock.get.mockReset(); - + jest.clearAllMocks(); colorGenerator = new ColorGenerator(storageMock); }); diff --git a/test/utils/services/Storage.test.js b/test/utils/services/Storage.test.ts similarity index 64% rename from test/utils/services/Storage.test.js rename to test/utils/services/Storage.test.ts index ec427015..e7de6771 100644 --- a/test/utils/services/Storage.test.js +++ b/test/utils/services/Storage.test.ts @@ -1,16 +1,14 @@ +import { Mock } from 'ts-mockery'; import LocalStorage from '../../../src/utils/services/LocalStorage'; describe('LocalStorage', () => { - const localStorageMock = { - getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null), - setItem: jest.fn(), - }; - let storage; + const getItem = jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null); + const setItem = jest.fn(); + const localStorageMock = Mock.of({ getItem, setItem }); + let storage: LocalStorage; beforeEach(() => { - localStorageMock.getItem.mockClear(); - localStorageMock.setItem.mockReset(); - + jest.clearAllMocks(); storage = new LocalStorage(localStorageMock); }); @@ -20,15 +18,15 @@ describe('LocalStorage', () => { storage.set('foo', value); - expect(localStorageMock.setItem).toHaveBeenCalledTimes(1); - expect(localStorageMock.setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value)); + expect(setItem).toHaveBeenCalledTimes(1); + expect(setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value)); }); }); describe('get', () => { it('fetches item from local storage', () => { storage.get('foo'); - expect(localStorageMock.getItem).toHaveBeenCalledTimes(1); + expect(getItem).toHaveBeenCalledTimes(1); }); it('returns parsed value when requested value is found in local storage', () => {