Fixed unhandled promise in remoteServers.test

This commit is contained in:
Alejandro Celaya 2021-10-31 12:20:02 +01:00
parent 80f0f9bd08
commit 57e73dcba6
2 changed files with 44 additions and 15 deletions

View file

@ -2,24 +2,17 @@ import { pipe, prop } from 'ramda';
import { AxiosInstance } from 'axios'; import { AxiosInstance } from 'axios';
import { Dispatch } from 'redux'; import { Dispatch } from 'redux';
import { homepage } from '../../../package.json'; import { homepage } from '../../../package.json';
import { ServerData } from '../data'; import { hasServerData, ServerData } from '../data';
import { createServers } from './servers'; import { createServers } from './servers';
const responseToServersList = pipe( const responseToServersList = pipe(
prop<any, any>('data'), prop<any, any>('data'),
(data: any): ServerData[] => { (data: any): ServerData[] => Array.isArray(data) ? data.filter(hasServerData) : [],
if (!Array.isArray(data)) {
throw new Error('Value is not an array');
}
return data as ServerData[];
},
); );
export const fetchServers = ({ get }: AxiosInstance) => () => async (dispatch: Dispatch) => { export const fetchServers = ({ get }: AxiosInstance) => () => async (dispatch: Dispatch) => {
const remoteList = await get(`${homepage}/servers.json`) const resp = await get(`${homepage}/servers.json`);
.then(responseToServersList) const remoteList = responseToServersList(resp);
.catch(() => []);
dispatch(createServers(remoteList)); dispatch(createServers(remoteList));
}; };

View file

@ -13,7 +13,7 @@ describe('remoteServersReducer', () => {
it.each([ it.each([
[ [
Promise.resolve({ {
data: [ data: [
{ {
id: '111', id: '111',
@ -28,7 +28,7 @@ describe('remoteServersReducer', () => {
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a', apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
}, },
], ],
}), },
{ {
111: { 111: {
id: '111', id: '111',
@ -44,8 +44,44 @@ describe('remoteServersReducer', () => {
}, },
}, },
], ],
[ Promise.resolve('<html></html>'), {}], [
[ 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',
},
},
],
[ '<html></html>', {}],
[{}, {}],
])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => { ])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => {
get.mockResolvedValue(mockedValue); get.mockResolvedValue(mockedValue);