2018-08-26 00:39:27 +03:00
|
|
|
import * as sinon from 'sinon';
|
2018-08-26 11:52:45 +03:00
|
|
|
import reducer, {
|
2018-08-12 10:22:18 +03:00
|
|
|
_selectServer,
|
|
|
|
RESET_SELECTED_SERVER,
|
|
|
|
resetSelectedServer,
|
|
|
|
SELECT_SERVER,
|
|
|
|
} from '../../../src/servers/reducers/selectedServer';
|
|
|
|
import { RESET_SHORT_URL_PARAMS } from '../../../src/short-urls/reducers/shortUrlsListParams';
|
|
|
|
|
|
|
|
describe('selectedServerReducer', () => {
|
2018-08-26 11:52:45 +03:00
|
|
|
describe('reducer', () => {
|
2018-08-12 10:34:14 +03:00
|
|
|
it('returns default when action is not handled', () =>
|
2018-08-26 11:52:45 +03:00
|
|
|
expect(reducer(null, { type: 'unknown' })).toEqual(null));
|
2018-08-12 10:34:14 +03:00
|
|
|
|
|
|
|
it('returns default when action is RESET_SELECTED_SERVER', () =>
|
2018-08-26 11:52:45 +03:00
|
|
|
expect(reducer(null, { type: RESET_SELECTED_SERVER })).toEqual(null));
|
2018-08-12 10:34:14 +03:00
|
|
|
|
|
|
|
it('returns selected server when action is SELECT_SERVER', () => {
|
|
|
|
const selectedServer = { id: 'abc123' };
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-26 11:52:45 +03:00
|
|
|
expect(reducer(null, { type: SELECT_SERVER, selectedServer })).toEqual(selectedServer);
|
2018-08-12 10:34:14 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 10:22:18 +03:00
|
|
|
describe('resetSelectedServer', () => {
|
|
|
|
it('returns proper action', () => {
|
|
|
|
expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-12 10:34:14 +03:00
|
|
|
describe('selectServer', () => {
|
2018-08-12 10:22:18 +03:00
|
|
|
const ShlinkApiClientMock = {
|
2018-08-26 00:39:27 +03:00
|
|
|
setConfig: sinon.spy(),
|
2018-08-12 10:22:18 +03:00
|
|
|
};
|
|
|
|
const serverId = 'abc123';
|
|
|
|
const selectedServer = {
|
2018-08-26 00:39:27 +03:00
|
|
|
id: serverId,
|
2018-08-12 10:22:18 +03:00
|
|
|
};
|
|
|
|
const ServersServiceMock = {
|
2018-08-26 00:39:27 +03:00
|
|
|
findServerById: sinon.fake.returns(selectedServer),
|
2018-08-12 10:22:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
ShlinkApiClientMock.setConfig.resetHistory();
|
|
|
|
ServersServiceMock.findServerById.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches proper actions', () => {
|
|
|
|
const dispatch = sinon.spy();
|
2018-08-26 00:39:27 +03:00
|
|
|
const expectedDispatchCalls = 2;
|
2018-08-12 10:22:18 +03:00
|
|
|
|
|
|
|
_selectServer(ShlinkApiClientMock, ServersServiceMock, serverId)(dispatch);
|
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
|
2018-08-12 10:22:18 +03:00
|
|
|
expect(dispatch.firstCall.calledWith({ type: RESET_SHORT_URL_PARAMS })).toEqual(true);
|
|
|
|
expect(dispatch.secondCall.calledWith({
|
|
|
|
type: SELECT_SERVER,
|
2018-08-26 00:39:27 +03:00
|
|
|
selectedServer,
|
2018-08-12 10:22:18 +03:00
|
|
|
})).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invokes dependencies', () => {
|
|
|
|
_selectServer(ShlinkApiClientMock, ServersServiceMock, serverId)(() => {});
|
|
|
|
|
|
|
|
expect(ShlinkApiClientMock.setConfig.callCount).toEqual(1);
|
|
|
|
expect(ServersServiceMock.findServerById.callCount).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|