Merge pull request #3623 from RocketChat/chore/migration-ts-redux-roles
Chore: Migrate redux module roles to typescript
This commit is contained in:
commit
c6c13f4d3c
|
@ -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 }
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -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 }
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
import { TActionActiveUsers } from '../../actions/activeUsers';
|
import { TActionActiveUsers } from '../../actions/activeUsers';
|
||||||
|
import { IActionRoles } from '../../actions/roles';
|
||||||
import { TActionSelectedUsers } from '../../actions/selectedUsers';
|
import { TActionSelectedUsers } from '../../actions/selectedUsers';
|
||||||
import { IActionSettings } from '../../actions/settings';
|
import { IActionSettings } from '../../actions/settings';
|
||||||
// REDUCERS
|
// REDUCERS
|
||||||
import { IActiveUsers } from '../../reducers/activeUsers';
|
import { IActiveUsers } from '../../reducers/activeUsers';
|
||||||
|
import { IRoles } from '../../reducers/roles';
|
||||||
import { ISelectedUsers } from '../../reducers/selectedUsers';
|
import { ISelectedUsers } from '../../reducers/selectedUsers';
|
||||||
import { ISettings } from '../../reducers/settings';
|
import { ISettings } from '../../reducers/settings';
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ export interface IApplicationState {
|
||||||
enterpriseModules: any;
|
enterpriseModules: any;
|
||||||
encryption: any;
|
encryption: any;
|
||||||
permissions: any;
|
permissions: any;
|
||||||
roles: any;
|
roles: IRoles;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionSettings;
|
export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionRoles & IActionSettings;
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,8 +1,11 @@
|
||||||
import { ROLES } from '../actions/actionsTypes';
|
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) {
|
switch (action.type) {
|
||||||
case ROLES.SET:
|
case ROLES.SET:
|
||||||
return action.roles;
|
return action.roles;
|
Loading…
Reference in New Issue