chore: migrate redux module roles to typescript
This commit is contained in:
parent
2946c4724e
commit
8d22b84253
|
@ -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,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;
|
||||
|
|
|
@ -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 { 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;
|
Loading…
Reference in New Issue