mirror of
https://github.com/ansible/awx.git
synced 2026-03-01 00:38:45 -03:30
convert InventoryList to hooks
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { useState, useCallback, useEffect } from 'react';
|
||||||
import { withRouter } from 'react-router-dom';
|
import { useLocation, useRouteMatch } from 'react-router-dom';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
|
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import { Card, PageSection } from '@patternfly/react-core';
|
import { Card, PageSection } from '@patternfly/react-core';
|
||||||
|
|
||||||
import { InventoriesAPI } from '@api';
|
import { InventoriesAPI } from '@api';
|
||||||
|
import useRequest, { useDeleteItems } from '@util/useRequest';
|
||||||
import AlertModal from '@components/AlertModal';
|
import AlertModal from '@components/AlertModal';
|
||||||
import DatalistToolbar from '@components/DataListToolbar';
|
import DatalistToolbar from '@components/DataListToolbar';
|
||||||
import ErrorDetail from '@components/ErrorDetail';
|
import ErrorDetail from '@components/ErrorDetail';
|
||||||
@@ -23,225 +24,172 @@ const QS_CONFIG = getQSConfig('inventory', {
|
|||||||
order_by: 'name',
|
order_by: 'name',
|
||||||
});
|
});
|
||||||
|
|
||||||
class InventoriesList extends Component {
|
function InventoryList({ i18n }) {
|
||||||
constructor(props) {
|
const location = useLocation();
|
||||||
super(props);
|
const match = useRouteMatch();
|
||||||
|
const [selected, setSelected] = useState([]);
|
||||||
|
|
||||||
this.state = {
|
const {
|
||||||
hasContentLoading: true,
|
result: { inventories, itemCount, actions },
|
||||||
contentError: null,
|
error: contentError,
|
||||||
deletionError: null,
|
isLoading,
|
||||||
selected: [],
|
request: fetchInventories,
|
||||||
|
} = useRequest(
|
||||||
|
useCallback(async () => {
|
||||||
|
const params = parseQueryString(QS_CONFIG, location.search);
|
||||||
|
const [response, actionsResponse] = await Promise.all([
|
||||||
|
InventoriesAPI.read(params),
|
||||||
|
InventoriesAPI.readOptions(),
|
||||||
|
]);
|
||||||
|
return {
|
||||||
|
inventories: response.data.results,
|
||||||
|
itemCount: response.data.count,
|
||||||
|
actions: actionsResponse.data.actions,
|
||||||
|
};
|
||||||
|
}, [location]),
|
||||||
|
{
|
||||||
inventories: [],
|
inventories: [],
|
||||||
itemCount: 0,
|
itemCount: 0,
|
||||||
};
|
actions: {},
|
||||||
|
|
||||||
this.loadInventories = this.loadInventories.bind(this);
|
|
||||||
this.handleSelectAll = this.handleSelectAll.bind(this);
|
|
||||||
this.handleSelect = this.handleSelect.bind(this);
|
|
||||||
this.handleInventoryDelete = this.handleInventoryDelete.bind(this);
|
|
||||||
this.handleDeleteErrorClose = this.handleDeleteErrorClose.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
this.loadInventories();
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate(prevProps) {
|
|
||||||
const { location } = this.props;
|
|
||||||
|
|
||||||
if (location !== prevProps.location) {
|
|
||||||
this.loadInventories();
|
|
||||||
}
|
}
|
||||||
}
|
);
|
||||||
|
|
||||||
handleDeleteErrorClose() {
|
useEffect(() => {
|
||||||
this.setState({ deletionError: null });
|
fetchInventories();
|
||||||
}
|
}, [fetchInventories]);
|
||||||
|
|
||||||
handleSelectAll(isSelected) {
|
const isAllSelected =
|
||||||
const { inventories } = this.state;
|
selected.length === inventories.length && selected.length > 0;
|
||||||
const selected = isSelected ? [...inventories] : [];
|
const {
|
||||||
this.setState({ selected });
|
isLoading: isDeleteLoading,
|
||||||
}
|
deleteItems: deleteTeams,
|
||||||
|
deletionError,
|
||||||
|
clearDeletionError,
|
||||||
|
} = useDeleteItems(
|
||||||
|
useCallback(async () => {
|
||||||
|
return Promise.all(selected.map(team => InventoriesAPI.destroy(team.id)));
|
||||||
|
}, [selected]),
|
||||||
|
{
|
||||||
|
qsConfig: QS_CONFIG,
|
||||||
|
allItemsSelected: isAllSelected,
|
||||||
|
fetchItems: fetchInventories,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
handleSelect(inventory) {
|
const handleInventoryDelete = async () => {
|
||||||
const { selected } = this.state;
|
await deleteTeams();
|
||||||
if (selected.some(s => s.id === inventory.id)) {
|
setSelected([]);
|
||||||
this.setState({ selected: selected.filter(s => s.id !== inventory.id) });
|
};
|
||||||
|
|
||||||
|
const hasContentLoading = isDeleteLoading || isLoading;
|
||||||
|
const canAdd = actions && actions.POST;
|
||||||
|
|
||||||
|
const handleSelectAll = isSelected => {
|
||||||
|
setSelected(isSelected ? [...inventories] : []);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSelect = row => {
|
||||||
|
if (selected.some(s => s.id === row.id)) {
|
||||||
|
setSelected(selected.filter(s => s.id !== row.id));
|
||||||
} else {
|
} else {
|
||||||
this.setState({ selected: selected.concat(inventory) });
|
setSelected(selected.concat(row));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async handleInventoryDelete() {
|
const addButton = (
|
||||||
const { selected, itemCount } = this.state;
|
<AddDropDownButton
|
||||||
|
key="add"
|
||||||
this.setState({ hasContentLoading: true });
|
dropdownItems={[
|
||||||
try {
|
|
||||||
await Promise.all(
|
|
||||||
selected.map(({ id }) => {
|
|
||||||
return InventoriesAPI.destroy(id);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
this.setState({ itemCount: itemCount - selected.length });
|
|
||||||
} catch (err) {
|
|
||||||
this.setState({ deletionError: err });
|
|
||||||
} finally {
|
|
||||||
await this.loadInventories();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async loadInventories() {
|
|
||||||
const { location } = this.props;
|
|
||||||
const { actions: cachedActions } = this.state;
|
|
||||||
const params = parseQueryString(QS_CONFIG, location.search);
|
|
||||||
|
|
||||||
let optionsPromise;
|
|
||||||
if (cachedActions) {
|
|
||||||
optionsPromise = Promise.resolve({ data: { actions: cachedActions } });
|
|
||||||
} else {
|
|
||||||
optionsPromise = InventoriesAPI.readOptions();
|
|
||||||
}
|
|
||||||
|
|
||||||
const promises = Promise.all([InventoriesAPI.read(params), optionsPromise]);
|
|
||||||
|
|
||||||
this.setState({ contentError: null, hasContentLoading: true });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [
|
|
||||||
{
|
{
|
||||||
data: { count, results },
|
label: i18n._(t`Inventory`),
|
||||||
|
url: `${match.url}/inventory/add/`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
data: { actions },
|
label: i18n._(t`Smart Inventory`),
|
||||||
|
url: `${match.url}/smart_inventory/add/`,
|
||||||
},
|
},
|
||||||
] = await promises;
|
]}
|
||||||
|
/>
|
||||||
this.setState({
|
);
|
||||||
actions,
|
return (
|
||||||
itemCount: count,
|
<PageSection>
|
||||||
inventories: results,
|
<Card>
|
||||||
selected: [],
|
<PaginatedDataList
|
||||||
});
|
contentError={contentError}
|
||||||
} catch (err) {
|
hasContentLoading={hasContentLoading}
|
||||||
this.setState({ contentError: err });
|
items={inventories}
|
||||||
} finally {
|
itemCount={itemCount}
|
||||||
this.setState({ hasContentLoading: false });
|
pluralizedItemName={i18n._(t`Inventories`)}
|
||||||
}
|
qsConfig={QS_CONFIG}
|
||||||
}
|
onRowClick={handleSelect}
|
||||||
|
toolbarSearchColumns={[
|
||||||
render() {
|
{
|
||||||
const {
|
name: i18n._(t`Name`),
|
||||||
contentError,
|
key: 'name',
|
||||||
hasContentLoading,
|
isDefault: true,
|
||||||
deletionError,
|
},
|
||||||
inventories,
|
{
|
||||||
itemCount,
|
name: i18n._(t`Created By (Username)`),
|
||||||
selected,
|
key: 'created_by__username',
|
||||||
actions,
|
},
|
||||||
} = this.state;
|
{
|
||||||
const { match, i18n } = this.props;
|
name: i18n._(t`Modified By (Username)`),
|
||||||
const canAdd =
|
key: 'modified_by__username',
|
||||||
actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
|
},
|
||||||
const isAllSelected =
|
]}
|
||||||
selected.length === inventories.length && selected.length !== 0;
|
toolbarSortColumns={[
|
||||||
const addButton = (
|
{
|
||||||
<AddDropDownButton
|
name: i18n._(t`Name`),
|
||||||
key="add"
|
key: 'name',
|
||||||
dropdownItems={[
|
},
|
||||||
{
|
]}
|
||||||
label: i18n._(t`Inventory`),
|
renderToolbar={props => (
|
||||||
url: `${match.url}/inventory/add/`,
|
<DatalistToolbar
|
||||||
},
|
{...props}
|
||||||
{
|
showSelectAll
|
||||||
label: i18n._(t`Smart Inventory`),
|
showExpandCollapse
|
||||||
url: `${match.url}/smart_inventory/add/`,
|
isAllSelected={isAllSelected}
|
||||||
},
|
onSelectAll={handleSelectAll}
|
||||||
]}
|
qsConfig={QS_CONFIG}
|
||||||
/>
|
additionalControls={[
|
||||||
);
|
...(canAdd ? [addButton] : []),
|
||||||
return (
|
<ToolbarDeleteButton
|
||||||
<PageSection>
|
key="delete"
|
||||||
<Card>
|
onDelete={handleInventoryDelete}
|
||||||
<PaginatedDataList
|
itemsToDelete={selected}
|
||||||
contentError={contentError}
|
pluralizedItemName="Inventories"
|
||||||
hasContentLoading={hasContentLoading}
|
/>,
|
||||||
items={inventories}
|
]}
|
||||||
itemCount={itemCount}
|
/>
|
||||||
pluralizedItemName={i18n._(t`Inventories`)}
|
)}
|
||||||
qsConfig={QS_CONFIG}
|
renderItem={inventory => (
|
||||||
onRowClick={this.handleSelect}
|
<InventoryListItem
|
||||||
toolbarSearchColumns={[
|
key={inventory.id}
|
||||||
{
|
value={inventory.name}
|
||||||
name: i18n._(t`Name`),
|
inventory={inventory}
|
||||||
key: 'name',
|
detailUrl={
|
||||||
isDefault: true,
|
inventory.kind === 'smart'
|
||||||
},
|
? `${match.url}/smart_inventory/${inventory.id}/details`
|
||||||
{
|
: `${match.url}/inventory/${inventory.id}/details`
|
||||||
name: i18n._(t`Created By (Username)`),
|
}
|
||||||
key: 'created_by__username',
|
onSelect={() => handleSelect(inventory)}
|
||||||
},
|
isSelected={selected.some(row => row.id === inventory.id)}
|
||||||
{
|
/>
|
||||||
name: i18n._(t`Modified By (Username)`),
|
)}
|
||||||
key: 'modified_by__username',
|
emptyStateControls={canAdd && addButton}
|
||||||
},
|
/>
|
||||||
]}
|
</Card>
|
||||||
toolbarSortColumns={[
|
<AlertModal
|
||||||
{
|
isOpen={deletionError}
|
||||||
name: i18n._(t`Name`),
|
variant="error"
|
||||||
key: 'name',
|
title={i18n._(t`Error!`)}
|
||||||
},
|
onClose={clearDeletionError}
|
||||||
]}
|
>
|
||||||
renderToolbar={props => (
|
{i18n._(t`Failed to delete one or more inventories.`)}
|
||||||
<DatalistToolbar
|
<ErrorDetail error={deletionError} />
|
||||||
{...props}
|
</AlertModal>
|
||||||
showSelectAll
|
</PageSection>
|
||||||
showExpandCollapse
|
);
|
||||||
isAllSelected={isAllSelected}
|
|
||||||
onSelectAll={this.handleSelectAll}
|
|
||||||
qsConfig={QS_CONFIG}
|
|
||||||
additionalControls={[
|
|
||||||
...(canAdd ? [addButton] : []),
|
|
||||||
<ToolbarDeleteButton
|
|
||||||
key="delete"
|
|
||||||
onDelete={this.handleInventoryDelete}
|
|
||||||
itemsToDelete={selected}
|
|
||||||
pluralizedItemName="Inventories"
|
|
||||||
/>,
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
renderItem={inventory => (
|
|
||||||
<InventoryListItem
|
|
||||||
key={inventory.id}
|
|
||||||
value={inventory.name}
|
|
||||||
inventory={inventory}
|
|
||||||
detailUrl={
|
|
||||||
inventory.kind === 'smart'
|
|
||||||
? `${match.url}/smart_inventory/${inventory.id}/details`
|
|
||||||
: `${match.url}/inventory/${inventory.id}/details`
|
|
||||||
}
|
|
||||||
onSelect={() => this.handleSelect(inventory)}
|
|
||||||
isSelected={selected.some(row => row.id === inventory.id)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
emptyStateControls={canAdd && addButton}
|
|
||||||
/>
|
|
||||||
</Card>
|
|
||||||
<AlertModal
|
|
||||||
isOpen={deletionError}
|
|
||||||
variant="error"
|
|
||||||
title={i18n._(t`Error!`)}
|
|
||||||
onClose={this.handleDeleteErrorClose}
|
|
||||||
>
|
|
||||||
{i18n._(t`Failed to delete one or more inventories.`)}
|
|
||||||
<ErrorDetail error={deletionError} />
|
|
||||||
</AlertModal>
|
|
||||||
</PageSection>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { InventoriesList as _InventoriesList };
|
export default withI18n()(InventoryList);
|
||||||
export default withI18n()(withRouter(InventoriesList));
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { act } from 'react-dom/test-utils';
|
||||||
import { InventoriesAPI } from '@api';
|
import { InventoriesAPI } from '@api';
|
||||||
import { mountWithContexts, waitForElement } from '@testUtils/enzymeHelpers';
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
|
|
||||||
import InventoriesList, { _InventoriesList } from './InventoryList';
|
import InventoryList from './InventoryList';
|
||||||
|
|
||||||
jest.mock('@api');
|
jest.mock('@api');
|
||||||
|
|
||||||
@@ -117,7 +118,7 @@ const mockInventories = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('<InventoriesList />', () => {
|
describe('<InventoryList />', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
InventoriesAPI.read.mockResolvedValue({
|
InventoriesAPI.read.mockResolvedValue({
|
||||||
data: {
|
data: {
|
||||||
@@ -140,186 +141,174 @@ describe('<InventoriesList />', () => {
|
|||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('initially renders successfully', () => {
|
test('should load and render teams', async () => {
|
||||||
mountWithContexts(
|
let wrapper;
|
||||||
<InventoriesList
|
await act(async () => {
|
||||||
match={{ path: '/inventories', url: '/inventories' }}
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
location={{ search: '', pathname: '/inventories' }}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Inventories are retrieved from the api and the components finishes loading', async done => {
|
|
||||||
const loadInventories = jest.spyOn(
|
|
||||||
_InventoriesList.prototype,
|
|
||||||
'loadInventories'
|
|
||||||
);
|
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === true
|
|
||||||
);
|
|
||||||
expect(loadInventories).toHaveBeenCalled();
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === false
|
|
||||||
);
|
|
||||||
expect(wrapper.find('InventoryListItem').length).toBe(3);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('handleSelect is called when a inventory list item is selected', async done => {
|
|
||||||
const handleSelect = jest.spyOn(_InventoriesList.prototype, 'handleSelect');
|
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === false
|
|
||||||
);
|
|
||||||
await wrapper
|
|
||||||
.find('input#select-inventory-1')
|
|
||||||
.closest('DataListCheck')
|
|
||||||
.props()
|
|
||||||
.onChange();
|
|
||||||
expect(handleSelect).toBeCalled();
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('selected').length === 1
|
|
||||||
);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('handleSelectAll is called when a inventory list item is selected', async done => {
|
|
||||||
const handleSelectAll = jest.spyOn(
|
|
||||||
_InventoriesList.prototype,
|
|
||||||
'handleSelectAll'
|
|
||||||
);
|
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === false
|
|
||||||
);
|
|
||||||
wrapper
|
|
||||||
.find('Checkbox#select-all')
|
|
||||||
.props()
|
|
||||||
.onChange(true);
|
|
||||||
expect(handleSelectAll).toBeCalled();
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('selected').length === 3
|
|
||||||
);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('delete button is disabled if user does not have delete capabilities on a selected inventory', async done => {
|
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
|
||||||
wrapper.find('InventoriesList').setState({
|
|
||||||
inventories: mockInventories,
|
|
||||||
itemCount: 3,
|
|
||||||
isInitialized: true,
|
|
||||||
selected: mockInventories.slice(0, 2),
|
|
||||||
});
|
});
|
||||||
await waitForElement(
|
wrapper.update();
|
||||||
wrapper,
|
|
||||||
'ToolbarDeleteButton * button',
|
expect(wrapper.find('InventoryListItem')).toHaveLength(3);
|
||||||
el => el.getDOMNode().disabled === false
|
|
||||||
);
|
|
||||||
wrapper.find('InventoriesList').setState({
|
|
||||||
selected: mockInventories,
|
|
||||||
});
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'ToolbarDeleteButton * button',
|
|
||||||
el => el.getDOMNode().disabled === true
|
|
||||||
);
|
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('api is called to delete inventories for each selected inventory.', () => {
|
test('should select team when checked', async () => {
|
||||||
InventoriesAPI.destroy = jest.fn();
|
let wrapper;
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
await act(async () => {
|
||||||
wrapper.find('InventoriesList').setState({
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
inventories: mockInventories,
|
|
||||||
itemCount: 3,
|
|
||||||
isInitialized: true,
|
|
||||||
isModalOpen: true,
|
|
||||||
selected: mockInventories.slice(0, 2),
|
|
||||||
});
|
});
|
||||||
wrapper.find('ToolbarDeleteButton').prop('onDelete')();
|
wrapper.update();
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
wrapper
|
||||||
|
.find('InventoryListItem')
|
||||||
|
.first()
|
||||||
|
.invoke('onSelect')();
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
wrapper
|
||||||
|
.find('InventoryListItem')
|
||||||
|
.first()
|
||||||
|
.prop('isSelected')
|
||||||
|
).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should select all', async () => {
|
||||||
|
let wrapper;
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
wrapper.find('DataListToolbar').invoke('onSelectAll')(true);
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
const items = wrapper.find('InventoryListItem');
|
||||||
|
expect(items).toHaveLength(3);
|
||||||
|
items.forEach(item => {
|
||||||
|
expect(item.prop('isSelected')).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
wrapper
|
||||||
|
.find('InventoryListItem')
|
||||||
|
.first()
|
||||||
|
.prop('isSelected')
|
||||||
|
).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should disable delete button', async () => {
|
||||||
|
let wrapper;
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
wrapper
|
||||||
|
.find('InventoryListItem')
|
||||||
|
.at(2)
|
||||||
|
.invoke('onSelect')();
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
expect(wrapper.find('ToolbarDeleteButton button').prop('disabled')).toEqual(
|
||||||
|
true
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should call delete api', async () => {
|
||||||
|
let wrapper;
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
wrapper
|
||||||
|
.find('InventoryListItem')
|
||||||
|
.at(0)
|
||||||
|
.invoke('onSelect')();
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
await act(async () => {
|
||||||
|
wrapper
|
||||||
|
.find('InventoryListItem')
|
||||||
|
.at(1)
|
||||||
|
.invoke('onSelect')();
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
await act(async () => {
|
||||||
|
wrapper.find('ToolbarDeleteButton').invoke('onDelete')();
|
||||||
|
});
|
||||||
|
|
||||||
expect(InventoriesAPI.destroy).toHaveBeenCalledTimes(2);
|
expect(InventoriesAPI.destroy).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('error is shown when inventory not successfully deleted from api', async done => {
|
test('should show deletion error', async () => {
|
||||||
InventoriesAPI.destroy.mockRejectedValue(
|
InventoriesAPI.destroy.mockRejectedValue(
|
||||||
new Error({
|
new Error({
|
||||||
response: {
|
response: {
|
||||||
config: {
|
config: {
|
||||||
method: 'delete',
|
method: 'delete',
|
||||||
url: '/api/v2/inventories/1',
|
url: '/api/v2/teams/1',
|
||||||
},
|
},
|
||||||
data: 'An error occurred',
|
data: 'An error occurred',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
let wrapper;
|
||||||
wrapper.find('InventoriesList').setState({
|
await act(async () => {
|
||||||
inventories: mockInventories,
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
itemCount: 1,
|
|
||||||
isInitialized: true,
|
|
||||||
isModalOpen: true,
|
|
||||||
selected: mockInventories.slice(0, 1),
|
|
||||||
});
|
});
|
||||||
wrapper.find('ToolbarDeleteButton').prop('onDelete')();
|
wrapper.update();
|
||||||
await waitForElement(
|
expect(InventoriesAPI.read).toHaveBeenCalledTimes(1);
|
||||||
wrapper,
|
await act(async () => {
|
||||||
'Modal',
|
wrapper
|
||||||
el => el.props().isOpen === true && el.props().title === 'Error!'
|
.find('InventoryListItem')
|
||||||
);
|
.at(0)
|
||||||
|
.invoke('onSelect')();
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
done();
|
await act(async () => {
|
||||||
|
wrapper.find('ToolbarDeleteButton').invoke('onDelete')();
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
const modal = wrapper.find('Modal');
|
||||||
|
expect(modal).toHaveLength(1);
|
||||||
|
expect(modal.prop('title')).toEqual('Error!');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Add button shown for users with ability to POST', async done => {
|
test('Add button shown for users without ability to POST', async () => {
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
let wrapper;
|
||||||
await waitForElement(
|
await act(async () => {
|
||||||
wrapper,
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
'InventoriesList',
|
});
|
||||||
el => el.state('hasContentLoading') === true
|
wrapper.update();
|
||||||
);
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === false
|
|
||||||
);
|
|
||||||
expect(wrapper.find('ToolbarAddButton').length).toBe(1);
|
expect(wrapper.find('ToolbarAddButton').length).toBe(1);
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Add button hidden for users without ability to POST', async done => {
|
test('Add button hidden for users without ability to POST', async () => {
|
||||||
InventoriesAPI.readOptions.mockResolvedValue({
|
InventoriesAPI.readOptions = () =>
|
||||||
data: {
|
Promise.resolve({
|
||||||
actions: {
|
data: {
|
||||||
GET: {},
|
actions: {
|
||||||
|
GET: {},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
});
|
||||||
|
let wrapper;
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<InventoryList />);
|
||||||
});
|
});
|
||||||
const wrapper = mountWithContexts(<InventoriesList />);
|
wrapper.update();
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === true
|
|
||||||
);
|
|
||||||
await waitForElement(
|
|
||||||
wrapper,
|
|
||||||
'InventoriesList',
|
|
||||||
el => el.state('hasContentLoading') === false
|
|
||||||
);
|
|
||||||
expect(wrapper.find('ToolbarAddButton').length).toBe(0);
|
expect(wrapper.find('ToolbarAddButton').length).toBe(0);
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user