From e532f4c0c5700ce2caf87a57ef2379bb08f52854 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 12 May 2020 14:38:05 -0700 Subject: [PATCH 1/3] fix launch prompt errors when no survey present --- .../LaunchPrompt/steps/PreviewStep.jsx | 16 +++++++++++----- .../LaunchPrompt/steps/useInventoryStep.jsx | 3 +++ .../LaunchPrompt/steps/useSurveyStep.jsx | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx index e8171debe2..154f31a44d 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx @@ -8,10 +8,16 @@ import getSurveyValues from '../getSurveyValues'; function PreviewStep({ resource, config, survey, formErrors }) { const { values } = useFormikContext(); const surveyValues = getSurveyValues(values); - const passwordFields = survey.spec - .filter(q => q.type === 'password') - .map(q => q.variable); - const masked = maskPasswords(surveyValues, passwordFields); + let extraVars; + if (survey && survey.spec) { + const passwordFields = survey.spec + .filter(q => q.type === 'password') + .map(q => q.variable); + const masked = maskPasswords(surveyValues, passwordFields); + extraVars = yaml.safeDump(mergeExtraVars(values.extra_vars, masked)); + } else { + extraVars = values.extra_vars; + } return ( <> {formErrors && ( diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx index e0d0ea009b..cd1deb76f7 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx @@ -9,6 +9,9 @@ export default function useInventoryStep(config, resource, visitedSteps, i18n) { const [stepErrors, setStepErrors] = useState({}); const validate = values => { + if (!config.ask_inventory_on_launch) { + return {}; + } const errors = {}; if (!values.inventory) { errors.inventory = i18n._(t`An inventory must be selected`); diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx index 7cee7f7f6d..5ee623dd14 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx @@ -56,7 +56,7 @@ export default function useSurveyStep(config, resource, visitedSteps, i18n) { isReady: !isLoading && !!survey, error, setTouched: setFieldsTouched => { - if (!survey) { + if (!survey || !survey.spec) { return; } const fields = {}; From aa2890931311fe9f71a4527de1a01999e8946dd2 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Wed, 13 May 2020 10:25:00 -0700 Subject: [PATCH 2/3] add PreviewStep tests --- .../LaunchPrompt/steps/PreviewStep.jsx | 6 +- .../LaunchPrompt/steps/PreviewStep.test.jsx | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx index 154f31a44d..f7e8ed1c36 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.jsx @@ -14,9 +14,11 @@ function PreviewStep({ resource, config, survey, formErrors }) { .filter(q => q.type === 'password') .map(q => q.variable); const masked = maskPasswords(surveyValues, passwordFields); - extraVars = yaml.safeDump(mergeExtraVars(values.extra_vars, masked)); + extraVars = yaml.safeDump( + mergeExtraVars(values.extra_vars || '---', masked) + ); } else { - extraVars = values.extra_vars; + extraVars = values.extra_vars || '---'; } return ( <> diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx new file mode 100644 index 0000000000..623620662d --- /dev/null +++ b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx @@ -0,0 +1,78 @@ +import React from 'react'; +import { act } from 'react-dom/test-utils'; +import { Formik } from 'formik'; +import { mountWithContexts } from '@testUtils/enzymeHelpers'; +import PreviewStep from './PreviewStep'; + +const resource = { + id: 1, + type: 'job_template', + summary_fields: { + inventory: { id: 12 }, + recent_jobs: [], + }, + related: {}, +}; + +const survey = { + name: '', + spec: [ + { + variable: 'foo', + type: 'text', + }, + ], +}; + +describe('PreviewStep', () => { + test('should render PromptDetail', async () => { + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + + + + ); + }); + + const detail = wrapper.find('PromptDetail'); + expect(detail).toHaveLength(1); + expect(detail.prop('resource')).toEqual(resource); + expect(detail.prop('overrides')).toEqual({ + extra_vars: 'foo: abc\n', + limit: '4', + survey_foo: 'abc', + }); + }); + + test('should render PromptDetail without survey', async () => { + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + + + + ); + }); + + const detail = wrapper.find('PromptDetail'); + expect(detail).toHaveLength(1); + expect(detail.prop('resource')).toEqual(resource); + expect(detail.prop('overrides')).toEqual({ + extra_vars: '---', + limit: '4', + }); + }); +}); From 3c77e5b005d6baa3e80898d7e2ab0d8a6cc4a78a Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Wed, 13 May 2020 14:13:55 -0700 Subject: [PATCH 3/3] fix import --- .../src/components/LaunchPrompt/steps/PreviewStep.test.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx index 623620662d..47aba96bb1 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/PreviewStep.test.jsx @@ -1,7 +1,7 @@ import React from 'react'; import { act } from 'react-dom/test-utils'; import { Formik } from 'formik'; -import { mountWithContexts } from '@testUtils/enzymeHelpers'; +import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import PreviewStep from './PreviewStep'; const resource = {