From 57e73dcba6fa97565cad7781ed6e302af93c660f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Oct 2021 12:20:02 +0100 Subject: [PATCH] Fixed unhandled promise in remoteServers.test --- src/servers/reducers/remoteServers.ts | 15 ++----- test/servers/reducers/remoteServers.test.ts | 44 +++++++++++++++++++-- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/servers/reducers/remoteServers.ts b/src/servers/reducers/remoteServers.ts index 316b03e9..ea21e389 100644 --- a/src/servers/reducers/remoteServers.ts +++ b/src/servers/reducers/remoteServers.ts @@ -2,24 +2,17 @@ import { pipe, prop } from 'ramda'; import { AxiosInstance } from 'axios'; import { Dispatch } from 'redux'; import { homepage } from '../../../package.json'; -import { ServerData } from '../data'; +import { hasServerData, ServerData } from '../data'; import { createServers } from './servers'; const responseToServersList = pipe( prop('data'), - (data: any): ServerData[] => { - if (!Array.isArray(data)) { - throw new Error('Value is not an array'); - } - - return data as ServerData[]; - }, + (data: any): ServerData[] => Array.isArray(data) ? data.filter(hasServerData) : [], ); export const fetchServers = ({ get }: AxiosInstance) => () => async (dispatch: Dispatch) => { - const remoteList = await get(`${homepage}/servers.json`) - .then(responseToServersList) - .catch(() => []); + const resp = await get(`${homepage}/servers.json`); + const remoteList = responseToServersList(resp); dispatch(createServers(remoteList)); }; diff --git a/test/servers/reducers/remoteServers.test.ts b/test/servers/reducers/remoteServers.test.ts index 07dbf0dc..c974fd92 100644 --- a/test/servers/reducers/remoteServers.test.ts +++ b/test/servers/reducers/remoteServers.test.ts @@ -13,7 +13,7 @@ describe('remoteServersReducer', () => { it.each([ [ - Promise.resolve({ + { data: [ { id: '111', @@ -28,7 +28,7 @@ describe('remoteServersReducer', () => { apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a', }, ], - }), + }, { 111: { id: '111', @@ -44,8 +44,44 @@ describe('remoteServersReducer', () => { }, }, ], - [ Promise.resolve(''), {}], - [ Promise.reject({}), {}], + [ + { + data: [ + { + id: '111', + name: 'acel.me from servers.json', + url: 'https://acel.me', + apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0', + }, + { + id: '222', + name: 'Invalid', + }, + { + id: '333', + name: 'Local from servers.json', + url: 'http://localhost:8000', + apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a', + }, + ], + }, + { + 111: { + id: '111', + name: 'acel.me from servers.json', + url: 'https://acel.me', + apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0', + }, + 333: { + id: '333', + name: 'Local from servers.json', + url: 'http://localhost:8000', + apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a', + }, + }, + ], + [ '', {}], + [{}, {}], ])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => { get.mockResolvedValue(mockedValue);