2022-11-15 22:31:35 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-04-27 13:54:52 +03:00
|
|
|
import { fetchServers } from '../../../src/servers/reducers/remoteServers';
|
2022-11-05 12:08:24 +03:00
|
|
|
import { createServers } from '../../../src/servers/reducers/servers';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { HttpClient } from '../../../src/common/services/HttpClient';
|
2020-04-27 13:54:52 +03:00
|
|
|
|
|
|
|
describe('remoteServersReducer', () => {
|
2021-10-31 14:07:38 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2020-04-27 13:54:52 +03:00
|
|
|
|
|
|
|
describe('fetchServers', () => {
|
2022-11-10 22:44:58 +03:00
|
|
|
const dispatch = jest.fn();
|
2022-11-15 22:31:35 +03:00
|
|
|
const fetchJson = jest.fn();
|
|
|
|
const httpClient = Mock.of<HttpClient>({ fetchJson });
|
2020-04-27 13:54:52 +03:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
[
|
2022-11-15 01:25:39 +03:00
|
|
|
[
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
],
|
2020-04-27 13:54:52 +03:00
|
|
|
{
|
|
|
|
111: {
|
|
|
|
id: '111',
|
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
|
|
|
222: {
|
|
|
|
id: '222',
|
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2021-10-31 14:20:02 +03:00
|
|
|
[
|
2022-11-15 01:25:39 +03:00
|
|
|
[
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
],
|
2021-10-31 14:20:02 +03:00
|
|
|
{
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-03-26 14:17:42 +03:00
|
|
|
['<html></html>', {}],
|
2021-10-31 14:20:02 +03:00
|
|
|
[{}, {}],
|
2021-10-31 14:07:38 +03:00
|
|
|
])('tries to fetch servers from remote', async (mockedValue, expectedNewServers) => {
|
2022-11-15 22:31:35 +03:00
|
|
|
fetchJson.mockResolvedValue(mockedValue);
|
|
|
|
const doFetchServers = fetchServers(httpClient);
|
2020-04-27 13:54:52 +03:00
|
|
|
|
2022-11-10 22:44:58 +03:00
|
|
|
await doFetchServers()(dispatch, jest.fn(), {});
|
2020-04-27 13:54:52 +03:00
|
|
|
|
2022-11-10 22:44:58 +03:00
|
|
|
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(),
|
|
|
|
}));
|
2022-11-15 22:31:35 +03:00
|
|
|
expect(fetchJson).toHaveBeenCalledTimes(1);
|
2020-04-27 13:54:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|