convert inventory source list to tables

This commit is contained in:
Keith J. Grant
2021-04-28 09:51:11 -07:00
parent 83ceacf588
commit cbe2a78287
4 changed files with 216 additions and 207 deletions

View File

@@ -9,7 +9,11 @@ import useRequest, {
} from '../../../util/useRequest'; } from '../../../util/useRequest';
import { getQSConfig, parseQueryString } from '../../../util/qs'; import { getQSConfig, parseQueryString } from '../../../util/qs';
import { InventoriesAPI, InventorySourcesAPI } from '../../../api'; import { InventoriesAPI, InventorySourcesAPI } from '../../../api';
import PaginatedDataList, { import PaginatedTable, {
HeaderRow,
HeaderCell,
} from '../../../components/PaginatedTable';
import {
ToolbarAddButton, ToolbarAddButton,
ToolbarDeleteButton, ToolbarDeleteButton,
} from '../../../components/PaginatedDataList'; } from '../../../components/PaginatedDataList';
@@ -148,7 +152,7 @@ function InventorySourceList() {
); );
return ( return (
<> <>
<PaginatedDataList <PaginatedTable
contentError={fetchError} contentError={fetchError}
hasContentLoading={ hasContentLoading={
isLoading || isLoading ||
@@ -208,21 +212,27 @@ function InventorySourceList() {
]} ]}
/> />
)} )}
renderItem={inventorySource => { headerRow={
let label; <HeaderRow qsConfig={QS_CONFIG}>
sourceChoices.forEach(([scMatch, scLabel]) => { <HeaderCell sortKey="name">{t`Name`}</HeaderCell>
if (inventorySource.source === scMatch) { <HeaderCell>{t`Status`}</HeaderCell>
label = scLabel; <HeaderCell>{t`Type`}</HeaderCell>
} <HeaderCell>{t`Actions`}</HeaderCell>
}); </HeaderRow>
}
renderRow={(inventorySource, index) => {
const label = sourceChoices.find(
([scMatch]) => inventorySource.source === scMatch
);
return ( return (
<InventorySourceListItem <InventorySourceListItem
key={inventorySource.id} key={inventorySource.id}
source={inventorySource} source={inventorySource}
onSelect={() => handleSelect(inventorySource)} onSelect={() => handleSelect(inventorySource)}
label={label} label={label[1]}
detailUrl={`${listUrl}${inventorySource.id}`} detailUrl={`${listUrl}${inventorySource.id}`}
isSelected={selected.some(row => row.id === inventorySource.id)} isSelected={selected.some(row => row.id === inventorySource.id)}
rowIndex={index}
/> />
); );
}} }}

View File

@@ -7,10 +7,7 @@ import {
InventorySourcesAPI, InventorySourcesAPI,
WorkflowJobTemplateNodesAPI, WorkflowJobTemplateNodesAPI,
} from '../../../api'; } from '../../../api';
import { import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
mountWithContexts,
waitForElement,
} from '../../../../testUtils/enzymeHelpers';
import InventorySourceList from './InventorySourceList'; import InventorySourceList from './InventorySourceList';
@@ -108,18 +105,15 @@ describe('<InventorySourceList />', () => {
} }
); );
}); });
wrapper.update();
}); });
afterEach(() => { afterEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
global.console.debug = debug; global.console.debug = debug;
}); });
test('should mount properly', async () => {
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
});
test('api calls should be made on mount', async () => { test('api calls should be made on mount', async () => {
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
expect(InventoriesAPI.readSources).toHaveBeenCalledWith('1', { expect(InventoriesAPI.readSources).toHaveBeenCalledWith('1', {
not__source: '', not__source: '',
order_by: 'name', order_by: 'name',
@@ -136,23 +130,16 @@ describe('<InventorySourceList />', () => {
}); });
test('source data should render properly', async () => { test('source data should render properly', async () => {
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0); expect(wrapper.find('InventorySourceListItem')).toHaveLength(2);
expect( expect(
wrapper wrapper
.find("DataListItem[aria-labelledby='check-action-1']") .find('InventorySourceListItem')
.find('PFDataListCell[aria-label="name"]') .first()
.text() .prop('source')
).toBe('Source Foo'); ).toEqual(sources.data.results[0]);
expect(
wrapper
.find("DataListItem[aria-labelledby='check-action-1']")
.find('PFDataListCell[aria-label="type"]')
.text()
).toBe('EC2');
}); });
test('add button is not disabled and delete button is disabled', async () => { test('add button is not disabled and delete button is disabled', async () => {
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
const addButton = wrapper.find('ToolbarAddButton').find('Link'); const addButton = wrapper.find('ToolbarAddButton').find('Link');
const deleteButton = wrapper.find('ToolbarDeleteButton').find('Button'); const deleteButton = wrapper.find('ToolbarDeleteButton').find('Button');
expect(addButton.prop('aria-disabled')).toBe(false); expect(addButton.prop('aria-disabled')).toBe(false);
@@ -162,14 +149,23 @@ describe('<InventorySourceList />', () => {
test('delete button becomes enabled and properly calls api to delete', async () => { test('delete button becomes enabled and properly calls api to delete', async () => {
const deleteButton = wrapper.find('ToolbarDeleteButton').find('Button'); const deleteButton = wrapper.find('ToolbarDeleteButton').find('Button');
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
expect(deleteButton.prop('isDisabled')).toBe(true); expect(deleteButton.prop('isDisabled')).toBe(true);
await act(async () => await act(async () =>
wrapper.find('DataListCheck#select-source-1').prop('onChange')({ id: 1 }) wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('onChange')({ id: 1 })
); );
wrapper.update(); wrapper.update();
expect(wrapper.find('input#select-source-1').prop('checked')).toBe(true); expect(
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toBe(true);
await act(async () => await act(async () =>
wrapper.find('Button[aria-label="Delete"]').prop('onClick')() wrapper.find('Button[aria-label="Delete"]').prop('onClick')()
@@ -199,10 +195,12 @@ describe('<InventorySourceList />', () => {
}) })
); );
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
await act(async () => await act(async () =>
wrapper.find('DataListCheck#select-source-1').prop('onChange')({ id: 1 }) wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('onChange')({ id: 1 })
); );
wrapper.update(); wrapper.update();
@@ -249,8 +247,7 @@ describe('<InventorySourceList />', () => {
await act(async () => { await act(async () => {
wrapper = mountWithContexts(<InventorySourceList />); wrapper = mountWithContexts(<InventorySourceList />);
}); });
wrapper.update();
await waitForElement(wrapper, 'ContentError', el => el.length > 0);
expect(wrapper.find('ContentError').length).toBe(1); expect(wrapper.find('ContentError').length).toBe(1);
}); });
@@ -272,8 +269,7 @@ describe('<InventorySourceList />', () => {
await act(async () => { await act(async () => {
wrapper = mountWithContexts(<InventorySourceList />); wrapper = mountWithContexts(<InventorySourceList />);
}); });
wrapper.update();
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
expect(wrapper.find('ContentError').length).toBe(1); expect(wrapper.find('ContentError').length).toBe(1);
}); });
@@ -291,7 +287,6 @@ describe('<InventorySourceList />', () => {
}, },
}) })
); );
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
await act(async () => await act(async () =>
wrapper.find('Button[aria-label="Sync all"]').prop('onClick')() wrapper.find('Button[aria-label="Sync all"]').prop('onClick')()
); );
@@ -301,11 +296,6 @@ describe('<InventorySourceList />', () => {
}); });
test('should render sync all button and make api call to start sync for all', async () => { test('should render sync all button and make api call to start sync for all', async () => {
await waitForElement(
wrapper,
'InventorySourceListItem',
el => el.length > 0
);
const syncAllButton = wrapper.find('Button[aria-label="Sync all"]'); const syncAllButton = wrapper.find('Button[aria-label="Sync all"]');
expect(syncAllButton.length).toBe(1); expect(syncAllButton.length).toBe(1);
await act(async () => syncAllButton.prop('onClick')()); await act(async () => syncAllButton.prop('onClick')());
@@ -359,11 +349,7 @@ describe('<InventorySourceList /> RBAC testing', () => {
} }
); );
}); });
await waitForElement( newWrapper.update();
newWrapper,
'InventorySourceList',
el => el.length > 0
);
expect(newWrapper.find('ToolbarAddButton').length).toBe(0); expect(newWrapper.find('ToolbarAddButton').length).toBe(0);
jest.clearAllMocks(); jest.clearAllMocks();
}); });
@@ -398,11 +384,7 @@ describe('<InventorySourceList /> RBAC testing', () => {
} }
); );
}); });
await waitForElement( newWrapper.update();
newWrapper,
'InventorySourceList',
el => el.length > 0
);
expect(newWrapper.find('Button[aria-label="Sync All"]').length).toBe(0); expect(newWrapper.find('Button[aria-label="Sync All"]').length).toBe(0);
jest.clearAllMocks(); jest.clearAllMocks();
}); });

View File

@@ -1,23 +1,15 @@
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { import { Button, Tooltip } from '@patternfly/react-core';
Button, import { Tr, Td } from '@patternfly/react-table';
DataListItem,
DataListItemRow,
DataListCheck,
DataListItemCells,
DataListCell,
DataListAction,
Tooltip,
} from '@patternfly/react-core';
import { import {
ExclamationTriangleIcon as PFExclamationTriangleIcon, ExclamationTriangleIcon as PFExclamationTriangleIcon,
PencilAltIcon, PencilAltIcon,
} from '@patternfly/react-icons'; } from '@patternfly/react-icons';
import styled from 'styled-components'; import styled from 'styled-components';
import { ActionsTd, ActionItem } from '../../../components/PaginatedTable';
import StatusIcon from '../../../components/StatusIcon'; import StatusIcon from '../../../components/StatusIcon';
import InventorySourceSyncButton from '../shared/InventorySourceSyncButton'; import InventorySourceSyncButton from '../shared/InventorySourceSyncButton';
@@ -30,9 +22,9 @@ function InventorySourceListItem({
source, source,
isSelected, isSelected,
onSelect, onSelect,
detailUrl, detailUrl,
label, label,
rowIndex,
}) { }) {
const generateLastJobTooltip = job => { const generateLastJobTooltip = job => {
return ( return (
@@ -58,80 +50,67 @@ function InventorySourceListItem({
return ( return (
<> <>
<DataListItem aria-labelledby={`check-action-${source.id}`}> <Tr id={`source-row-${source.id}`}>
<DataListItemRow> <Td
<DataListCheck select={{
id={`select-source-${source.id}`} rowIndex,
checked={isSelected} isSelected,
onChange={onSelect} onSelect,
aria-labelledby={`check-action-${source.id}`} }}
/> />
<DataListItemCells <Td dataLabel={t`Name`}>
dataListCells={[ <Link to={`${detailUrl}/details`}>
<DataListCell key="status" isFilled={false}> <b>{source.name}</b>
{source.summary_fields.last_job && ( </Link>
<Tooltip {missingExecutionEnvironment && (
position="top" <span>
content={generateLastJobTooltip( <Tooltip
source.summary_fields.last_job className="missing-execution-environment"
)} content={t`Custom virtual environment ${source.custom_virtualenv} must be replaced by an execution environment.`}
key={source.summary_fields.last_job.id} position="right"
>
<Link
to={`/jobs/inventory/${source.summary_fields.last_job.id}`}
>
<StatusIcon
status={source.summary_fields.last_job.status}
/>
</Link>
</Tooltip>
)}
</DataListCell>,
<DataListCell aria-label={t`name`} key="name">
<span>
<Link to={`${detailUrl}/details`}>
<b>{source.name}</b>
</Link>
</span>
{missingExecutionEnvironment && (
<span>
<Tooltip
className="missing-execution-environment"
content={t`Custom virtual environment ${source.custom_virtualenv} must be replaced by an execution environment.`}
position="right"
>
<ExclamationTriangleIcon />
</Tooltip>
</span>
)}
</DataListCell>,
<DataListCell aria-label={t`type`} key="type">
{label}
</DataListCell>,
]}
/>
<DataListAction
id="actions"
aria-labelledby="actions"
aria-label={t`actions`}
>
{source.summary_fields.user_capabilities.start && (
<InventorySourceSyncButton source={source} />
)}
{source.summary_fields.user_capabilities.edit && (
<Button
ouiaId={`${source.id}-edit-button`}
aria-label={t`Edit Source`}
variant="plain"
component={Link}
to={`${detailUrl}/edit`}
> >
<PencilAltIcon /> <ExclamationTriangleIcon />
</Button> </Tooltip>
)} </span>
</DataListAction> )}
</DataListItemRow> </Td>
</DataListItem> <Td dataLabel={t`Status`}>
{source.summary_fields.last_job && (
<Tooltip
position="top"
content={generateLastJobTooltip(source.summary_fields.last_job)}
key={source.summary_fields.last_job.id}
>
<Link to={`/jobs/inventory/${source.summary_fields.last_job.id}`}>
<StatusIcon status={source.summary_fields.last_job.status} />
</Link>
</Tooltip>
)}
</Td>
<Td dataLabel={t`Type`}>{label}</Td>
<ActionsTd dataLabel={t`Actions`}>
<ActionItem
visible={source.summary_fields.user_capabilities.start}
tooltip={t`Sync`}
>
<InventorySourceSyncButton source={source} />
</ActionItem>
<ActionItem
visible={source.summary_fields.user_capabilities.edit}
tooltip={t`Edit`}
>
<Button
ouiaId={`${source.id}-edit-button`}
aria-label={t`Edit Source`}
variant="plain"
component={Link}
to={`${detailUrl}/edit`}
>
<PencilAltIcon />
</Button>
</ActionItem>
</ActionsTd>
</Tr>
</> </>
); );
} }

View File

@@ -26,15 +26,20 @@ describe('<InventorySourceListItem />', () => {
wrapper.unmount(); wrapper.unmount();
jest.clearAllMocks(); jest.clearAllMocks();
}); });
test('should mount properly', () => { test('should mount properly', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={source} <tbody>
isSelected={false} <InventorySourceListItem
onSelect={onSelect} source={source}
label="Source Bar" isSelected={false}
/> onSelect={onSelect}
label="Source Bar"
/>
</tbody>
</table>
); );
expect(wrapper.find('InventorySourceListItem').length).toBe(1); expect(wrapper.find('InventorySourceListItem').length).toBe(1);
}); });
@@ -42,32 +47,35 @@ describe('<InventorySourceListItem />', () => {
test('all buttons and text fields should render properly', () => { test('all buttons and text fields should render properly', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={source} <tbody>
isSelected={false} <InventorySourceListItem
onSelect={onSelect} source={source}
label="Source Bar" isSelected={false}
/> onSelect={onSelect}
label="Source Bar"
/>
</tbody>
</table>
); );
expect(wrapper.find('StatusIcon').length).toBe(1); expect(wrapper.find('StatusIcon').length).toBe(1);
expect( expect(
wrapper wrapper
.find('Link') .find('Link')
.at(0) .at(1)
.prop('to') .prop('to')
).toBe('/jobs/inventory/664'); ).toBe('/jobs/inventory/664');
expect(wrapper.find('DataListCheck').length).toBe(1); expect(wrapper.find('.pf-c-table__check').length).toBe(1);
expect();
expect( expect(
wrapper wrapper
.find('DataListCell') .find('Td')
.at(1) .at(1)
.text() .text()
).toBe('Foo'); ).toBe('Foo');
expect( expect(
wrapper wrapper
.find('DataListCell') .find('Td')
.at(2) .at(3)
.text() .text()
).toBe('Source Bar'); ).toBe('Source Bar');
expect(wrapper.find('InventorySourceSyncButton').length).toBe(1); expect(wrapper.find('InventorySourceSyncButton').length).toBe(1);
@@ -77,32 +85,46 @@ describe('<InventorySourceListItem />', () => {
test('item should be checked', () => { test('item should be checked', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={source} <tbody>
isSelected <InventorySourceListItem
onSelect={onSelect} source={source}
label="Source Bar" isSelected
/> onSelect={onSelect}
label="Source Bar"
/>
</tbody>
</table>
); );
expect(wrapper.find('DataListCheck').length).toBe(1); wrapper.update();
expect(wrapper.find('DataListCheck').prop('checked')).toBe(true); expect(wrapper.find('.pf-c-table__check').length).toBe(1);
expect(
wrapper
.find('Td')
.first()
.prop('select').isSelected
).toEqual(true);
}); });
test('should not render status icon', () => { test('should not render status icon', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={{ <tbody>
...source, <InventorySourceListItem
summary_fields: { source={{
user_capabilities: { start: true, edit: true }, ...source,
last_job: null, summary_fields: {
}, user_capabilities: { start: true, edit: true },
}} last_job: null,
isSelected={false} },
onSelect={onSelect} }}
label="Source Bar" isSelected={false}
/> onSelect={onSelect}
label="Source Bar"
/>
</tbody>
</table>
); );
expect(wrapper.find('StatusIcon').length).toBe(0); expect(wrapper.find('StatusIcon').length).toBe(0);
}); });
@@ -110,14 +132,20 @@ describe('<InventorySourceListItem />', () => {
test('should not render sync buttons', async () => { test('should not render sync buttons', async () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={{ <tbody>
...source, <InventorySourceListItem
summary_fields: { user_capabilities: { start: false, edit: true } }, source={{
}} ...source,
isSelected={false} summary_fields: {
onSelect={onSelect} user_capabilities: { start: false, edit: true },
/> },
}}
isSelected={false}
onSelect={onSelect}
/>
</tbody>
</table>
); );
expect(wrapper.find('InventorySourceSyncButton').length).toBe(0); expect(wrapper.find('InventorySourceSyncButton').length).toBe(0);
expect(wrapper.find('Button[aria-label="Edit Source"]').length).toBe(1); expect(wrapper.find('Button[aria-label="Edit Source"]').length).toBe(1);
@@ -126,15 +154,21 @@ describe('<InventorySourceListItem />', () => {
test('should not render edit buttons', async () => { test('should not render edit buttons', async () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={{ <tbody>
...source, <InventorySourceListItem
summary_fields: { user_capabilities: { start: true, edit: false } }, source={{
}} ...source,
isSelected={false} summary_fields: {
onSelect={onSelect} user_capabilities: { start: true, edit: false },
label="Source Bar" },
/> }}
isSelected={false}
onSelect={onSelect}
label="Source Bar"
/>
</tbody>
</table>
); );
expect(wrapper.find('Button[aria-label="Edit Source"]').length).toBe(0); expect(wrapper.find('Button[aria-label="Edit Source"]').length).toBe(0);
expect(wrapper.find('InventorySourceSyncButton').length).toBe(1); expect(wrapper.find('InventorySourceSyncButton').length).toBe(1);
@@ -143,16 +177,20 @@ describe('<InventorySourceListItem />', () => {
test('should render warning about missing execution environment', () => { test('should render warning about missing execution environment', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
wrapper = mountWithContexts( wrapper = mountWithContexts(
<InventorySourceListItem <table>
source={{ <tbody>
...source, <InventorySourceListItem
custom_virtualenv: '/var/lib/awx/env', source={{
execution_environment: null, ...source,
}} custom_virtualenv: '/var/lib/awx/env',
isSelected={false} execution_environment: null,
onSelect={onSelect} }}
label="Source Bar" isSelected={false}
/> onSelect={onSelect}
label="Source Bar"
/>
</tbody>
</table>
); );
expect( expect(
wrapper.find('.missing-execution-environment').prop('content') wrapper.find('.missing-execution-environment').prop('content')