mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 01:20:24 +03:00
Created selectedServer reducer test
This commit is contained in:
parent
f23245a39c
commit
ec4c14e8de
3 changed files with 60 additions and 4 deletions
|
@ -1,9 +1,10 @@
|
|||
import ShlinkApiClient from '../../api/ShlinkApiClient';
|
||||
import ServersService from '../../servers/services/ServersService';
|
||||
import { resetShortUrlParams } from '../../short-urls/reducers/shortUrlsListParams'
|
||||
import { curry } from 'ramda';
|
||||
|
||||
const SELECT_SERVER = 'shlink/selectedServer/SELECT_SERVER';
|
||||
const RESET_SELECTED_SERVER = 'shlink/selectedServer/RESET_SELECTED_SERVER';
|
||||
export const SELECT_SERVER = 'shlink/selectedServer/SELECT_SERVER';
|
||||
export const RESET_SELECTED_SERVER = 'shlink/selectedServer/RESET_SELECTED_SERVER';
|
||||
|
||||
const defaultState = null;
|
||||
|
||||
|
@ -20,7 +21,7 @@ export default function reducer(state = defaultState, action) {
|
|||
|
||||
export const resetSelectedServer = () => ({ type: RESET_SELECTED_SERVER });
|
||||
|
||||
export const selectServer = serverId => dispatch => {
|
||||
export const _selectServer = (ShlinkApiClient, ServersService, serverId) => dispatch => {
|
||||
dispatch(resetShortUrlParams());
|
||||
|
||||
const selectedServer = ServersService.findServerById(serverId);
|
||||
|
@ -31,3 +32,4 @@ export const selectServer = serverId => dispatch => {
|
|||
selectedServer
|
||||
})
|
||||
};
|
||||
export const selectServer = curry(_selectServer)(ShlinkApiClient, ServersService);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { LIST_SHORT_URLS } from './shortUrlsList';
|
||||
|
||||
const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS';
|
||||
export const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS';
|
||||
|
||||
const defaultState = { page: '1' };
|
||||
|
||||
|
|
54
test/servers/reducers/selectedServer.test.js
Normal file
54
test/servers/reducers/selectedServer.test.js
Normal file
|
@ -0,0 +1,54 @@
|
|||
import {
|
||||
_selectServer,
|
||||
RESET_SELECTED_SERVER,
|
||||
resetSelectedServer,
|
||||
SELECT_SERVER,
|
||||
} from '../../../src/servers/reducers/selectedServer';
|
||||
import * as sinon from 'sinon';
|
||||
import { RESET_SHORT_URL_PARAMS } from '../../../src/short-urls/reducers/shortUrlsListParams';
|
||||
|
||||
describe('selectedServerReducer', () => {
|
||||
describe('resetSelectedServer', () => {
|
||||
it('returns proper action', () => {
|
||||
expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectedServer', () => {
|
||||
const ShlinkApiClientMock = {
|
||||
setConfig: sinon.spy()
|
||||
};
|
||||
const serverId = 'abc123';
|
||||
const selectedServer = {
|
||||
id: serverId
|
||||
};
|
||||
const ServersServiceMock = {
|
||||
findServerById: sinon.fake.returns(selectedServer)
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
ShlinkApiClientMock.setConfig.resetHistory();
|
||||
ServersServiceMock.findServerById.resetHistory();
|
||||
});
|
||||
|
||||
it('dispatches proper actions', () => {
|
||||
const dispatch = sinon.spy();
|
||||
|
||||
_selectServer(ShlinkApiClientMock, ServersServiceMock, serverId)(dispatch);
|
||||
|
||||
expect(dispatch.callCount).toEqual(2);
|
||||
expect(dispatch.firstCall.calledWith({ type: RESET_SHORT_URL_PARAMS })).toEqual(true);
|
||||
expect(dispatch.secondCall.calledWith({
|
||||
type: SELECT_SERVER,
|
||||
selectedServer
|
||||
})).toEqual(true);
|
||||
});
|
||||
|
||||
it('invokes dependencies', () => {
|
||||
_selectServer(ShlinkApiClientMock, ServersServiceMock, serverId)(() => {});
|
||||
|
||||
expect(ShlinkApiClientMock.setConfig.callCount).toEqual(1);
|
||||
expect(ServersServiceMock.findServerById.callCount).toEqual(1);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue