mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Ensured default servers is validated as JSON and ignored otherwise
This commit is contained in:
parent
66124370a6
commit
f74d135922
2 changed files with 46 additions and 18 deletions
|
@ -31,9 +31,10 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
|
|||
}
|
||||
|
||||
// If local list is empty, try to fetch it remotely and calculate IDs for every server
|
||||
// It's important to parse the content to json, so that it is ignored for other formats (because it will catch)
|
||||
const getDataAsJsonWithIds = pipe(prop('data'), JSON.parse, map(assocId));
|
||||
const remoteList = await get(`${homepage}/servers.json`)
|
||||
.then(prop('data'))
|
||||
.then(map(assocId))
|
||||
.then(getDataAsJsonWithIds)
|
||||
.catch(() => []);
|
||||
|
||||
createServers(remoteList);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { values } from 'ramda';
|
||||
import each from 'jest-each';
|
||||
import reducer, {
|
||||
createServer,
|
||||
deleteServer,
|
||||
|
@ -20,27 +21,18 @@ describe('serverReducer', () => {
|
|||
createServers: jest.fn(),
|
||||
};
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns servers when action is FETCH_SERVERS', () =>
|
||||
expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual({ loading: false, list }));
|
||||
});
|
||||
|
||||
describe('action creators', () => {
|
||||
beforeEach(() => {
|
||||
ServersServiceMock.listServers.mockClear();
|
||||
ServersServiceMock.createServer.mockReset();
|
||||
ServersServiceMock.deleteServer.mockReset();
|
||||
ServersServiceMock.createServers.mockReset();
|
||||
});
|
||||
|
||||
describe('listServers', () => {
|
||||
const axios = { get: jest.fn().mockResolvedValue({ data: [] }) };
|
||||
const axios = { get: jest.fn() };
|
||||
const dispatch = jest.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
axios.get.mockClear();
|
||||
dispatch.mockReset();
|
||||
});
|
||||
const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) };
|
||||
|
||||
it('fetches servers from local storage when found', async () => {
|
||||
await listServers(ServersServiceMock, axios)()(dispatch);
|
||||
|
@ -55,14 +47,49 @@ describe('serverReducer', () => {
|
|||
expect(axios.get).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('tries to fetch servers from remote when not found locally', async () => {
|
||||
const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) };
|
||||
each([
|
||||
[
|
||||
Promise.resolve({
|
||||
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"
|
||||
}
|
||||
]`,
|
||||
}),
|
||||
{
|
||||
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',
|
||||
},
|
||||
},
|
||||
],
|
||||
[ Promise.resolve('<html></html>'), {}],
|
||||
[ Promise.reject({}), {}],
|
||||
]).it('tries to fetch servers from remote when not found locally', async (mockedValue, expectedList) => {
|
||||
axios.get.mockReturnValue(mockedValue);
|
||||
|
||||
await listServers(NoListServersServiceMock, axios)()(dispatch);
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: FETCH_SERVERS, list: {} });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: FETCH_SERVERS, list: expectedList });
|
||||
expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1);
|
||||
expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled();
|
||||
expect(NoListServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
||||
|
|
Loading…
Reference in a new issue