Adds more testing for Urls

This commit is contained in:
Alex Corey
2020-05-08 12:03:06 -04:00
parent 68d56d5616
commit 2cbcbddc52
6 changed files with 93 additions and 30 deletions

View File

@@ -27,12 +27,12 @@ class Users extends Base {
readRoles(userId, params) { readRoles(userId, params) {
return this.http.get(`${this.baseUrl}${userId}/roles/`, { return this.http.get(`${this.baseUrl}${userId}/roles/`, {
params params,
}); });
} }
roleOptions(userId) { readRoleOptions(userId) {
return this.http.options(`${this.baseUrl}${userId}/roles/`) return this.http.options(`${this.baseUrl}${userId}/roles/`);
} }
} }

View File

@@ -31,12 +31,17 @@ function UserAccessList({ i18n }) {
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const params = parseQueryString(QS_CONFIG, search); const params = parseQueryString(QS_CONFIG, search);
const { const [
data: { results, count }, {
} = await UsersAPI.readRoles(id, params); data: { results, count },
const { },
data: { actions }, {
} = await UsersAPI.roleOptions(id); data: { actions },
},
] = await Promise.all([
UsersAPI.readRoles(id, params),
UsersAPI.readRoleOptions(id),
]);
return { roleCount: count, roles: results, options: actions }; return { roleCount: count, roles: results, options: actions };
}, [id, search]), }, [id, search]),
{ {
@@ -76,8 +81,8 @@ function UserAccessList({ i18n }) {
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
toolbarSearchColumns={[ toolbarSearchColumns={[
{ {
name: i18n._(t`Type`), name: i18n._(t`Role`),
key: 'content_type__search', key: 'role_field',
isDefault: true, isDefault: true,
}, },
]} ]}

View File

@@ -3,12 +3,14 @@ import { act } from 'react-dom/test-utils';
import { createMemoryHistory } from 'history'; import { createMemoryHistory } from 'history';
import { UsersAPI } from '@api'; import { UsersAPI } from '@api';
import { Route } from 'react-router-dom'; import { Route } from 'react-router-dom';
import { mountWithContexts } from '@testUtils/enzymeHelpers'; import { mountWithContexts, waitForElement } from '@testUtils/enzymeHelpers';
import UserAccessList from './UserAccessList'; import UserAccessList from './UserAccessList';
jest.mock('@api/models/Users'); jest.mock('@api/models/Users');
describe('<UserAccessList />', () => { describe('<UserAccessList />', () => {
test('should render properly', async () => { let wrapper;
let history;
beforeEach(async () => {
UsersAPI.readRoles.mockResolvedValue({ UsersAPI.readRoles.mockResolvedValue({
data: { data: {
results: [ results: [
@@ -27,28 +29,66 @@ describe('<UserAccessList />', () => {
}, },
{ {
id: 3, id: 3,
name: 'Update', name: 'Admin',
type: 'role',
url: '/api/v2/roles/257/',
summary_fields: {
resource_name: 'template delete project',
resource_id: 16,
resource_type: 'workflow_job_template',
resource_type_display_name: 'Job Template',
user_capabilities: { unattach: true },
},
},
{
id: 4,
name: 'Execute',
type: 'role', type: 'role',
url: '/api/v2/roles/258/', url: '/api/v2/roles/258/',
summary_fields: { summary_fields: {
resource_name: 'Foo Bar', resource_name: 'Credential Bar',
resource_id: 75, resource_id: 75,
resource_type: 'credential', resource_type: 'credential',
resource_type_display_name: 'Credential', resource_type_display_name: 'Credential',
user_capabilities: { unattach: true }, user_capabilities: { unattach: true },
}, },
}, },
{
id: 5,
name: 'Update',
type: 'role',
url: '/api/v2/roles/259/',
summary_fields: {
resource_name: 'Inventory Foo',
resource_id: 76,
resource_type: 'inventory',
resource_type_display_name: 'Inventory',
user_capabilities: { unattach: true },
},
},
{
id: 6,
name: 'Admin',
type: 'role',
url: '/api/v2/roles/260/',
summary_fields: {
resource_name: 'Smart Inventory Foo',
resource_id: 77,
resource_type: 'smart_inventory',
resource_type_display_name: 'Inventory',
user_capabilities: { unattach: true },
},
},
], ],
count: 2, count: 4,
}, },
}); });
UsersAPI.roleOptions.mockResolvedValue({ UsersAPI.readRoleOptions.mockResolvedValue({
data: { actions: { POST: { id: 1, disassociate: true } } }, data: { actions: { POST: { id: 1, disassociate: true } } },
}); });
let wrapper; history = createMemoryHistory({
const history = createMemoryHistory({
initialEntries: ['/users/18/access'], initialEntries: ['/users/18/access'],
}); });
@@ -70,7 +110,32 @@ describe('<UserAccessList />', () => {
} }
); );
}); });
});
afterEach(() => {
jest.clearAllMocks();
wrapper.unmount();
});
test('should render properly', async () => {
expect(wrapper.find('UserAccessList').length).toBe(1); expect(wrapper.find('UserAccessList').length).toBe(1);
}); });
test('should create proper detailUrl', async () => {
waitForElement(wrapper, 'ContentEmpty', el => el.length === 0);
expect(wrapper.find(`Link#userRole-2`).prop('to')).toBe(
'/templates/job_template/15/details'
);
expect(wrapper.find(`Link#userRole-3`).prop('to')).toBe(
'/templates/workflow_job_template/16/details'
);
expect(wrapper.find('Link#userRole-4').prop('to')).toBe(
'/credentials/75/details'
);
expect(wrapper.find('Link#userRole-5').prop('to')).toBe(
'/inventories/inventory/76/details'
);
expect(wrapper.find('Link#userRole-6').prop('to')).toBe(
'/inventories/smart_inventory/77/details'
);
});
}); });

View File

@@ -11,7 +11,7 @@ import DataListCell from '@components/DataListCell';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
function UserAccessListItem({ role, i18n, detailUrl }) { function UserAccessListItem({ role, i18n, detailUrl }) {
const labelId = `check-action-${role.id}`; const labelId = `userRole-${role.id}`;
return ( return (
<DataListItem key={role.id} aria-labelledby={labelId} id={`${role.id}`}> <DataListItem key={role.id} aria-labelledby={labelId} id={`${role.id}`}>
<DataListItemRow> <DataListItemRow>

View File

@@ -35,7 +35,6 @@ describe('<UserAccessListItem/>', () => {
expect( expect(
wrapper.find('PFDataListCell[aria-label="resource name"]').text() wrapper.find('PFDataListCell[aria-label="resource name"]').text()
).toBe('template delete project'); ).toBe('template delete project');
console.log(wrapper.debug());
expect( expect(
wrapper.find('PFDataListCell[aria-label="resource type"]').text() wrapper.find('PFDataListCell[aria-label="resource type"]').text()
).toContain('Job Template'); ).toContain('Job Template');

View File

@@ -1,8 +1,2 @@
export { export { default as UserAccessListList } from './UserAccessList';
default as UserAccessListList export { default as UserAccessListItem } from './UserAccessListItem';
}
from './UserAccessList';
export {
default as UserAccessListItem
}
from './UserAccessListItem';