diff --git a/app/actions/server.js b/app/actions/server.js deleted file mode 100644 index acc7c7cb3..000000000 --- a/app/actions/server.js +++ /dev/null @@ -1,61 +0,0 @@ -import { SERVER } from './actionsTypes'; - -export function selectServerRequest(server, version, fetchVersion = true, changeServer = false) { - return { - type: SERVER.SELECT_REQUEST, - server, - version, - fetchVersion, - changeServer - }; -} - -export function selectServerSuccess(server, version) { - return { - type: SERVER.SELECT_SUCCESS, - server, - version - }; -} - -export function selectServerFailure() { - return { - type: SERVER.SELECT_FAILURE - }; -} - -// TODO -export function serverRequest(server, username, fromServerHistory = false) { - return { - type: SERVER.REQUEST, - server, - username: username || null, - fromServerHistory - }; -} - -export function serverSuccess() { - return { - type: SERVER.SUCCESS - }; -} - -export function serverFailure(err) { - return { - type: SERVER.FAILURE, - err - }; -} - -export function serverInitAdd(previousServer) { - return { - type: SERVER.INIT_ADD, - previousServer - }; -} - -export function serverFinishAdd() { - return { - type: SERVER.FINISH_ADD - }; -} diff --git a/app/actions/server.ts b/app/actions/server.ts new file mode 100644 index 000000000..da8ee6d4c --- /dev/null +++ b/app/actions/server.ts @@ -0,0 +1,90 @@ +import { Action } from 'redux'; + +import { SERVER } from './actionsTypes'; + +interface ISelectServer extends Action { + server: string; + version?: string; + fetchVersion: boolean; + changeServer: boolean; +} + +interface ISelectServerSuccess extends Action { + server: string; + version: string; +} + +interface IServer extends Action { + server: string; + username: string | null; + fromServerHistory: boolean; +} + +interface IServerInit extends Action { + previousServer: string; +} + +interface IServerFailure extends Action { + err: any; +} + +export type TActionServer = ISelectServer & ISelectServerSuccess & IServer & IServerInit & IServerFailure; + +export function selectServerRequest(server: string, version?: string, fetchVersion = true, changeServer = false): ISelectServer { + return { + type: SERVER.SELECT_REQUEST, + server, + version, + fetchVersion, + changeServer + }; +} + +export function selectServerSuccess(server: string, version: string): ISelectServerSuccess { + return { + type: SERVER.SELECT_SUCCESS, + server, + version + }; +} + +export function selectServerFailure(): Action { + return { + type: SERVER.SELECT_FAILURE + }; +} + +export function serverRequest(server: string, username: string | null = null, fromServerHistory = false): IServer { + return { + type: SERVER.REQUEST, + server, + username, + fromServerHistory + }; +} + +export function serverSuccess(): Action { + return { + type: SERVER.SUCCESS + }; +} + +export function serverFailure(err: any): IServerFailure { + return { + type: SERVER.FAILURE, + err + }; +} + +export function serverInitAdd(previousServer: string): IServerInit { + return { + type: SERVER.INIT_ADD, + previousServer + }; +} + +export function serverFinishAdd(): Action { + return { + type: SERVER.FINISH_ADD + }; +} diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index 3a46d6ef3..2154bdfcb 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,3 +1,5 @@ +// ACTIONS +import { TActionServer } from '../../actions/server'; import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionCustomEmojis } from '../../actions/customEmojis'; import { TActionEncryption } from '../../actions/encryption'; @@ -13,6 +15,7 @@ import { IEncryption } from '../../reducers/encryption'; import { IInviteLinks } from '../../reducers/inviteLinks'; import { IRoles } from '../../reducers/roles'; import { ISelectedUsers } from '../../reducers/selectedUsers'; +import { IServer } from '../../reducers/server'; import { IConnect } from '../../reducers/connect'; import { ISettings } from '../../reducers/settings'; @@ -20,7 +23,7 @@ export interface IApplicationState { settings: ISettings; login: any; meteor: IConnect; - server: any; + server: IServer; selectedUsers: ISelectedUsers; createChannel: any; app: any; @@ -48,4 +51,5 @@ export type TApplicationActions = TActionActiveUsers & IActionSettings & TActionEncryption & TActionSortPreferences & - TActionUserTyping; + TActionUserTyping & + TActionServer; diff --git a/app/reducers/server.test.ts b/app/reducers/server.test.ts new file mode 100644 index 000000000..0b0f8fd40 --- /dev/null +++ b/app/reducers/server.test.ts @@ -0,0 +1,68 @@ +import { + selectServerRequest, + serverRequest, + selectServerSuccess, + serverInitAdd, + serverFailure, + serverFinishAdd, + selectServerFailure +} from '../actions/server'; +import { mockedStore } from './mockedStore'; +import { initialState } from './server'; + +describe('test server reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().server; + expect(state).toEqual(initialState); + }); + + it('should return modified store after serverRequest', () => { + const server = 'https://open.rocket.chat/'; + mockedStore.dispatch(serverRequest(server)); + const state = mockedStore.getState().server; + const manipulated = { ...initialState, connecting: true, failure: false }; + expect(state).toEqual(manipulated); + }); + + it('should return modified store after selectServerFailure', () => { + mockedStore.dispatch(selectServerFailure()); + const state = mockedStore.getState().server; + const manipulated = { ...initialState, connecting: false, connected: false, loading: false, changingServer: false }; + expect(state).toEqual(manipulated); + }); + + it('should return modified store after selectServer', () => { + const server = 'https://open.rocket.chat/'; + mockedStore.dispatch(selectServerRequest(server)); + const state = mockedStore.getState().server.server; + expect(state).toEqual(server); + }); + + it('should return modified store after selectServerSucess', () => { + const server = 'https://open.rocket.chat/'; + const version = '4.1.0'; + mockedStore.dispatch(selectServerSuccess(server, version)); + const state = mockedStore.getState().server; + const manipulated = { ...initialState, server, version, connected: true, loading: false }; + expect(state).toEqual(manipulated); + }); + + it('should return modified store after serverRequestInitAdd', () => { + const previousServer = 'https://mobile.rocket.chat'; + mockedStore.dispatch(serverInitAdd(previousServer)); + const state = mockedStore.getState().server.previousServer; + expect(state).toEqual(previousServer); + }); + + it('should return modified store after serverFinishAdd', () => { + mockedStore.dispatch(serverFinishAdd()); + const state = mockedStore.getState().server.previousServer; + expect(state).toEqual(null); + }); + + it('should return modified store after serverRequestFailure', () => { + mockedStore.dispatch(serverFailure('error')); + const state = mockedStore.getState().server; + expect(state.failure).toEqual(true); + }); +}); diff --git a/app/reducers/server.js b/app/reducers/server.ts similarity index 76% rename from app/reducers/server.js rename to app/reducers/server.ts index 14c7bbfdf..02a7ffede 100644 --- a/app/reducers/server.js +++ b/app/reducers/server.ts @@ -1,6 +1,18 @@ +import { TActionServer } from '../actions/server'; import { SERVER } from '../actions/actionsTypes'; -const initialState = { +export interface IServer { + connecting: boolean; + connected: boolean; + failure: boolean; + server: string; + version: string | null; + loading: boolean; + previousServer: string | null; + changingServer: boolean; +} + +export const initialState: IServer = { connecting: false, connected: false, failure: false, @@ -11,7 +23,7 @@ const initialState = { changingServer: false }; -export default function server(state = initialState, action) { +export default function server(state = initialState, action: TActionServer): IServer { switch (action.type) { case SERVER.REQUEST: return {