Added tests for new use cases

This commit is contained in:
Alejandro Celaya
2019-04-28 12:40:50 +02:00
parent 20820c47d4
commit 78c34a342d
6 changed files with 76 additions and 31 deletions

View File

@@ -28,7 +28,7 @@ export default class Home extends React.Component {
<h5 className="home__intro">
{!loading && hasServers && <span>Please, select a server.</span>}
{!loading && !hasServers && <span>Please, <Link to="/server/create">add a server</Link>.</span>}
{loading && <span>Trying to load servers....</span>}
{loading && <span>Trying to load servers...</span>}
</h5>
{!loading && hasServers && (

View File

@@ -13,7 +13,7 @@ const initialState = {
loading: false,
};
const assocId = (server) => assoc('id', uuid(), server);
const assocId = (server) => assoc('id', server.id || uuid(), server);
export default handleActions({
[FETCH_SERVERS_START]: (state) => ({ ...state, loading: true }),
@@ -22,8 +22,6 @@ export default handleActions({
export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => {
dispatch({ type: FETCH_SERVERS_START });
// Fetch list from local storage.
const localList = listServers();
if (!isEmpty(localList)) {
@@ -32,12 +30,12 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
return;
}
// If local list is empty, try to fetch it remotely, calculate IDs for every server, and use it
// If local list is empty, try to fetch it remotely and calculate IDs for every server
const { data: remoteList } = await get(`${homepage}/servers.json`);
const listWithIds = map(assocId, remoteList);
createServers(listWithIds);
dispatch({ type: FETCH_SERVERS, list: listWithIds });
dispatch({ type: FETCH_SERVERS, list: listWithIds.reduce((map, server) => ({ ...map, [server.id]: server }), {}) });
};
export const createServer = ({ createServer }, listServersAction) => pipe(createServer, listServersAction);

View File

@@ -1,11 +1,10 @@
import { assoc, curry, dissoc, reduce } from 'ramda';
import { assoc, dissoc, reduce } from 'ramda';
const SERVERS_STORAGE_KEY = 'servers';
export default class ServersService {
constructor(storage) {
this.storage = storage;
this.setServers = curry(this.storage.set)(SERVERS_STORAGE_KEY);
}
listServers = () => this.storage.get(SERVERS_STORAGE_KEY) || {};
@@ -21,9 +20,9 @@ export default class ServersService {
servers
);
this.setServers(allServers);
this.storage.set(SERVERS_STORAGE_KEY, allServers);
};
deleteServer = ({ id }) =>
this.setServers(dissoc(id, this.listServers()));
this.storage.set(SERVERS_STORAGE_KEY, dissoc(id, this.listServers()));
}