From a75d372026c27e4a64f70572b322dc34c5983af9 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Tue, 18 Jan 2022 11:59:43 -0400 Subject: [PATCH 1/8] Migrate redux server action/reducer to ts --- app/actions/server.js | 60 ------------------ app/actions/server.ts | 90 +++++++++++++++++++++++++++ app/definitions/redux/index.ts | 7 ++- app/reducers/{server.js => server.ts} | 18 +++++- 4 files changed, 110 insertions(+), 65 deletions(-) delete mode 100644 app/actions/server.js create mode 100644 app/actions/server.ts rename app/reducers/{server.js => server.ts} (73%) diff --git a/app/actions/server.js b/app/actions/server.js deleted file mode 100644 index fea23945..00000000 --- a/app/actions/server.js +++ /dev/null @@ -1,60 +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 - }; -} - -export function serverRequest(server, username = null, fromServerHistory = false) { - return { - type: SERVER.REQUEST, - server, - username, - 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 00000000..f635e412 --- /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 = 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 e95763e2..2ba1c7e0 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,14 +1,17 @@ +// ACTIONS import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionActiveUsers } from '../../actions/activeUsers'; +import { TActionServer } from '../../actions/server'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; +import { IServer } from '../../reducers/server'; export interface IApplicationState { settings: any; login: any; meteor: any; - server: any; + server: IServer; selectedUsers: ISelectedUsers; createChannel: any; app: any; @@ -28,4 +31,4 @@ export interface IApplicationState { roles: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers; +export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionServer; diff --git a/app/reducers/server.js b/app/reducers/server.ts similarity index 73% rename from app/reducers/server.js rename to app/reducers/server.ts index 14c7bbfd..906fe414 100644 --- a/app/reducers/server.js +++ b/app/reducers/server.ts @@ -1,6 +1,18 @@ +import { TApplicationActions } from '../definitions'; 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; +} + +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: TApplicationActions): IServer { switch (action.type) { case SERVER.REQUEST: return { @@ -34,7 +46,7 @@ export default function server(state = initialState, action) { connecting: true, connected: false, loading: true, - changingServer: action.changeServer + changingServer: action.changeServer! }; case SERVER.SELECT_SUCCESS: return { From 64780dee6f2b4ba665cbb10c8557de17636e7f58 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 19 Jan 2022 12:30:45 -0400 Subject: [PATCH 2/8] add tests --- app/reducers/server.test.ts | 26 ++++++++++++++++++++++++++ app/reducers/server.ts | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/reducers/server.test.ts diff --git a/app/reducers/server.test.ts b/app/reducers/server.test.ts new file mode 100644 index 00000000..e90d2cd9 --- /dev/null +++ b/app/reducers/server.test.ts @@ -0,0 +1,26 @@ +import { selectServerRequest, selectServerSuccess } 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 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 serverStr = 'https://mobile.rocket.chat/'; + const versionStr = '4.1.0'; + mockedStore.dispatch(selectServerSuccess(serverStr, versionStr)); + const { server, version } = mockedStore.getState().server; + expect(server).toEqual(serverStr); + expect(version).toEqual(versionStr); + }); +}); diff --git a/app/reducers/server.ts b/app/reducers/server.ts index 906fe414..5641ef03 100644 --- a/app/reducers/server.ts +++ b/app/reducers/server.ts @@ -12,7 +12,7 @@ export interface IServer { changingServer: boolean; } -const initialState: IServer = { +export const initialState: IServer = { connecting: false, connected: false, failure: false, From 89c844d79842b0ecf3cd6055efa589846cc3c3e4 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 20 Jan 2022 11:06:12 -0400 Subject: [PATCH 3/8] update tests --- app/reducers/server.test.ts | 56 ++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/app/reducers/server.test.ts b/app/reducers/server.test.ts index e90d2cd9..0b0f8fd4 100644 --- a/app/reducers/server.test.ts +++ b/app/reducers/server.test.ts @@ -1,4 +1,12 @@ -import { selectServerRequest, selectServerSuccess } from '../actions/server'; +import { + selectServerRequest, + serverRequest, + selectServerSuccess, + serverInitAdd, + serverFailure, + serverFinishAdd, + selectServerFailure +} from '../actions/server'; import { mockedStore } from './mockedStore'; import { initialState } from './server'; @@ -8,6 +16,21 @@ describe('test server reducer', () => { 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)); @@ -16,11 +39,30 @@ describe('test server reducer', () => { }); it('should return modified store after selectServerSucess', () => { - const serverStr = 'https://mobile.rocket.chat/'; - const versionStr = '4.1.0'; - mockedStore.dispatch(selectServerSuccess(serverStr, versionStr)); - const { server, version } = mockedStore.getState().server; - expect(server).toEqual(serverStr); - expect(version).toEqual(versionStr); + 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); }); }); From 2bcf2ef495f088265286710326f7913c8abcc34c Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 20 Jan 2022 17:00:04 -0400 Subject: [PATCH 4/8] update interface --- app/actions/server.ts | 4 ++-- app/reducers/server.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/actions/server.ts b/app/actions/server.ts index f635e412..3661892d 100644 --- a/app/actions/server.ts +++ b/app/actions/server.ts @@ -5,8 +5,8 @@ import { SERVER } from './actionsTypes'; interface ISelectServer extends Action { server: string; version?: string; - fetchVersion?: boolean; - changeServer?: boolean; + fetchVersion: boolean; + changeServer: boolean; } interface ISelectServerSuccess extends Action { diff --git a/app/reducers/server.ts b/app/reducers/server.ts index 5641ef03..551c4fba 100644 --- a/app/reducers/server.ts +++ b/app/reducers/server.ts @@ -46,7 +46,7 @@ export default function server(state = initialState, action: TApplicationActions connecting: true, connected: false, loading: true, - changingServer: action.changeServer! + changingServer: action.changeServer }; case SERVER.SELECT_SUCCESS: return { From 757b2c739bb43703dd3143627ba50aef3070c5db Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 20 Jan 2022 17:07:11 -0400 Subject: [PATCH 5/8] update action definition --- app/reducers/server.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/reducers/server.ts b/app/reducers/server.ts index 551c4fba..02a7ffed 100644 --- a/app/reducers/server.ts +++ b/app/reducers/server.ts @@ -1,4 +1,4 @@ -import { TApplicationActions } from '../definitions'; +import { TActionServer } from '../actions/server'; import { SERVER } from '../actions/actionsTypes'; export interface IServer { @@ -23,7 +23,7 @@ export const initialState: IServer = { changingServer: false }; -export default function server(state = initialState, action: TApplicationActions): IServer { +export default function server(state = initialState, action: TActionServer): IServer { switch (action.type) { case SERVER.REQUEST: return { From 66968b46fe237b1710fc07beeb76c3332d464167 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 26 Jan 2022 10:20:23 -0400 Subject: [PATCH 6/8] Fix lint --- app/actions/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/actions/server.ts b/app/actions/server.ts index 3661892d..da8ee6d4 100644 --- a/app/actions/server.ts +++ b/app/actions/server.ts @@ -54,7 +54,7 @@ export function selectServerFailure(): Action { }; } -export function serverRequest(server: string, username = null, fromServerHistory = false): IServer { +export function serverRequest(server: string, username: string | null = null, fromServerHistory = false): IServer { return { type: SERVER.REQUEST, server, From ae31612343fe4bb3f6b3f8611b57d3cb6cc68f59 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 26 Jan 2022 15:35:28 -0400 Subject: [PATCH 7/8] test --- app/definitions/redux/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index 0489eeaf..525055f3 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -41,7 +41,7 @@ export interface IApplicationState { permissions: any; roles: IRoles; } - +console.log('test'); export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionCustomEmojis & From b594488ad993b8b63cacb5deb97a47855eaf73d1 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 27 Jan 2022 09:21:46 -0400 Subject: [PATCH 8/8] Remove console.log --- app/definitions/redux/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index 525055f3..0489eeaf 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -41,7 +41,7 @@ export interface IApplicationState { permissions: any; roles: IRoles; } -console.log('test'); + export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionCustomEmojis &