2023-08-30 00:12:25 +03:00
|
|
|
import type { HttpClient } from '@shlinkio/shlink-js-sdk';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2020-04-27 13:54:52 +03:00
|
|
|
import { fetchServers } from '../../../src/servers/reducers/remoteServers';
|
|
|
|
|
|
|
|
describe('remoteServersReducer', () => {
|
|
|
|
describe('fetchServers', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const dispatch = vi.fn();
|
2023-08-30 00:12:25 +03:00
|
|
|
const jsonRequest = vi.fn();
|
|
|
|
const httpClient = fromPartial<HttpClient>({ jsonRequest });
|
2020-04-27 13:54:52 +03:00
|
|
|
|
|
|
|
it.each([
|
2024-11-01 14:15:15 +03:00
|
|
|
{
|
|
|
|
serversArray: [
|
2022-11-15 01:25:39 +03:00
|
|
|
{
|
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
|
|
|
],
|
2024-11-01 14:15:15 +03:00
|
|
|
expectedNewServers: {
|
2024-11-01 13:59:54 +03:00
|
|
|
'acel.me-from-servers.json-acel.me': {
|
|
|
|
id: 'acel.me-from-servers.json-acel.me',
|
2020-04-27 13:54:52 +03:00
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
2024-11-01 13:59:54 +03:00
|
|
|
'local-from-servers.json-localhost-8000': {
|
|
|
|
id: 'local-from-servers.json-localhost-8000',
|
2020-04-27 13:54:52 +03:00
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
|
|
|
},
|
2024-11-01 14:15:15 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
serversArray: [
|
2022-11-15 01:25:39 +03:00
|
|
|
{
|
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Invalid',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
|
|
|
],
|
2024-11-01 14:15:15 +03:00
|
|
|
expectedNewServers: {
|
2024-11-01 13:59:54 +03:00
|
|
|
'acel.me-from-servers.json-acel.me': {
|
|
|
|
id: 'acel.me-from-servers.json-acel.me',
|
2021-10-31 14:20:02 +03:00
|
|
|
name: 'acel.me from servers.json',
|
|
|
|
url: 'https://acel.me',
|
|
|
|
apiKey: '07fb8a96-8059-4094-a24c-80a7d5e7e9b0',
|
|
|
|
},
|
2024-11-01 13:59:54 +03:00
|
|
|
'local-from-servers.json-localhost-8000': {
|
|
|
|
id: 'local-from-servers.json-localhost-8000',
|
2021-10-31 14:20:02 +03:00
|
|
|
name: 'Local from servers.json',
|
|
|
|
url: 'http://localhost:8000',
|
|
|
|
apiKey: '7a531c75-134e-4d5c-86e0-a71b7167b57a',
|
|
|
|
},
|
2024-11-01 14:15:15 +03:00
|
|
|
|
2021-10-31 14:20:02 +03:00
|
|
|
},
|
2024-11-01 14:15:15 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
serversArray: '<html></html>',
|
|
|
|
expectedNewServers: {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
serversArray: {},
|
|
|
|
expectedNewServers: {},
|
|
|
|
},
|
|
|
|
])('tries to fetch servers from remote', async ({ serversArray, expectedNewServers }) => {
|
|
|
|
jsonRequest.mockResolvedValue(serversArray);
|
2022-11-15 22:31:35 +03:00
|
|
|
const doFetchServers = fetchServers(httpClient);
|
2020-04-27 13:54:52 +03:00
|
|
|
|
2023-05-27 12:57:26 +03:00
|
|
|
await doFetchServers()(dispatch, vi.fn(), {});
|
2020-04-27 13:54:52 +03:00
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(3);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ payload: expectedNewServers }));
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(3, expect.objectContaining({ payload: undefined }));
|
2023-08-30 00:12:25 +03:00
|
|
|
expect(jsonRequest).toHaveBeenCalledTimes(1);
|
2020-04-27 13:54:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|