From 8d22b8425342023eabbf5c587d399aeab4fd8bc9 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Mon, 17 Jan 2022 15:16:24 -0300 Subject: [PATCH] chore: migrate redux module roles to typescript --- app/actions/roles.js | 20 --------------- app/actions/roles.ts | 39 +++++++++++++++++++++++++++++ app/definitions/redux/index.ts | 5 ++-- app/reducers/roles.test.ts | 35 ++++++++++++++++++++++++++ app/reducers/{roles.js => roles.ts} | 7 ++++-- 5 files changed, 82 insertions(+), 24 deletions(-) delete mode 100644 app/actions/roles.js create mode 100644 app/actions/roles.ts create mode 100644 app/reducers/roles.test.ts rename app/reducers/{roles.js => roles.ts} (63%) diff --git a/app/actions/roles.js b/app/actions/roles.js deleted file mode 100644 index 8ee30425b..000000000 --- a/app/actions/roles.js +++ /dev/null @@ -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 } - }; -} diff --git a/app/actions/roles.ts b/app/actions/roles.ts new file mode 100644 index 000000000..3dbfa370d --- /dev/null +++ b/app/actions/roles.ts @@ -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 } + }; +} diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index e95763e29..36d7c0ea0 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,5 +1,6 @@ -import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionActiveUsers } from '../../actions/activeUsers'; +import { IActionRoles } from '../../actions/roles'; +import { TActionSelectedUsers } from '../../actions/selectedUsers'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; @@ -28,4 +29,4 @@ export interface IApplicationState { roles: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers; +export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionRoles; diff --git a/app/reducers/roles.test.ts b/app/reducers/roles.test.ts new file mode 100644 index 000000000..8348a8712 --- /dev/null +++ b/app/reducers/roles.test.ts @@ -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); + }); +}); diff --git a/app/reducers/roles.js b/app/reducers/roles.ts similarity index 63% rename from app/reducers/roles.js rename to app/reducers/roles.ts index 93cbffcb0..fc4f11248 100644 --- a/app/reducers/roles.js +++ b/app/reducers/roles.ts @@ -1,8 +1,11 @@ import { ROLES } from '../actions/actionsTypes'; +import { IActionRoles } from '../actions/roles'; -const initialState = {}; +export type IRoles = Record; -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;