Merge pull request #3623 from RocketChat/chore/migration-ts-redux-roles

Chore: Migrate redux module roles to typescript
This commit is contained in:
Gleidson Daniel Silva 2022-01-26 10:39:12 -03:00 committed by GitHub
commit c6c13f4d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 24 deletions

View File

@ -1,20 +0,0 @@
import * as types from './actionsTypes';
export function setRoles(roles) {
return {
type: types.ROLES.SET,
roles
};
}
export function updateRoles(id, desc) {
return {
type: types.ROLES.UPDATE,
payload: { id, desc }
};
}
export function removeRoles(id) {
return {
type: types.ROLES.REMOVE,
payload: { id }
};
}

39
app/actions/roles.ts Normal file
View File

@ -0,0 +1,39 @@
import { Action } from 'redux';
import { IRoles } from '../reducers/roles';
import { ROLES } from './actionsTypes';
export interface ISetRoles extends Action {
roles: IRoles;
}
export interface IUpdateRoles extends Action {
payload: { id: string; desc: string };
}
export interface IRemoveRoles extends Action {
payload: { id: string };
}
export type IActionRoles = ISetRoles & IUpdateRoles & IRemoveRoles;
export function setRoles(roles: IRoles): ISetRoles {
return {
type: ROLES.SET,
roles
};
}
export function updateRoles(id: string, desc: string): IUpdateRoles {
return {
type: ROLES.UPDATE,
payload: { id, desc }
};
}
export function removeRoles(id: string): IRemoveRoles {
return {
type: ROLES.REMOVE,
payload: { id }
};
}

View File

@ -1,8 +1,10 @@
import { TActionActiveUsers } from '../../actions/activeUsers';
import { IActionRoles } from '../../actions/roles';
import { TActionSelectedUsers } from '../../actions/selectedUsers';
import { IActionSettings } from '../../actions/settings';
// REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers';
import { IRoles } from '../../reducers/roles';
import { ISelectedUsers } from '../../reducers/selectedUsers';
import { ISettings } from '../../reducers/settings';
@ -27,7 +29,7 @@ export interface IApplicationState {
enterpriseModules: any;
encryption: any;
permissions: any;
roles: any;
roles: IRoles;
}
export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionSettings;
export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionRoles & IActionSettings;

View File

@ -0,0 +1,35 @@
import { setRoles, updateRoles, removeRoles } from '../actions/roles';
import { mockedStore } from './mockedStore';
import { initialState } from './roles';
describe('test roles reducer', () => {
it('should return initial state', () => {
const state = mockedStore.getState().roles;
expect(state).toEqual(initialState);
});
it('should return modified store after call setRoles action', () => {
const roles = { admin: 'enabled', user: 'enabled', dog: 'carlitos' };
mockedStore.dispatch(setRoles(roles));
const state = mockedStore.getState().roles;
expect(state.admin).toEqual('enabled');
expect(state.user).toEqual('enabled');
expect(state.dog).toEqual('carlitos');
});
it('should return modified store after call updateRoles action', () => {
mockedStore.dispatch(updateRoles('admin', 'disabled'));
const state = mockedStore.getState().roles;
expect(state.admin).toEqual('disabled');
expect(state.user).toEqual('enabled');
expect(state.dog).toEqual('carlitos');
});
it('should return modified store after call removeRoles action', () => {
mockedStore.dispatch(removeRoles('dog'));
const state = mockedStore.getState().roles;
expect(state.admin).toEqual('disabled');
expect(state.user).toEqual('enabled');
expect(state.dog).toEqual(undefined);
});
});

View File

@ -1,8 +1,11 @@
import { ROLES } from '../actions/actionsTypes';
import { IActionRoles } from '../actions/roles';
const initialState = {};
export type IRoles = Record<string, string>;
export default function permissions(state = initialState, action) {
export const initialState: IRoles = {};
export default function roles(state = initialState, action: IActionRoles): IRoles {
switch (action.type) {
case ROLES.SET:
return action.roles;