mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-03 14:57:22 +03:00
Migrated fetchServers to RTK
This commit is contained in:
parent
a1d869900b
commit
2e0e24d87b
2 changed files with 19 additions and 9 deletions
|
@ -1,18 +1,21 @@
|
||||||
import { pipe, prop } from 'ramda';
|
import { pipe, prop } from 'ramda';
|
||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
import { Dispatch } from 'redux';
|
|
||||||
import pack from '../../../package.json';
|
import pack from '../../../package.json';
|
||||||
import { hasServerData, ServerData } from '../data';
|
import { hasServerData, ServerData } from '../data';
|
||||||
import { createServers } from './servers';
|
import { createServers } from './servers';
|
||||||
|
import { createAsyncThunk } from '../../utils/helpers/redux';
|
||||||
|
|
||||||
const responseToServersList = pipe(
|
const responseToServersList = pipe(
|
||||||
prop<any, any>('data'),
|
prop<any, any>('data'),
|
||||||
(data: any): ServerData[] => (Array.isArray(data) ? data.filter(hasServerData) : []),
|
(data: any): ServerData[] => (Array.isArray(data) ? data.filter(hasServerData) : []),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const fetchServers = ({ get }: AxiosInstance) => () => async (dispatch: Dispatch) => {
|
export const fetchServers = ({ get }: AxiosInstance) => createAsyncThunk(
|
||||||
const resp = await get(`${pack.homepage}/servers.json`);
|
'shlink/remoteServers/fetchServers',
|
||||||
const remoteList = responseToServersList(resp);
|
async (_: void, { dispatch }): Promise<void> => {
|
||||||
|
const resp = await get(`${pack.homepage}/servers.json`);
|
||||||
|
const result = responseToServersList(resp);
|
||||||
|
|
||||||
dispatch(createServers(remoteList));
|
dispatch(createServers(result));
|
||||||
};
|
},
|
||||||
|
);
|
||||||
|
|
|
@ -7,9 +7,9 @@ describe('remoteServersReducer', () => {
|
||||||
afterEach(jest.clearAllMocks);
|
afterEach(jest.clearAllMocks);
|
||||||
|
|
||||||
describe('fetchServers', () => {
|
describe('fetchServers', () => {
|
||||||
|
const dispatch = jest.fn();
|
||||||
const get = jest.fn();
|
const get = jest.fn();
|
||||||
const axios = Mock.of<AxiosInstance>({ get });
|
const axios = Mock.of<AxiosInstance>({ get });
|
||||||
const dispatch = jest.fn();
|
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
[
|
[
|
||||||
|
@ -84,10 +84,17 @@ describe('remoteServersReducer', () => {
|
||||||
[{}, {}],
|
[{}, {}],
|
||||||
])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => {
|
])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => {
|
||||||
get.mockResolvedValue(mockedValue);
|
get.mockResolvedValue(mockedValue);
|
||||||
|
const doFetchServers = fetchServers(axios);
|
||||||
|
|
||||||
await fetchServers(axios)()(dispatch);
|
await doFetchServers()(dispatch, jest.fn(), {});
|
||||||
|
|
||||||
expect(dispatch).toHaveBeenCalledWith({ type: createServers.toString(), payload: expectedNewServers });
|
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
||||||
|
type: doFetchServers.pending.toString(),
|
||||||
|
}));
|
||||||
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: createServers.toString(), payload: expectedNewServers });
|
||||||
|
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||||
|
type: doFetchServers.fulfilled.toString(),
|
||||||
|
}));
|
||||||
expect(get).toHaveBeenCalledTimes(1);
|
expect(get).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue