From bb5b255c28acccad9c5ca55b1ce7c7ff43ea713e Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Mon, 22 Jul 2019 16:21:11 -0700 Subject: [PATCH] updating job template tests --- awx/ui_next/src/components/Lookup/Lookup.jsx | 14 ++++++- .../src/components/Lookup/Lookup.test.jsx | 42 +++++++++++++++++++ .../JobTemplateAdd/JobTemplateAdd.test.jsx | 2 +- .../Template/shared/InventoriesLookup.jsx | 10 +++-- .../Template/shared/JobTemplateForm.jsx | 35 +--------------- .../Template/shared/JobTemplateForm.test.jsx | 13 ++++-- 6 files changed, 73 insertions(+), 43 deletions(-) diff --git a/awx/ui_next/src/components/Lookup/Lookup.jsx b/awx/ui_next/src/components/Lookup/Lookup.jsx index b7f60672ca..99da673316 100644 --- a/awx/ui_next/src/components/Lookup/Lookup.jsx +++ b/awx/ui_next/src/components/Lookup/Lookup.jsx @@ -176,15 +176,21 @@ class Lookup extends React.Component { columns, multiple, name, + required, i18n, } = this.props; const header = lookupHeader || i18n._(t`items`); + const canDelete = !required || (multiple && value.length > 1); const chips = value ? ( {(multiple ? value : [value]).map(chip => ( - this.toggleSelected(chip)}> + this.toggleSelected(chip)} + isReadOnly={!canDelete} + > {chip.name} ))} @@ -253,6 +259,7 @@ class Lookup extends React.Component { selected={lookupSelectedItems} showOverflowAfter={5} onRemove={this.toggleSelected} + isReadOnly={!canDelete} /> )} {error ?
error
: ''} @@ -272,16 +279,19 @@ Lookup.propTypes = { lookupHeader: string, name: string, onLookupSave: func.isRequired, - value: oneOfType([Item, arrayOf(Item)]).isRequired, + value: oneOfType([Item, arrayOf(Item)]), sortedColumnKey: string.isRequired, multiple: bool, + required: bool, }; Lookup.defaultProps = { id: 'lookup-search', lookupHeader: null, name: null, + value: null, multiple: false, + required: false, }; export { Lookup as _Lookup }; diff --git a/awx/ui_next/src/components/Lookup/Lookup.test.jsx b/awx/ui_next/src/components/Lookup/Lookup.test.jsx index ccf14d7154..c84e2d63fd 100644 --- a/awx/ui_next/src/components/Lookup/Lookup.test.jsx +++ b/awx/ui_next/src/components/Lookup/Lookup.test.jsx @@ -17,6 +17,7 @@ describe('', () => { getItems={() => {}} columns={mockColumns} sortedColumnKey="name" + multiple /> ); }); @@ -33,6 +34,7 @@ describe('', () => { })} columns={mockColumns} sortedColumnKey="name" + multiple /> ).find('Lookup'); @@ -57,6 +59,7 @@ describe('', () => { getItems={() => {}} columns={mockColumns} sortedColumnKey="name" + multiple /> ).find('Lookup'); expect(spy).not.toHaveBeenCalled(); @@ -90,6 +93,7 @@ describe('', () => { getItems={() => ({ data })} columns={mockColumns} sortedColumnKey="name" + multiple /> ); setImmediate(() => { @@ -118,6 +122,7 @@ describe('', () => { getItems={() => ({ data })} columns={mockColumns} sortedColumnKey="name" + multiple /> ); const removeIcon = wrapper.find('button[aria-label="close"]').first(); @@ -136,6 +141,7 @@ describe('', () => { getItems={() => {}} columns={mockColumns} sortedColumnKey="name" + multiple /> ).find('Lookup'); const chip = wrapper.find('.pf-c-chip'); @@ -152,6 +158,7 @@ describe('', () => { getItems={() => {}} columns={mockColumns} sortedColumnKey="name" + multiple /> ).find('Lookup'); wrapper.instance().toggleSelected({ @@ -182,6 +189,7 @@ describe('', () => { onLookupSave={onLookupSaveFn} getItems={() => {}} sortedColumnKey="name" + multiple /> ).find('Lookup'); wrapper.instance().toggleSelected({ @@ -206,7 +214,40 @@ describe('', () => { ); }); + test('should call callback with selected single item', () => { + mockData = { name: 'foo', id: 1, isChecked: false, url: 'https://foo' }; + const onLookupSaveFn = jest.fn(); + const wrapper = mountWithContexts( + ({ + data: { + results: [ mockData ], + count: 1, + } + })} + sortedColumnKey="name" + /> + ); + wrapper.find('Lookup').instance().toggleSelected({ + id: 1, + name: 'foo', + }); + wrapper.find('Lookup').instance().saveModal(); + expect(onLookupSaveFn).toHaveBeenCalledWith( + { + id: 1, + name: 'foo', + }, + 'fooBar' + ); + }); + test('should re-fetch data when URL params change', async () => { + mockData = [{ name: 'foo', id: 1, isChecked: false }]; const history = createMemoryHistory({ initialEntries: ['/organizations/add'], }); @@ -220,6 +261,7 @@ describe('', () => { columns={mockColumns} sortedColumnKey="name" getItems={getItems} + multiple handleHttpError={() => {}} location={{ history }} i18n={{ _: val => val.toString() }} diff --git a/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx b/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx index 74dc133dc8..caebbd7fdd 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx @@ -29,7 +29,7 @@ describe('', () => { expect(wrapper.find('input#template-description').text()).toBe( defaultProps.description ); - expect(wrapper.find('input#template-inventory').text()).toBe( + expect(wrapper.find('InventoriesLookup').prop('value')).toBe( defaultProps.inventory ); expect(wrapper.find('AnsibleSelect[name="job_type"]').props().value).toBe( diff --git a/awx/ui_next/src/screens/Template/shared/InventoriesLookup.jsx b/awx/ui_next/src/screens/Template/shared/InventoriesLookup.jsx index 722a6b5eba..a3d03b81eb 100644 --- a/awx/ui_next/src/screens/Template/shared/InventoriesLookup.jsx +++ b/awx/ui_next/src/screens/Template/shared/InventoriesLookup.jsx @@ -1,5 +1,5 @@ import React, { Fragment } from 'react'; -import { string, func } from 'prop-types'; +import { string, func, bool } from 'prop-types'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { FormGroup, Tooltip } from '@patternfly/react-core'; @@ -13,7 +13,7 @@ const getInventories = async params => InventoriesAPI.read(params); class InventoriesLookup extends React.Component { render() { - const { value, tooltip, onChange, i18n } = this.props; + const { value, tooltip, onChange, required, i18n } = this.props; return ( )} /> - {/* - {console.log(value)}} - getItems={() => ({ - data: { - results: [{id: 1, name: 'foo'}, {id: 2, name: 'bar'}], - count: 2 - } - })} - columns={[ - { name: i18n._(t`Name`), key: 'name', isSortable: true}, - ]} - sortedColumnsKey="name" - /> - */} - {/* */} ', () => { project: 3, playbook: 'Baz', type: 'job_template', + summary_fields: { + inventory: { + id: 2, + name: 'foo', + }, + }, }; afterEach(() => { @@ -31,7 +37,7 @@ describe('', () => { ); }); - test('should update form values on input changes', () => { + test('should update form values on input changes', async () => { const wrapper = mountWithContexts( ', () => { target: { value: 'new job type', name: 'job_type' }, }); expect(form.state('values').job_type).toEqual('new job type'); - wrapper.find('input#template-inventory').simulate('change', { - target: { value: 3, name: 'inventory' }, + wrapper.find('InventoriesLookup').prop('onChange')({ + id: 3, + name: 'inventory' }); expect(form.state('values').inventory).toEqual(3); wrapper.find('input#template-project').simulate('change', {