Skip to content

Commit

Permalink
Merge pull request #168 from acelaya-forks/feature/default-servers-issue
Browse files Browse the repository at this point in the history
Feature/default servers issue
  • Loading branch information
acelaya authored Oct 21, 2019
2 parents e9fc2bb + 0237af7 commit 359b16e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/Bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ With that said, please fill in the information requested next. More information
#### Shlink web client version

* Version: x.y.z
* How do you use shlink-web-client: app.shlink.io|Docker image|self-hosted

#### Summary

Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/Question_Support.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ With that said, please fill in the information requested next. More information
#### Shlink web client version

* Version: x.y.z
* How do you use shlink-web-client: app.shlink.io|Docker image|self-hosted

#### Summary

Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).

## 2.2.2 - 2019-10-21

#### Added

* *Nothing*

#### Changed

* *Nothing*

#### Deprecated

* *Nothing*

#### Removed

* *Nothing*

#### Fixed

* [#167](https://github.com/shlinkio/shlink-web-client/issues/167) Fixed `/servers.json` path not being ignored when returning something other than an array.


## 2.2.1 - 2019-10-18

#### Added
Expand Down
2 changes: 1 addition & 1 deletion config/docker/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server {
index index.html;

# When requesting static paths with extension, try them, and return a 404 if not found
location ~ .+\.(css|js|html|png|jpg|jpeg|gif|bmp|ico|csv|otf|eot|svg|svgz|ttf|woff|woff2|ijmap|pdf|tif|map) {
location ~ .+\.(css|js|html|png|jpg|jpeg|gif|bmp|ico|json|csv|otf|eot|svg|svgz|ttf|woff|woff2|ijmap|pdf|tif|map) {
try_files $uri $uri/ =404;
}

Expand Down
16 changes: 13 additions & 3 deletions src/servers/reducers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
return;
}

// If local list is empty, try to fetch it remotely and calculate IDs for every server
// If local list is empty, try to fetch it remotely (making sure it's an array) and calculate IDs for every server
const getDataAsArrayWithIds = pipe(
prop('data'),
(value) => {
if (!Array.isArray(value)) {
throw new Error('Value is not an array');
}

return value;
},
map(assocId),
);
const remoteList = await get(`${homepage}/servers.json`)
.then(prop('data'))
.then(map(assocId))
.then(getDataAsArrayWithIds)
.catch(() => []);

createServers(remoteList);
Expand Down
59 changes: 43 additions & 16 deletions test/servers/reducers/server.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { values } from 'ramda';
import each from 'jest-each';
import reducer, {
createServer,
deleteServer,
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 359b16e

Please sign in to comment.