chore: migrate redux module roles to typescript

This commit is contained in:
GleidsonDaniel 2022-01-17 15:16:24 -03:00
parent 2946c4724e
commit 8d22b84253
5 changed files with 82 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,5 +1,6 @@
import { TActionSelectedUsers } from '../../actions/selectedUsers';
import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionActiveUsers } from '../../actions/activeUsers';
import { IActionRoles } from '../../actions/roles';
import { TActionSelectedUsers } from '../../actions/selectedUsers';
// REDUCERS // REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers'; import { IActiveUsers } from '../../reducers/activeUsers';
import { ISelectedUsers } from '../../reducers/selectedUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers';
@ -28,4 +29,4 @@ export interface IApplicationState {
roles: any; roles: any;
} }
export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers; export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionRoles;

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 { 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;