From 0b4a348969fe4c96f3f12fbe506f516c9b11d269 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 23 Aug 2020 11:58:43 +0200 Subject: [PATCH] Migrated remoteServers reducer to TS --- .../reducers/{remoteServers.js => remoteServers.ts} | 13 ++++++++----- ...{remoteServers.test.js => remoteServers.test.ts} | 9 ++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) rename src/servers/reducers/{remoteServers.js => remoteServers.ts} (54%) rename test/servers/reducers/{remoteServers.test.js => remoteServers.test.ts} (86%) diff --git a/src/servers/reducers/remoteServers.js b/src/servers/reducers/remoteServers.ts similarity index 54% rename from src/servers/reducers/remoteServers.js rename to src/servers/reducers/remoteServers.ts index 675c36e7..316b03e9 100644 --- a/src/servers/reducers/remoteServers.js +++ b/src/servers/reducers/remoteServers.ts @@ -1,19 +1,22 @@ import { pipe, prop } from 'ramda'; +import { AxiosInstance } from 'axios'; +import { Dispatch } from 'redux'; import { homepage } from '../../../package.json'; +import { ServerData } from '../data'; import { createServers } from './servers'; const responseToServersList = pipe( - prop('data'), - (value) => { - if (!Array.isArray(value)) { + prop('data'), + (data: any): ServerData[] => { + if (!Array.isArray(data)) { throw new Error('Value is not an array'); } - return value; + return data as ServerData[]; }, ); -export const fetchServers = ({ get }) => () => async (dispatch) => { +export const fetchServers = ({ get }: AxiosInstance) => () => async (dispatch: Dispatch) => { const remoteList = await get(`${homepage}/servers.json`) .then(responseToServersList) .catch(() => []); diff --git a/test/servers/reducers/remoteServers.test.js b/test/servers/reducers/remoteServers.test.ts similarity index 86% rename from test/servers/reducers/remoteServers.test.js rename to test/servers/reducers/remoteServers.test.ts index ba4afc80..3468d1f2 100644 --- a/test/servers/reducers/remoteServers.test.js +++ b/test/servers/reducers/remoteServers.test.ts @@ -1,3 +1,5 @@ +import { Mock } from 'ts-mockery'; +import { AxiosInstance } from 'axios'; import { fetchServers } from '../../../src/servers/reducers/remoteServers'; import { CREATE_SERVERS } from '../../../src/servers/reducers/servers'; @@ -5,7 +7,8 @@ describe('remoteServersReducer', () => { afterEach(jest.resetAllMocks); describe('fetchServers', () => { - const axios = { get: jest.fn() }; + const get = jest.fn(); + const axios = Mock.of({ get }); const dispatch = jest.fn(); it.each([ @@ -44,12 +47,12 @@ describe('remoteServersReducer', () => { [ Promise.resolve(''), {}], [ Promise.reject({}), {}], ])('tries to fetch servers from remote', async (mockedValue, expectedList) => { - axios.get.mockReturnValue(mockedValue); + get.mockReturnValue(mockedValue); await fetchServers(axios)()(dispatch); expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, newServers: expectedList }); - expect(axios.get).toHaveBeenCalledTimes(1); + expect(get).toHaveBeenCalledTimes(1); }); }); });