From 9d1a187a0dcedec2fca42d5d71cb5bacbd9e44ae Mon Sep 17 00:00:00 2001 From: karthik Date: Fri, 11 Dec 2020 21:15:36 +0530 Subject: [PATCH] Fix Pipleine creation without namespace issue --- .../pipeline-builder/PipelineBuilderPage.tsx | 3 + .../__tests__/PipelineBuilderPage.spec.tsx | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/__tests__/PipelineBuilderPage.spec.tsx diff --git a/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/PipelineBuilderPage.tsx b/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/PipelineBuilderPage.tsx index c652c4bc9444..06116cb5f03e 100644 --- a/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/PipelineBuilderPage.tsx +++ b/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/PipelineBuilderPage.tsx @@ -49,6 +49,9 @@ const PipelineBuilderPage: React.FC = (props) => { if (values.editorType === EditorType.YAML) { try { pipeline = safeLoad(values.yamlData); + if (!pipeline.metadata?.namespace) { + pipeline.metadata.namespace = ns; + } } catch (err) { actions.setStatus({ submitError: `Invalid YAML - ${err}` }); return null; diff --git a/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/__tests__/PipelineBuilderPage.spec.tsx b/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/__tests__/PipelineBuilderPage.spec.tsx new file mode 100644 index 000000000000..6dce923bfeb9 --- /dev/null +++ b/frontend/packages/pipelines-plugin/src/components/pipelines/pipeline-builder/__tests__/PipelineBuilderPage.spec.tsx @@ -0,0 +1,58 @@ +import * as React from 'react'; +import { shallow, ShallowWrapper } from 'enzyme'; +import { Formik } from 'formik'; +import PipelineBuilderPage from '../PipelineBuilderPage'; +import { pipelineTestData, PipelineExampleNames } from '../../../../test-data/pipeline-data'; + +type PipelineBuilderPageProps = React.ComponentProps; +type BuilderProps = React.ComponentProps; + +jest.mock('react-i18next', () => { + const reactI18next = require.requireActual('react-i18next'); + return { + ...reactI18next, + useTranslation: () => ({ t: (key) => key }), + }; +}); + +const { pipeline } = pipelineTestData[PipelineExampleNames.WORKSPACE_PIPELINE]; +describe('PipelineBuilderPage Form', () => { + let formProps: PipelineBuilderPageProps; + let PipelineBuilderPageWrapper: ShallowWrapper; + + beforeEach(() => { + formProps = { + history: null, + location: null, + match: { params: { ns: 'default' }, isExact: true, path: '', url: '' }, + }; + PipelineBuilderPageWrapper = shallow(); + }); + + it('should render a Formik component', () => { + const PipelineBuilderForm = PipelineBuilderPageWrapper.find(Formik); + expect(PipelineBuilderForm).toHaveLength(1); + }); + + it('should have form view as default option and empty default values', () => { + const PipelineBuilderForm = PipelineBuilderPageWrapper.find(Formik); + const builderProps = PipelineBuilderForm.props() as BuilderProps; + + expect(builderProps.initialValues.editorType).toBe('form'); + expect(builderProps.initialValues.formData.params).toHaveLength(0); + expect(builderProps.initialValues.formData.tasks).toHaveLength(0); + expect(builderProps.initialValues.formData.resources).toHaveLength(0); + }); + + it('should contain the given pipeline values in intialValues', () => { + PipelineBuilderPageWrapper = shallow( + , + ); + const PipelineBuilderForm = PipelineBuilderPageWrapper.find(Formik); + const builderProps = PipelineBuilderForm.props() as BuilderProps; + const { name, tasks } = builderProps.initialValues.formData; + + expect(name).toBe(pipeline.metadata.name); + expect(tasks).toHaveLength(pipeline.spec.tasks.length); + }); +});