From 03459147ed99242c16027dfe55a3e4fb3c47826f Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Wed, 2 Feb 2022 14:01:31 -0300 Subject: [PATCH] Chore: Migrate redux module permissions to typescript (#3630) --- app/actions/permissions.js | 15 ---------- app/actions/permissions.ts | 28 +++++++++++++++++++ app/reducers/permissions.test.ts | 23 +++++++++++++++ .../{permissions.js => permissions.ts} | 7 +++-- 4 files changed, 56 insertions(+), 17 deletions(-) delete mode 100644 app/actions/permissions.js create mode 100644 app/actions/permissions.ts create mode 100644 app/reducers/permissions.test.ts rename app/reducers/{permissions.js => permissions.ts} (63%) diff --git a/app/actions/permissions.js b/app/actions/permissions.js deleted file mode 100644 index 444d9f11c..000000000 --- a/app/actions/permissions.js +++ /dev/null @@ -1,15 +0,0 @@ -import * as types from './actionsTypes'; - -export function setPermissions(permissions) { - return { - type: types.PERMISSIONS.SET, - permissions - }; -} - -export function updatePermission(id, roles) { - return { - type: types.PERMISSIONS.UPDATE, - payload: { id, roles } - }; -} diff --git a/app/actions/permissions.ts b/app/actions/permissions.ts new file mode 100644 index 000000000..d47118fce --- /dev/null +++ b/app/actions/permissions.ts @@ -0,0 +1,28 @@ +import { Action } from 'redux'; + +import { IPermissions } from '../reducers/permissions'; +import { PERMISSIONS } from './actionsTypes'; + +interface ISetPermissions extends Action { + permissions: IPermissions; +} + +interface IUpdatePermissions extends Action { + payload: { id: string; roles: string }; +} + +export type TActionPermissions = ISetPermissions & IUpdatePermissions; + +export function setPermissions(permissions: IPermissions): ISetPermissions { + return { + type: PERMISSIONS.SET, + permissions + }; +} + +export function updatePermission(id: string, roles: string): IUpdatePermissions { + return { + type: PERMISSIONS.UPDATE, + payload: { id, roles } + }; +} diff --git a/app/reducers/permissions.test.ts b/app/reducers/permissions.test.ts new file mode 100644 index 000000000..8980a4428 --- /dev/null +++ b/app/reducers/permissions.test.ts @@ -0,0 +1,23 @@ +import { setPermissions, updatePermission } from '../actions/permissions'; +import { mockedStore } from './mockedStore'; +import { initialState } from './permissions'; + +describe('test permissions reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().permissions; + expect(state).toEqual(initialState); + }); + + it('should return modified store after setPermissions', () => { + const permissions = { hasEditPermission: 'enabled', hasForceDeletePermission: 'enabled' }; + mockedStore.dispatch(setPermissions(permissions)); + const state = mockedStore.getState().permissions; + expect(state).toEqual(permissions); + }); + + it('should return empty store after remove user', () => { + mockedStore.dispatch(updatePermission('hasEditPermission', 'disabled')); + const state = mockedStore.getState().permissions; + expect(state.hasEditPermission).toEqual('disabled'); + }); +}); diff --git a/app/reducers/permissions.js b/app/reducers/permissions.ts similarity index 63% rename from app/reducers/permissions.js rename to app/reducers/permissions.ts index 034c51420..a80a4e87e 100644 --- a/app/reducers/permissions.js +++ b/app/reducers/permissions.ts @@ -1,8 +1,11 @@ import { PERMISSIONS } from '../actions/actionsTypes'; +import { TActionPermissions } from '../actions/permissions'; -const initialState = {}; +export type IPermissions = Record; -export default function permissions(state = initialState, action) { +export const initialState: IPermissions = {}; + +export default function permissions(state = initialState, action: TActionPermissions): IPermissions { switch (action.type) { case PERMISSIONS.SET: return action.permissions;