Merge pull request #3627 from RocketChat/chore.migrate-redux-server-to-ts

Chore: Migrate redux module server to TypeScript
This commit is contained in:
Gerzon Z 2022-02-01 11:41:50 -04:00 committed by GitHub
commit 9773fc1799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 178 additions and 65 deletions

View File

@ -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
};
}

90
app/actions/server.ts Normal file
View File

@ -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
};
}

View File

@ -1,3 +1,5 @@
// ACTIONS
import { TActionServer } from '../../actions/server';
import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionActiveUsers } from '../../actions/activeUsers';
import { TActionCustomEmojis } from '../../actions/customEmojis'; import { TActionCustomEmojis } from '../../actions/customEmojis';
import { TActionEncryption } from '../../actions/encryption'; import { TActionEncryption } from '../../actions/encryption';
@ -13,6 +15,7 @@ import { IEncryption } from '../../reducers/encryption';
import { IInviteLinks } from '../../reducers/inviteLinks'; import { IInviteLinks } from '../../reducers/inviteLinks';
import { IRoles } from '../../reducers/roles'; import { IRoles } from '../../reducers/roles';
import { ISelectedUsers } from '../../reducers/selectedUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers';
import { IServer } from '../../reducers/server';
import { IConnect } from '../../reducers/connect'; import { IConnect } from '../../reducers/connect';
import { ISettings } from '../../reducers/settings'; import { ISettings } from '../../reducers/settings';
@ -20,7 +23,7 @@ export interface IApplicationState {
settings: ISettings; settings: ISettings;
login: any; login: any;
meteor: IConnect; meteor: IConnect;
server: any; server: IServer;
selectedUsers: ISelectedUsers; selectedUsers: ISelectedUsers;
createChannel: any; createChannel: any;
app: any; app: any;
@ -48,4 +51,5 @@ export type TApplicationActions = TActionActiveUsers &
IActionSettings & IActionSettings &
TActionEncryption & TActionEncryption &
TActionSortPreferences & TActionSortPreferences &
TActionUserTyping; TActionUserTyping &
TActionServer;

View File

@ -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);
});
});

View File

@ -1,6 +1,18 @@
import { TActionServer } from '../actions/server';
import { SERVER } from '../actions/actionsTypes'; 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, connecting: false,
connected: false, connected: false,
failure: false, failure: false,
@ -11,7 +23,7 @@ const initialState = {
changingServer: false changingServer: false
}; };
export default function server(state = initialState, action) { export default function server(state = initialState, action: TActionServer): IServer {
switch (action.type) { switch (action.type) {
case SERVER.REQUEST: case SERVER.REQUEST:
return { return {