Skip to content

Commit

Permalink
Added roles actions and reducers.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed Apr 8, 2018
1 parent a75d08f commit 15e228d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* Schema widget @robgietema
* User actions and reducers @robgietema
* Group actions and reducers @robgietema
* Roles actions and reducers @robgietema

### Changes

Expand Down
1 change: 1 addition & 0 deletions src/actions/index.js
Expand Up @@ -36,6 +36,7 @@ export {
} from './groups/groups';
export { addMessage, removeMessage, purgeMessages } from './messages/messages';
export getNavigation from './navigation/navigation';
export listRoles from './roles/roles';
export getSchema from './schema/schema';
export getTiles from './tiles/tiles';
export getTypes from './types/types';
Expand Down
18 changes: 18 additions & 0 deletions src/actions/roles/roles.js
@@ -0,0 +1,18 @@
/**
* Roles actions.
* @module actions/roles/roles
*/

import { LIST_ROLES } from '../../constants/ActionTypes';

/**
* List roles.
* @function listRoles
* @returns {Object} List roles action.
*/
export default function listRoles() {
return {
type: LIST_ROLES,
promise: api => api.get('/@roles'),
};
}
19 changes: 19 additions & 0 deletions src/actions/roles/roles.test.js
@@ -0,0 +1,19 @@
import listRoles from './roles';
import { LIST_ROLES } from '../../constants/ActionTypes';

describe('Roles action', () => {
describe('listRoles', () => {
it('should create an action to get the breadcrumbs', () => {
const action = listRoles();

expect(action.type).toEqual(LIST_ROLES);

const apiMock = {
get: jest.fn(),
};
action.promise(apiMock);

expect(apiMock.get).toBeCalledWith('/@roles');
});
});
});
1 change: 1 addition & 0 deletions src/constants/ActionTypes.js
Expand Up @@ -44,6 +44,7 @@ export const GET_VOCABULARY = 'GET_VOCABULARY';
export const GET_WORKFLOW = 'GET_WORKFLOW';
export const GET_WORKFLOW_MULTIPLE = 'GET_WORKFLOW_MULTIPLE';
export const LIST_GROUPS = 'LIST_GROUPS';
export const LIST_ROLES = 'LIST_ROLES';
export const LOGIN = 'LOGIN';
export const LOGIN_RENEW = 'LOGIN_RENEW';
export const LOGOUT = 'LOGOUT';
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/index.js
Expand Up @@ -22,6 +22,7 @@ import history from './history/history';
import groups from './groups/groups';
import messages from './messages/messages';
import navigation from './navigation/navigation';
import roles from './roles/roles';
import schema from './schema/schema';
import search from './search/search';
import sharing from './sharing/sharing';
Expand Down Expand Up @@ -57,6 +58,7 @@ export default combineReducers({
history,
messages,
navigation,
roles,
schema,
search,
sharing,
Expand Down
50 changes: 50 additions & 0 deletions src/reducers/roles/roles.js
@@ -0,0 +1,50 @@
/**
* Roles reducer.
* @module reducers/roles/roles
*/

import { LIST_ROLES } from '../../constants/ActionTypes';

const initialState = {
error: null,
roles: [],
loaded: false,
loading: false,
};

/**
* Roles reducer.
* @function roles
* @param {Object} state Current state.
* @param {Object} action Action to be handled.
* @returns {Object} New state.
*/
export default function roles(state = initialState, action = {}) {
switch (action.type) {
case `${LIST_ROLES}_PENDING`:
return {
...state,
error: null,
loaded: false,
loading: true,
};
case `${LIST_ROLES}_SUCCESS`:
return {
...state,
error: null,
roles: action.result,
loaded: true,
loading: false,
};
case `${LIST_ROLES}_FAIL`:
return {
...state,
error: action.error,
roles: [],
loaded: false,
loading: false,
};
default:
return state;
}
}
54 changes: 54 additions & 0 deletions src/reducers/roles/roles.test.js
@@ -0,0 +1,54 @@
import roles from './roles';
import { LIST_ROLES } from '../../constants/ActionTypes';

describe('Roles reducer', () => {
it('should return the initial state', () => {
expect(roles()).toEqual({
error: null,
roles: [],
loaded: false,
loading: false,
});
});

it('should handle LIST_ROLES_PENDING', () => {
expect(
roles(undefined, {
type: `${LIST_ROLES}_PENDING`,
}),
).toEqual({
error: null,
roles: [],
loaded: false,
loading: true,
});
});

it('should handle LIST_ROLES_SUCCESS', () => {
expect(
roles(undefined, {
type: `${LIST_ROLES}_SUCCESS`,
result: 'roles',
}),
).toEqual({
error: null,
roles: 'roles',
loaded: true,
loading: false,
});
});

it('should handle LIST_ROLES_FAIL', () => {
expect(
roles(undefined, {
type: `${LIST_ROLES}_FAIL`,
error: 'failed',
}),
).toEqual({
error: 'failed',
roles: [],
loaded: false,
loading: false,
});
});
});

0 comments on commit 15e228d

Please sign in to comment.