Extracted helper fetch function and migrated remoteServers redux action from axios to fetch

This commit is contained in:
Alejandro Celaya
2022-11-14 23:25:39 +01:00
parent e5afe4f767
commit d800062159
9 changed files with 67 additions and 78 deletions

View File

@@ -1,5 +1,3 @@
import { Mock } from 'ts-mockery';
import { AxiosInstance } from 'axios';
import { fetchServers } from '../../../src/servers/reducers/remoteServers';
import { createServers } from '../../../src/servers/reducers/servers';
@@ -8,27 +6,24 @@ describe('remoteServersReducer', () => {
describe('fetchServers', () => {
const dispatch = jest.fn();
const get = jest.fn();
const axios = Mock.of<AxiosInstance>({ get });
const fetch = jest.fn();
it.each([
[
{
data: [
{
id: '111',
name: 'acel.me from servers.json',
url: 'https://acel.me',
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
},
{
id: '222',
name: 'Local from servers.json',
url: 'http://localhost:8000',
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
},
],
},
[
{
id: '111',
name: 'acel.me from servers.json',
url: 'https://acel.me',
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
},
{
id: '222',
name: 'Local from servers.json',
url: 'http://localhost:8000',
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
},
],
{
111: {
id: '111',
@@ -45,26 +40,24 @@ describe('remoteServersReducer', () => {
},
],
[
{
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',
},
],
},
[
{
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',
@@ -83,8 +76,8 @@ describe('remoteServersReducer', () => {
['<html></html>', {}],
[{}, {}],
])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => {
get.mockResolvedValue(mockedValue);
const doFetchServers = fetchServers(axios);
fetch.mockResolvedValue(mockedValue);
const doFetchServers = fetchServers(fetch);
await doFetchServers()(dispatch, jest.fn(), {});
@@ -95,7 +88,7 @@ describe('remoteServersReducer', () => {
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
type: doFetchServers.fulfilled.toString(),
}));
expect(get).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledTimes(1);
});
});
});