mirror of
https://github.com/ansible/awx.git
synced 2026-02-15 10:10:01 -03:30
fix InventoriesLookup on new JT form; add DataListRadio tests
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { string, bool, func } from 'prop-types';
|
import { string, bool, func } from 'prop-types';
|
||||||
import styled from 'styled-components';
|
|
||||||
|
|
||||||
function DataListRadio({
|
function DataListRadio({
|
||||||
className = '',
|
className = '',
|
||||||
@@ -17,7 +16,7 @@ function DataListRadio({
|
|||||||
<input
|
<input
|
||||||
{...props}
|
{...props}
|
||||||
type="radio"
|
type="radio"
|
||||||
onChange={event => onChange(event.currentTarget.checked, event)}
|
onChange={event => onChange(event.currentTarget.checked, event) }
|
||||||
aria-invalid={!isValid}
|
aria-invalid={!isValid}
|
||||||
disabled={isDisabled}
|
disabled={isDisabled}
|
||||||
checked={isChecked || checked}
|
checked={isChecked || checked}
|
||||||
@@ -43,6 +42,6 @@ DataListRadio.defaultProps = {
|
|||||||
checked: false,
|
checked: false,
|
||||||
onChange: () => {},
|
onChange: () => {},
|
||||||
'aria-labelledby': '',
|
'aria-labelledby': '',
|
||||||
}
|
};
|
||||||
|
|
||||||
export default DataListRadio;
|
export default DataListRadio;
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
|
import DataListRadio from './DataListRadio';
|
||||||
|
|
||||||
|
describe('DataListRadio', () => {
|
||||||
|
test('should call onChange', () => {
|
||||||
|
const onChange = jest.fn();
|
||||||
|
const wrapper = mountWithContexts(<DataListRadio onChange={onChange} />);
|
||||||
|
wrapper.find('input[type="radio"]').prop('onChange')({
|
||||||
|
currentTarget: { checked: true },
|
||||||
|
});
|
||||||
|
expect(onChange).toHaveBeenCalledWith(true, {
|
||||||
|
currentTarget: { checked: true },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should pass props to correct children', () => {
|
||||||
|
const onChange = jest.fn();
|
||||||
|
const wrapper = mountWithContexts(
|
||||||
|
<DataListRadio
|
||||||
|
onChange={onChange}
|
||||||
|
className="foo"
|
||||||
|
isValid
|
||||||
|
isDisabled
|
||||||
|
checked
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
const div = wrapper.find('.pf-c-data-list__item-control');
|
||||||
|
const input = wrapper.find('input[type="radio"]');
|
||||||
|
|
||||||
|
expect(div.prop('className')).toEqual('pf-c-data-list__item-control foo');
|
||||||
|
expect(input.prop('disabled')).toBe(true);
|
||||||
|
expect(input.prop('checked')).toBe(true);
|
||||||
|
expect(input.prop('aria-invalid')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -41,11 +41,13 @@ class Lookup extends React.Component {
|
|||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.assertCorrectValueType();
|
this.assertCorrectValueType();
|
||||||
|
let lookupSelectedItems = [];
|
||||||
|
if (props.value) {
|
||||||
|
lookupSelectedItems = props.multiple ? [...props.value] : [props.value];
|
||||||
|
}
|
||||||
this.state = {
|
this.state = {
|
||||||
isModalOpen: false,
|
isModalOpen: false,
|
||||||
lookupSelectedItems: props.multiple
|
lookupSelectedItems,
|
||||||
? [...props.value] || []
|
|
||||||
: [props.value],
|
|
||||||
results: [],
|
results: [],
|
||||||
count: 0,
|
count: 0,
|
||||||
error: null,
|
error: null,
|
||||||
@@ -144,9 +146,11 @@ class Lookup extends React.Component {
|
|||||||
// This handles the case where the user closes/cancels the modal and
|
// This handles the case where the user closes/cancels the modal and
|
||||||
// opens it again
|
// opens it again
|
||||||
if (!isModalOpen) {
|
if (!isModalOpen) {
|
||||||
this.setState({
|
let lookupSelectedItems = [];
|
||||||
lookupSelectedItems: multiple ? [...value] : [value],
|
if (value) {
|
||||||
});
|
lookupSelectedItems = multiple ? [...value] : [value];
|
||||||
|
}
|
||||||
|
this.setState({ lookupSelectedItems });
|
||||||
}
|
}
|
||||||
this.setState(prevState => ({
|
this.setState(prevState => ({
|
||||||
isModalOpen: !prevState.isModalOpen,
|
isModalOpen: !prevState.isModalOpen,
|
||||||
@@ -156,7 +160,9 @@ class Lookup extends React.Component {
|
|||||||
saveModal() {
|
saveModal() {
|
||||||
const { onLookupSave, name, multiple } = this.props;
|
const { onLookupSave, name, multiple } = this.props;
|
||||||
const { lookupSelectedItems } = this.state;
|
const { lookupSelectedItems } = this.state;
|
||||||
const value = multiple ? lookupSelectedItems : lookupSelectedItems[0];
|
const value = multiple
|
||||||
|
? lookupSelectedItems
|
||||||
|
: lookupSelectedItems[0] || null;
|
||||||
onLookupSave(value, name);
|
onLookupSave(value, name);
|
||||||
this.handleModalToggle();
|
this.handleModalToggle();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,18 +225,24 @@ describe('<Lookup />', () => {
|
|||||||
onLookupSave={onLookupSaveFn}
|
onLookupSave={onLookupSaveFn}
|
||||||
getItems={() => ({
|
getItems={() => ({
|
||||||
data: {
|
data: {
|
||||||
results: [ mockData ],
|
results: [mockData],
|
||||||
count: 1,
|
count: 1,
|
||||||
}
|
},
|
||||||
})}
|
})}
|
||||||
sortedColumnKey="name"
|
sortedColumnKey="name"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
wrapper.find('Lookup').instance().toggleSelected({
|
wrapper
|
||||||
id: 1,
|
.find('Lookup')
|
||||||
name: 'foo',
|
.instance()
|
||||||
});
|
.toggleSelected({
|
||||||
wrapper.find('Lookup').instance().saveModal();
|
id: 1,
|
||||||
|
name: 'foo',
|
||||||
|
});
|
||||||
|
wrapper
|
||||||
|
.find('Lookup')
|
||||||
|
.instance()
|
||||||
|
.saveModal();
|
||||||
expect(onLookupSaveFn).toHaveBeenCalledWith(
|
expect(onLookupSaveFn).toHaveBeenCalledWith(
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ describe('<JobTemplateAdd />', () => {
|
|||||||
expect(wrapper.find('input#template-description').text()).toBe(
|
expect(wrapper.find('input#template-description').text()).toBe(
|
||||||
defaultProps.description
|
defaultProps.description
|
||||||
);
|
);
|
||||||
expect(wrapper.find('InventoriesLookup').prop('value')).toBe(
|
expect(wrapper.find('InventoriesLookup').prop('value')).toBe(null);
|
||||||
defaultProps.inventory
|
|
||||||
);
|
|
||||||
expect(wrapper.find('AnsibleSelect[name="job_type"]').props().value).toBe(
|
expect(wrapper.find('AnsibleSelect[name="job_type"]').props().value).toBe(
|
||||||
defaultProps.job_type
|
defaultProps.job_type
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ class JobTemplateForm extends Component {
|
|||||||
job_type: 'run',
|
job_type: 'run',
|
||||||
project: '',
|
project: '',
|
||||||
playbook: '',
|
playbook: '',
|
||||||
summary_fields: {},
|
summary_fields: {
|
||||||
|
inventory: null,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ describe('<JobTemplateForm />', () => {
|
|||||||
expect(form.state('values').job_type).toEqual('new job type');
|
expect(form.state('values').job_type).toEqual('new job type');
|
||||||
wrapper.find('InventoriesLookup').prop('onChange')({
|
wrapper.find('InventoriesLookup').prop('onChange')({
|
||||||
id: 3,
|
id: 3,
|
||||||
name: 'inventory'
|
name: 'inventory',
|
||||||
});
|
});
|
||||||
expect(form.state('values').inventory).toEqual(3);
|
expect(form.state('values').inventory).toEqual(3);
|
||||||
wrapper.find('input#template-project').simulate('change', {
|
wrapper.find('input#template-project').simulate('change', {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
export function pluralize(str) {
|
export function pluralize(str) {
|
||||||
const lastChar = str[str.length - 1];
|
const lastChar = str[str.length - 1];
|
||||||
if (lastChar === 's') {
|
if (lastChar === 's') {
|
||||||
return `${str}es`
|
return `${str}es`;
|
||||||
}
|
}
|
||||||
if (lastChar === 'y') {
|
if (lastChar === 'y') {
|
||||||
return `${str.substr(0, str.length - 1)}ies`;
|
return `${str.substr(0, str.length - 1)}ies`;
|
||||||
|
|||||||
Reference in New Issue
Block a user