mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 02:07:26 +03:00
Added tests for new use cases
This commit is contained in:
parent
20820c47d4
commit
78c34a342d
6 changed files with 76 additions and 31 deletions
|
@ -28,7 +28,7 @@ export default class Home extends React.Component {
|
||||||
<h5 className="home__intro">
|
<h5 className="home__intro">
|
||||||
{!loading && hasServers && <span>Please, select a server.</span>}
|
{!loading && hasServers && <span>Please, select a server.</span>}
|
||||||
{!loading && !hasServers && <span>Please, <Link to="/server/create">add a server</Link>.</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>
|
</h5>
|
||||||
|
|
||||||
{!loading && hasServers && (
|
{!loading && hasServers && (
|
||||||
|
|
|
@ -13,7 +13,7 @@ const initialState = {
|
||||||
loading: false,
|
loading: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const assocId = (server) => assoc('id', uuid(), server);
|
const assocId = (server) => assoc('id', server.id || uuid(), server);
|
||||||
|
|
||||||
export default handleActions({
|
export default handleActions({
|
||||||
[FETCH_SERVERS_START]: (state) => ({ ...state, loading: true }),
|
[FETCH_SERVERS_START]: (state) => ({ ...state, loading: true }),
|
||||||
|
@ -22,8 +22,6 @@ export default handleActions({
|
||||||
|
|
||||||
export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => {
|
export const listServers = ({ listServers, createServers }, { get }) => () => async (dispatch) => {
|
||||||
dispatch({ type: FETCH_SERVERS_START });
|
dispatch({ type: FETCH_SERVERS_START });
|
||||||
|
|
||||||
// Fetch list from local storage.
|
|
||||||
const localList = listServers();
|
const localList = listServers();
|
||||||
|
|
||||||
if (!isEmpty(localList)) {
|
if (!isEmpty(localList)) {
|
||||||
|
@ -32,12 +30,12 @@ export const listServers = ({ listServers, createServers }, { get }) => () => as
|
||||||
return;
|
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 { data: remoteList } = await get(`${homepage}/servers.json`);
|
||||||
const listWithIds = map(assocId, remoteList);
|
const listWithIds = map(assocId, remoteList);
|
||||||
|
|
||||||
createServers(listWithIds);
|
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);
|
export const createServer = ({ createServer }, listServersAction) => pipe(createServer, listServersAction);
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
import { assoc, curry, dissoc, reduce } from 'ramda';
|
import { assoc, dissoc, reduce } from 'ramda';
|
||||||
|
|
||||||
const SERVERS_STORAGE_KEY = 'servers';
|
const SERVERS_STORAGE_KEY = 'servers';
|
||||||
|
|
||||||
export default class ServersService {
|
export default class ServersService {
|
||||||
constructor(storage) {
|
constructor(storage) {
|
||||||
this.storage = storage;
|
this.storage = storage;
|
||||||
this.setServers = curry(this.storage.set)(SERVERS_STORAGE_KEY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listServers = () => this.storage.get(SERVERS_STORAGE_KEY) || {};
|
listServers = () => this.storage.get(SERVERS_STORAGE_KEY) || {};
|
||||||
|
@ -21,9 +20,9 @@ export default class ServersService {
|
||||||
servers
|
servers
|
||||||
);
|
);
|
||||||
|
|
||||||
this.setServers(allServers);
|
this.storage.set(SERVERS_STORAGE_KEY, allServers);
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteServer = ({ id }) =>
|
deleteServer = ({ id }) =>
|
||||||
this.setServers(dissoc(id, this.listServers()));
|
this.storage.set(SERVERS_STORAGE_KEY, dissoc(id, this.listServers()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,8 @@ import Home from '../../src/common/Home';
|
||||||
describe('<Home />', () => {
|
describe('<Home />', () => {
|
||||||
let wrapped;
|
let wrapped;
|
||||||
const defaultProps = {
|
const defaultProps = {
|
||||||
resetSelectedServer() {
|
resetSelectedServer: () => '',
|
||||||
return '';
|
servers: { loading: false, list: {} },
|
||||||
},
|
|
||||||
servers: {},
|
|
||||||
};
|
};
|
||||||
const createComponent = (props) => {
|
const createComponent = (props) => {
|
||||||
const actualProps = { ...defaultProps, ...props };
|
const actualProps = { ...defaultProps, ...props };
|
||||||
|
@ -41,10 +39,22 @@ describe('<Home />', () => {
|
||||||
expect(wrapped.find('ListGroup')).toHaveLength(0);
|
expect(wrapped.find('ListGroup')).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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...');
|
||||||
|
expect(wrapped.find('ListGroup')).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('shows servers list when list of servers is not empty', () => {
|
it('shows servers list when list of servers is not empty', () => {
|
||||||
const servers = {
|
const servers = {
|
||||||
|
loading: false,
|
||||||
|
list: {
|
||||||
1: { name: 'foo', id: '123' },
|
1: { name: 'foo', id: '123' },
|
||||||
2: { name: 'bar', id: '456' },
|
2: { name: 'bar', id: '456' },
|
||||||
|
},
|
||||||
};
|
};
|
||||||
const wrapped = createComponent({ servers });
|
const wrapped = createComponent({ servers });
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,12 @@ describe('<ServersDropdown />', () => {
|
||||||
let wrapped;
|
let wrapped;
|
||||||
let ServersDropdown;
|
let ServersDropdown;
|
||||||
const servers = {
|
const servers = {
|
||||||
|
list: {
|
||||||
'1a': { name: 'foo', id: 1 },
|
'1a': { name: 'foo', id: 1 },
|
||||||
'2b': { name: 'bar', id: 2 },
|
'2b': { name: 'bar', id: 2 },
|
||||||
'3c': { name: 'baz', id: 3 },
|
'3c': { name: 'baz', id: 3 },
|
||||||
|
},
|
||||||
|
loading: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
@ -20,7 +23,7 @@ describe('<ServersDropdown />', () => {
|
||||||
afterEach(() => wrapped.unmount());
|
afterEach(() => wrapped.unmount());
|
||||||
|
|
||||||
it('contains the list of servers', () =>
|
it('contains the list of servers', () =>
|
||||||
expect(wrapped.find(DropdownItem).filter('[to]')).toHaveLength(values(servers).length));
|
expect(wrapped.find(DropdownItem).filter('[to]')).toHaveLength(values(servers.list).length));
|
||||||
|
|
||||||
it('contains a toggle with proper title', () =>
|
it('contains a toggle with proper title', () =>
|
||||||
expect(wrapped.find(DropdownToggle)).toHaveLength(1));
|
expect(wrapped.find(DropdownToggle)).toHaveLength(1));
|
||||||
|
@ -32,12 +35,21 @@ describe('<ServersDropdown />', () => {
|
||||||
expect(items.filter('.servers-dropdown__export-item')).toHaveLength(1);
|
expect(items.filter('.servers-dropdown__export-item')).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('contains a message when no servers exist yet', () => {
|
it('shows a message when no servers exist yet', () => {
|
||||||
wrapped = shallow(<ServersDropdown servers={{}} listServers={identity} />);
|
wrapped = shallow(<ServersDropdown servers={{ loading: false, list: {} }} listServers={identity} />);
|
||||||
const item = wrapped.find(DropdownItem);
|
const item = wrapped.find(DropdownItem);
|
||||||
|
|
||||||
expect(item).toHaveLength(1);
|
expect(item).toHaveLength(1);
|
||||||
expect(item.prop('disabled')).toEqual(true);
|
expect(item.prop('disabled')).toEqual(true);
|
||||||
expect(item.find('i').text()).toEqual('Add a server first...');
|
expect(item.find('i').text()).toEqual('Add a server first...');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('shows a message when loading', () => {
|
||||||
|
wrapped = shallow(<ServersDropdown servers={{ loading: true, list: {} }} listServers={identity} />);
|
||||||
|
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...');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,17 +4,17 @@ import reducer, {
|
||||||
deleteServer,
|
deleteServer,
|
||||||
listServers,
|
listServers,
|
||||||
createServers,
|
createServers,
|
||||||
FETCH_SERVERS,
|
FETCH_SERVERS, FETCH_SERVERS_START,
|
||||||
} from '../../../src/servers/reducers/server';
|
} from '../../../src/servers/reducers/server';
|
||||||
|
|
||||||
describe('serverReducer', () => {
|
describe('serverReducer', () => {
|
||||||
const payload = {
|
const list = {
|
||||||
abc123: { id: 'abc123' },
|
abc123: { id: 'abc123' },
|
||||||
def456: { id: 'def456' },
|
def456: { id: 'def456' },
|
||||||
};
|
};
|
||||||
const expectedFetchServersResult = { type: FETCH_SERVERS, payload };
|
const expectedFetchServersResult = { type: FETCH_SERVERS, list };
|
||||||
const ServersServiceMock = {
|
const ServersServiceMock = {
|
||||||
listServers: jest.fn(() => payload),
|
listServers: jest.fn(() => list),
|
||||||
createServer: jest.fn(),
|
createServer: jest.fn(),
|
||||||
deleteServer: jest.fn(),
|
deleteServer: jest.fn(),
|
||||||
createServers: jest.fn(),
|
createServers: jest.fn(),
|
||||||
|
@ -22,7 +22,7 @@ describe('serverReducer', () => {
|
||||||
|
|
||||||
describe('reducer', () => {
|
describe('reducer', () => {
|
||||||
it('returns servers when action is FETCH_SERVERS', () =>
|
it('returns servers when action is FETCH_SERVERS', () =>
|
||||||
expect(reducer({}, { type: FETCH_SERVERS, payload })).toEqual(payload));
|
expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual({ loading: false, list }));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('action creators', () => {
|
describe('action creators', () => {
|
||||||
|
@ -34,14 +34,40 @@ describe('serverReducer', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('listServers', () => {
|
describe('listServers', () => {
|
||||||
it('fetches servers and returns them as part of the action', () => {
|
const axios = { get: jest.fn().mockResolvedValue({ data: [] }) };
|
||||||
const result = listServers(ServersServiceMock)();
|
const dispatch = jest.fn();
|
||||||
|
|
||||||
expect(result).toEqual(expectedFetchServersResult);
|
beforeEach(() => {
|
||||||
|
axios.get.mockClear();
|
||||||
|
dispatch.mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
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(ServersServiceMock.listServers).toHaveBeenCalledTimes(1);
|
expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1);
|
||||||
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
|
expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
|
||||||
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
||||||
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
|
expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
|
||||||
|
expect(axios.get).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('tries to fetch servers from remote when not found locally', async () => {
|
||||||
|
const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) };
|
||||||
|
|
||||||
|
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(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1);
|
||||||
|
expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled();
|
||||||
|
expect(NoListServersServiceMock.deleteServer).not.toHaveBeenCalled();
|
||||||
|
expect(NoListServersServiceMock.createServers).toHaveBeenCalledTimes(1);
|
||||||
|
expect(axios.get).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -75,7 +101,7 @@ describe('serverReducer', () => {
|
||||||
|
|
||||||
describe('createServer', () => {
|
describe('createServer', () => {
|
||||||
it('creates multiple servers and then fetches servers again', () => {
|
it('creates multiple servers and then fetches servers again', () => {
|
||||||
const serversToCreate = values(payload);
|
const serversToCreate = values(list);
|
||||||
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
|
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
|
||||||
|
|
||||||
expect(result).toEqual(expectedFetchServersResult);
|
expect(result).toEqual(expectedFetchServersResult);
|
||||||
|
|
Loading…
Reference in a new issue