Add tests

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-06-11 17:41:49 +02:00 committed by Alexandre Bodin
parent 467d77cbaa
commit 2fd80d7de4
4 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import init from '../init';
describe('UPLOAD | hooks | useUserPermissions | init', () => {
it('should return the initialState and the allowedActions', () => {
const initialState = {
isLoading: true,
};
const expected = {
isLoading: true,
allowedActions: {},
};
expect(init(initialState, [])).toEqual(expected);
});
it('should return an object with the allowedActions set properly', () => {
const initialState = {
isLoading: true,
};
const expected = {
isLoading: true,
allowedActions: {
canRead: false,
canUpdate: false,
},
};
const data = ['read', 'update'];
expect(init(initialState, data)).toEqual(expected);
});
});

View File

@ -0,0 +1,36 @@
import reducer from '../reducer';
describe('UPLOAD | hooks | useUserPermissions | reducer', () => {
describe('DEFAULT_ACTION', () => {
it('should return the initialState', () => {
const initialState = {
ok: true,
};
expect(reducer(initialState, { type: '' })).toEqual(initialState);
});
});
describe('GET_DATA_SUCCEEDED', () => {
it('should set the data correctly', () => {
const initialState = {
isLoading: true,
allowedActions: {},
};
const action = {
type: 'GET_DATA_SUCCEEDED',
data: {
canRead: false,
},
};
const expected = {
allowedActions: {
canRead: false,
},
isLoading: false,
};
expect(reducer(initialState, action)).toEqual(expected);
});
});
});

View File

@ -0,0 +1,10 @@
import { upperFirst } from 'lodash';
const generateResultsObject = array =>
array.reduce((acc, current) => {
acc[`can${upperFirst(current.permissionName)}`] = current.hasPermission;
return acc;
}, {});
export default generateResultsObject;

View File

@ -0,0 +1,16 @@
import generateResultsObject from '../generateResultsObject';
describe('UPLOAD | hooks | useUserPermissions | | utils | generateResultsObject', () => {
it('should return an object with { key: can<PermissionName>, value: bool }', () => {
const data = [
{ permissionName: 'read', hasPermission: true },
{ permissionName: 'update', hasPermission: false },
];
const expected = {
canRead: true,
canUpdate: false,
};
expect(generateResultsObject(data)).toEqual(expected);
});
});