mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-05 15:57:24 +03:00
Created server reducer test
This commit is contained in:
parent
6969233b6f
commit
e0ab67899d
2 changed files with 102 additions and 13 deletions
|
@ -1,8 +1,9 @@
|
||||||
import ServersService from '../services/ServersService';
|
import ServersService from '../services/ServersService';
|
||||||
|
import { curry } from 'ramda';
|
||||||
|
|
||||||
const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS';
|
export const FETCH_SERVERS = 'shlink/servers/FETCH_SERVERS';
|
||||||
const CREATE_SERVER = 'shlink/servers/CREATE_SERVER';
|
export const CREATE_SERVER = 'shlink/servers/CREATE_SERVER';
|
||||||
const DELETE_SERVER = 'shlink/servers/DELETE_SERVER';
|
export const DELETE_SERVER = 'shlink/servers/DELETE_SERVER';
|
||||||
|
|
||||||
export default function reducer(state = {}, action) {
|
export default function reducer(state = {}, action) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
@ -17,19 +18,20 @@ export default function reducer(state = {}, action) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const listServers = () => {
|
export const _listServers = ServersService => ({
|
||||||
return {
|
|
||||||
type: FETCH_SERVERS,
|
type: FETCH_SERVERS,
|
||||||
servers: ServersService.listServers(),
|
servers: ServersService.listServers(),
|
||||||
};
|
});
|
||||||
};
|
export const listServers = () => _listServers(ServersService);
|
||||||
|
|
||||||
export const createServer = server => {
|
export const _createServer = (ServersService, server) => {
|
||||||
ServersService.createServer(server);
|
ServersService.createServer(server);
|
||||||
return listServers();
|
return _listServers(ServersService);
|
||||||
};
|
};
|
||||||
|
export const createServer = curry(_createServer)(ServersService);
|
||||||
|
|
||||||
export const deleteServer = server => {
|
export const _deleteServer = (ServersService, server) => {
|
||||||
ServersService.deleteServer(server);
|
ServersService.deleteServer(server);
|
||||||
return listServers();
|
return _listServers(ServersService);
|
||||||
};
|
};
|
||||||
|
export const deleteServer = curry(_deleteServer)(ServersService);
|
||||||
|
|
87
test/servers/reducers/server.test.js
Normal file
87
test/servers/reducers/server.test.js
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
import reduce, {
|
||||||
|
_createServer,
|
||||||
|
_deleteServer,
|
||||||
|
_listServers,
|
||||||
|
CREATE_SERVER,
|
||||||
|
DELETE_SERVER,
|
||||||
|
FETCH_SERVERS,
|
||||||
|
} from '../../../src/servers/reducers/server';
|
||||||
|
import * as sinon from 'sinon';
|
||||||
|
|
||||||
|
describe('serverReducer', () => {
|
||||||
|
const servers = {
|
||||||
|
abc123: { id: 'abc123' },
|
||||||
|
def456: { id: 'def456' }
|
||||||
|
};
|
||||||
|
const ServersServiceMock = {
|
||||||
|
listServers: sinon.fake.returns(servers),
|
||||||
|
createServer: sinon.fake(),
|
||||||
|
deleteServer: sinon.fake(),
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('reduce', () => {
|
||||||
|
it('returns servers when action is FETCH_SERVERS', () =>
|
||||||
|
expect(reduce({}, { type: FETCH_SERVERS, servers })).toEqual(servers)
|
||||||
|
);
|
||||||
|
|
||||||
|
it('returns servers when action is DELETE_SERVER', () =>
|
||||||
|
expect(reduce({}, { type: DELETE_SERVER, servers })).toEqual(servers)
|
||||||
|
);
|
||||||
|
|
||||||
|
it('adds server to list when action is CREATE_SERVER', () => {
|
||||||
|
const server = { id: 'abc123' };
|
||||||
|
expect(reduce({}, { type: CREATE_SERVER, server })).toEqual({
|
||||||
|
[server.id]: server
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns default when action is unknown', () =>
|
||||||
|
expect(reduce({}, { type: 'unknown' })).toEqual({})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('action creators', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
ServersServiceMock.listServers.resetHistory();
|
||||||
|
ServersServiceMock.createServer.resetHistory();
|
||||||
|
ServersServiceMock.deleteServer.resetHistory();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('listServers', () => {
|
||||||
|
it('fetches servers and returns them as part of the action', () => {
|
||||||
|
const result = _listServers(ServersServiceMock);
|
||||||
|
|
||||||
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
||||||
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
||||||
|
expect(ServersServiceMock.createServer.callCount).toEqual(0);
|
||||||
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('createServer', () => {
|
||||||
|
it('adds new server and then fetches servers again', () => {
|
||||||
|
const serverToCreate = { id: 'abc123' };
|
||||||
|
const result = _createServer(ServersServiceMock, serverToCreate);
|
||||||
|
|
||||||
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
||||||
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
||||||
|
expect(ServersServiceMock.createServer.callCount).toEqual(1);
|
||||||
|
expect(ServersServiceMock.createServer.firstCall.calledWith(serverToCreate)).toEqual(true);
|
||||||
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('deleteServer', () => {
|
||||||
|
it('deletes a server and then fetches servers again', () => {
|
||||||
|
const serverToDelete = { id: 'abc123' };
|
||||||
|
const result = _deleteServer(ServersServiceMock, serverToDelete);
|
||||||
|
|
||||||
|
expect(result).toEqual({ type: FETCH_SERVERS, servers });
|
||||||
|
expect(ServersServiceMock.listServers.callCount).toEqual(1);
|
||||||
|
expect(ServersServiceMock.createServer.callCount).toEqual(0);
|
||||||
|
expect(ServersServiceMock.deleteServer.callCount).toEqual(1);
|
||||||
|
expect(ServersServiceMock.deleteServer.firstCall.calledWith(serverToDelete)).toEqual(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue