diff --git a/src/servers/reducers/selectedServer.js b/src/servers/reducers/selectedServer.js index 18833b34..3ed83fa6 100644 --- a/src/servers/reducers/selectedServer.js +++ b/src/servers/reducers/selectedServer.js @@ -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); diff --git a/src/short-urls/reducers/shortUrlsListParams.js b/src/short-urls/reducers/shortUrlsListParams.js index 3217167d..29464264 100644 --- a/src/short-urls/reducers/shortUrlsListParams.js +++ b/src/short-urls/reducers/shortUrlsListParams.js @@ -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' }; diff --git a/test/servers/reducers/selectedServer.test.js b/test/servers/reducers/selectedServer.test.js new file mode 100644 index 00000000..731655ab --- /dev/null +++ b/test/servers/reducers/selectedServer.test.js @@ -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); + }); + }); +});