Migrated remoteServers reducer to TS

This commit is contained in:
Alejandro Celaya 2020-08-23 11:58:43 +02:00
parent 3e2fee0df5
commit 0b4a348969
2 changed files with 14 additions and 8 deletions

View file

@ -1,19 +1,22 @@
import { pipe, prop } from 'ramda'; import { pipe, prop } from 'ramda';
import { AxiosInstance } from 'axios';
import { Dispatch } from 'redux';
import { homepage } from '../../../package.json'; import { homepage } from '../../../package.json';
import { ServerData } from '../data';
import { createServers } from './servers'; import { createServers } from './servers';
const responseToServersList = pipe( const responseToServersList = pipe(
prop('data'), prop<any, any>('data'),
(value) => { (data: any): ServerData[] => {
if (!Array.isArray(value)) { if (!Array.isArray(data)) {
throw new Error('Value is not an array'); 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`) const remoteList = await get(`${homepage}/servers.json`)
.then(responseToServersList) .then(responseToServersList)
.catch(() => []); .catch(() => []);

View file

@ -1,3 +1,5 @@
import { Mock } from 'ts-mockery';
import { AxiosInstance } from 'axios';
import { fetchServers } from '../../../src/servers/reducers/remoteServers'; import { fetchServers } from '../../../src/servers/reducers/remoteServers';
import { CREATE_SERVERS } from '../../../src/servers/reducers/servers'; import { CREATE_SERVERS } from '../../../src/servers/reducers/servers';
@ -5,7 +7,8 @@ describe('remoteServersReducer', () => {
afterEach(jest.resetAllMocks); afterEach(jest.resetAllMocks);
describe('fetchServers', () => { describe('fetchServers', () => {
const axios = { get: jest.fn() }; const get = jest.fn();
const axios = Mock.of<AxiosInstance>({ get });
const dispatch = jest.fn(); const dispatch = jest.fn();
it.each([ it.each([
@ -44,12 +47,12 @@ describe('remoteServersReducer', () => {
[ Promise.resolve('<html></html>'), {}], [ Promise.resolve('<html></html>'), {}],
[ Promise.reject({}), {}], [ Promise.reject({}), {}],
])('tries to fetch servers from remote', async (mockedValue, expectedList) => { ])('tries to fetch servers from remote', async (mockedValue, expectedList) => {
axios.get.mockReturnValue(mockedValue); get.mockReturnValue(mockedValue);
await fetchServers(axios)()(dispatch); await fetchServers(axios)()(dispatch);
expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, newServers: expectedList }); expect(dispatch).toHaveBeenCalledWith({ type: CREATE_SERVERS, newServers: expectedList });
expect(axios.get).toHaveBeenCalledTimes(1); expect(get).toHaveBeenCalledTimes(1);
}); });
}); });
}); });