From 277b5e43f81c13453e12f9a32ab6db3f57bf3f79 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 27 Apr 2020 10:49:55 +0200 Subject: [PATCH] Flatten model holding list of servers --- src/common/Home.js | 13 ++++++------- src/servers/ServersDropdown.js | 12 ++++-------- src/servers/helpers/ServerError.js | 4 ++-- src/servers/reducers/server.js | 12 ++---------- test/common/Home.test.js | 22 +++++++--------------- test/servers/ServersDropdown.test.js | 24 +++++------------------- test/servers/helpers/ServerError.test.js | 2 +- test/servers/reducers/server.test.js | 13 +++++-------- 8 files changed, 32 insertions(+), 70 deletions(-) diff --git a/src/common/Home.js b/src/common/Home.js index 7c7cf396..63fbc80a 100644 --- a/src/common/Home.js +++ b/src/common/Home.js @@ -10,9 +10,9 @@ const propTypes = { servers: PropTypes.object, }; -const Home = ({ resetSelectedServer, servers: { list, loading } }) => { - const servers = values(list); - const hasServers = !isEmpty(servers); +const Home = ({ resetSelectedServer, servers }) => { + const serversList = values(servers); + const hasServers = !isEmpty(serversList); useEffect(() => { resetSelectedServer(); @@ -21,10 +21,9 @@ const Home = ({ resetSelectedServer, servers: { list, loading } }) => { return (

Welcome to Shlink

- - {!loading && hasServers && Please, select a server.} - {!loading && !hasServers && Please, add a server.} - {loading && Trying to load servers...} + + {hasServers && Please, select a server.} + {!hasServers && Please, add a server.}
); diff --git a/src/servers/ServersDropdown.js b/src/servers/ServersDropdown.js index c66cb22c..f6092d60 100644 --- a/src/servers/ServersDropdown.js +++ b/src/servers/ServersDropdown.js @@ -15,22 +15,18 @@ const ServersDropdown = (serversExporter) => class ServersDropdown extends React }; renderServers = () => { - const { servers: { list, loading }, selectedServer } = this.props; - const servers = values(list); + const { servers, selectedServer } = this.props; + const serversList = values(servers); const { push } = this.props.history; const loadServer = (id) => push(`/server/${id}/list-short-urls/1`); - if (loading) { - return Trying to load servers...; - } - - if (isEmpty(servers)) { + if (isEmpty(serversList)) { return Add a server first...; } return ( - {servers.map(({ name, id }) => ( + {serversList.map(({ name, id }) => ( loadServer(id)}> {name} diff --git a/src/servers/helpers/ServerError.js b/src/servers/helpers/ServerError.js index 7389d394..14983987 100644 --- a/src/servers/helpers/ServerError.js +++ b/src/servers/helpers/ServerError.js @@ -13,7 +13,7 @@ const propTypes = { }; export const ServerError = (DeleteServerButton) => { - const ServerErrorComp = ({ type, servers: { list }, selectedServer }) => ( + const ServerErrorComp = ({ type, servers, selectedServer }) => (
@@ -27,7 +27,7 @@ export const ServerError = (DeleteServerButton) => {
- + These are the Shlink servers currently configured. Choose one of them or add a new one. diff --git a/src/servers/reducers/server.js b/src/servers/reducers/server.js index e665751c..d703ce11 100644 --- a/src/servers/reducers/server.js +++ b/src/servers/reducers/server.js @@ -3,25 +3,17 @@ import { pipe, isEmpty, assoc, map, prop } from 'ramda'; import { v4 as uuid } from 'uuid'; import { homepage } from '../../../package.json'; -/* eslint-disable padding-line-between-statements */ -export const FETCH_SERVERS_START = 'shlink/servers/FETCH_SERVERS_START'; export const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS'; -/* eslint-enable padding-line-between-statements */ -const initialState = { - list: {}, - loading: false, -}; +const initialState = {}; const assocId = (server) => assoc('id', server.id || uuid(), server); export default handleActions({ - [FETCH_SERVERS_START]: (state) => ({ ...state, loading: true }), - [FETCH_SERVERS]: (state, { list }) => ({ list, loading: false }), + [FETCH_SERVERS]: (state, { list }) => list, }, initialState); export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => { - dispatch({ type: FETCH_SERVERS_START }); const localList = listServers(); if (!isEmpty(localList)) { diff --git a/test/common/Home.test.js b/test/common/Home.test.js index fa6e7993..0af589b2 100644 --- a/test/common/Home.test.js +++ b/test/common/Home.test.js @@ -6,7 +6,7 @@ describe('', () => { let wrapped; const defaultProps = { resetSelectedServer: jest.fn(), - servers: { loading: false, list: {} }, + servers: {}, }; const createComponent = (props) => { const actualProps = { ...defaultProps, ...props }; @@ -24,20 +24,12 @@ describe('', () => { expect(wrapped.find('Link')).toHaveLength(1); }); - it('shows message when loading servers', () => { - const wrapped = createComponent({ servers: { loading: true } }); - const span = wrapped.find('span'); - - expect(span).toHaveLength(1); - expect(span.text()).toContain('Trying to load servers...'); - }); - - it('Asks to select a server when not loadign and servers exist', () => { - const list = [ - { name: 'foo', id: '1' }, - { name: 'bar', id: '2' }, - ]; - const wrapped = createComponent({ servers: { list } }); + it('asks to select a server when servers exist', () => { + const servers = { + 1: { name: 'foo', id: '1' }, + 2: { name: 'bar', id: '2' }, + }; + const wrapped = createComponent({ servers }); const span = wrapped.find('span'); expect(span).toHaveLength(1); diff --git a/test/servers/ServersDropdown.test.js b/test/servers/ServersDropdown.test.js index 2120e695..d90dbba2 100644 --- a/test/servers/ServersDropdown.test.js +++ b/test/servers/ServersDropdown.test.js @@ -8,12 +8,9 @@ describe('', () => { let wrapped; let ServersDropdown; const servers = { - list: { - '1a': { name: 'foo', id: 1 }, - '2b': { name: 'bar', id: 2 }, - '3c': { name: 'baz', id: 3 }, - }, - loading: false, + '1a': { name: 'foo', id: 1 }, + '2b': { name: 'bar', id: 2 }, + '3c': { name: 'baz', id: 3 }, }; const history = { push: jest.fn(), @@ -26,7 +23,7 @@ describe('', () => { afterEach(() => wrapped.unmount()); it('contains the list of servers, the divider and the export button', () => - expect(wrapped.find(DropdownItem)).toHaveLength(values(servers.list).length + 2)); + expect(wrapped.find(DropdownItem)).toHaveLength(values(servers).length + 2)); it('contains a toggle with proper title', () => expect(wrapped.find(DropdownToggle)).toHaveLength(1)); @@ -40,7 +37,7 @@ describe('', () => { it('shows a message when no servers exist yet', () => { wrapped = shallow( - + ); const item = wrapped.find(DropdownItem); @@ -48,15 +45,4 @@ describe('', () => { expect(item.prop('disabled')).toEqual(true); expect(item.find('i').text()).toEqual('Add a server first...'); }); - - it('shows a message when loading', () => { - wrapped = shallow( - - ); - const item = wrapped.find(DropdownItem); - - expect(item).toHaveLength(1); - expect(item.prop('disabled')).toEqual(true); - expect(item.find('i').text()).toEqual('Trying to load servers...'); - }); }); diff --git a/test/servers/helpers/ServerError.test.js b/test/servers/helpers/ServerError.test.js index 32e1871c..01811564 100644 --- a/test/servers/helpers/ServerError.test.js +++ b/test/servers/helpers/ServerError.test.js @@ -32,7 +32,7 @@ describe('', () => { ])('renders expected information for type "%s"', (type, textsToFind) => { wrapper = shallow( - + ); const wrapperText = wrapper.html(); diff --git a/test/servers/reducers/server.test.js b/test/servers/reducers/server.test.js index baa4e82f..73d6dd83 100644 --- a/test/servers/reducers/server.test.js +++ b/test/servers/reducers/server.test.js @@ -6,7 +6,6 @@ import reducer, { createServers, editServer, FETCH_SERVERS, - FETCH_SERVERS_START, } from '../../../src/servers/reducers/server'; describe('serverReducer', () => { @@ -27,7 +26,7 @@ describe('serverReducer', () => { describe('reducer', () => { it('returns servers when action is FETCH_SERVERS', () => - expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual({ loading: false, list })); + expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual(list)); }); describe('action creators', () => { @@ -39,9 +38,8 @@ describe('serverReducer', () => { it('fetches servers from local storage when found', async () => { await listServers(ServersServiceMock, axios)()(dispatch); - expect(dispatch).toHaveBeenCalledTimes(2); - expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START }); - expect(dispatch).toHaveBeenNthCalledWith(2, expectedFetchServersResult); + expect(dispatch).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenNthCalledWith(1, expectedFetchServersResult); expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1); expect(ServersServiceMock.createServer).not.toHaveBeenCalled(); expect(ServersServiceMock.editServer).not.toHaveBeenCalled(); @@ -90,9 +88,8 @@ describe('serverReducer', () => { 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: expectedList }); + expect(dispatch).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS, list: expectedList }); expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1); expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled(); expect(NoListServersServiceMock.editServer).not.toHaveBeenCalled();