diff --git a/x-pack/legacy/plugins/rollup/__jest__/client_integration/helpers/job_clone.helpers.js b/x-pack/legacy/plugins/rollup/__jest__/client_integration/helpers/job_clone.helpers.js index 48672a8bc13d..b99eb44bee3b 100644 --- a/x-pack/legacy/plugins/rollup/__jest__/client_integration/helpers/job_clone.helpers.js +++ b/x-pack/legacy/plugins/rollup/__jest__/client_integration/helpers/job_clone.helpers.js @@ -10,13 +10,13 @@ import { JobCreate } from '../../../public/crud_app/sections'; import { JOB_TO_CLONE } from './constants'; import { deserializeJob } from '../../../public/crud_app/services'; -const initTestBed = registerTestBed(JobCreate, { - store: createRollupJobsStore({ - cloneJob: { job: deserializeJob(JOB_TO_CLONE.jobs[0]) }, - }), -}); export const setup = props => { + const initTestBed = registerTestBed(JobCreate, { + store: createRollupJobsStore({ + cloneJob: { job: deserializeJob(JOB_TO_CLONE.jobs[0]) }, + }), + }); const testBed = initTestBed(props); const { component } = testBed; diff --git a/x-pack/legacy/plugins/rollup/__jest__/client_integration/job_create_clone.test.js b/x-pack/legacy/plugins/rollup/__jest__/client_integration/job_create_clone.test.js index 8a0b3159b34d..29d2d00163ad 100644 --- a/x-pack/legacy/plugins/rollup/__jest__/client_integration/job_create_clone.test.js +++ b/x-pack/legacy/plugins/rollup/__jest__/client_integration/job_create_clone.test.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { setupEnvironment, pageHelpers } from './helpers'; +import { setupEnvironment, pageHelpers, nextTick } from './helpers'; import { JOB_TO_CLONE, JOB_CLONE_INDEX_PATTERN_CHECK } from './helpers/constants'; jest.mock('ui/new_platform'); @@ -134,4 +134,64 @@ describe('Cloning a rollup job through create job wizard', () => { expect(checkedCountActual).toBe(checkedCountExpected); }); }); + + it('should correctly reset defaults after index pattern changes', async () => { + // 1. Logistics + + // Sanity check for rollup job name, i.e., we are in clone mode. + expect(find('rollupJobName').props().value).toBe(jobConfig.id + '-copy'); + + // Changing the index pattern value after cloning a rollup job should update a number of values. + // On each view of the set up wizard we check for the expected state after this change. + form.setInputValue('rollupIndexPattern', 'test'); + // Fires off a network request. + await nextTick(); + + const { + groups: { + date_histogram: dateHistogram + }, + } = jobConfig; + + await actions.clickNextStep(); + + // 2. Date Histogram + + expect(exists('rollupJobCreateDateHistogramTitle')).toBe(true); + expect(find('rollupJobCreateDateFieldSelect').props().value).toBe(dateHistogram.field); + + await actions.clickNextStep(); + + // 3. Terms + + expect(exists('rollupJobCreateTermsTitle')).toBe(true); + const { tableCellsValues: tableCellValuesTerms } = table.getMetaData('rollupJobTermsFieldList'); + expect(tableCellValuesTerms[0][0]).toBe('No terms fields added'); + + await actions.clickNextStep(); + + // 4. Histogram + + expect(exists('rollupJobCreateHistogramTitle')).toBe(true); + const { tableCellsValues: tableCellValuesHisto } = table.getMetaData( + 'rollupJobHistogramFieldList' + ); + + expect(tableCellValuesHisto[0][0]).toBe('No histogram fields added'); + + await actions.clickNextStep(); + + // 5. Metrics + + expect(exists('rollupJobCreateMetricsTitle')).toBe(true); + const { rows: metricsRows } = table.getMetaData('rollupJobMetricsFieldList'); + // Empty placeholder value + expect(metricsRows.length).toBe(1); + + // 6. Review + + await actions.clickNextStep(); + + expect(exists('rollupJobCreateReviewTitle')).toBe(true); + }); }); diff --git a/x-pack/legacy/plugins/rollup/public/crud_app/sections/job_create/job_create.js b/x-pack/legacy/plugins/rollup/public/crud_app/sections/job_create/job_create.js index 1d7d0c1dd8de..52968c22045d 100644 --- a/x-pack/legacy/plugins/rollup/public/crud_app/sections/job_create/job_create.js +++ b/x-pack/legacy/plugins/rollup/public/crud_app/sections/job_create/job_create.js @@ -125,13 +125,14 @@ export class JobCreateUi extends Component { const { clearCloneJob, jobToClone } = this.props; if (jobToClone) { clearCloneJob(); - this.requestIndexPatternValidation(); + this.requestIndexPatternValidation(false); } } componentDidUpdate(prevProps, prevState) { const indexPattern = this.getIndexPattern(); if (indexPattern !== this.getIndexPattern(prevState)) { + // If the user hasn't entered anything, then skip validation. if (!indexPattern || !indexPattern.trim()) { this.setState({ @@ -159,7 +160,7 @@ export class JobCreateUi extends Component { this.props.clearCreateJobErrors(); } - requestIndexPatternValidation = debounce(() => { + requestIndexPatternValidation = debounce((resetDefaults = true) => { const indexPattern = this.getIndexPattern(); const lastIndexPatternValidationTime = this.lastIndexPatternValidationTime = Date.now(); @@ -265,6 +266,16 @@ export class JobCreateUi extends Component { indexPatternDateFields.sort(); + if (resetDefaults) { + // Whenever the index pattern changes we default to the first date field if there is one. + this.onFieldsChange( + { + dateHistogramField: indexPatternDateFields.length ? indexPatternDateFields[0] : undefined, + }, + STEP_DATE_HISTOGRAM + ); + } + this.setState({ indexPatternAsyncErrors, indexPatternDateFields, @@ -273,16 +284,6 @@ export class JobCreateUi extends Component { indexPatternMetricsFields, isValidatingIndexPattern: false, }); - - if (!jobToClone) { - // Select first time field by default. - this.onFieldsChange( - { - dateHistogramField: indexPatternDateFields.length ? indexPatternDateFields[0] : null, - }, - STEP_DATE_HISTOGRAM - ); - } }).catch(error => { // We don't need to do anything if this component has been unmounted. if (!this._isMounted) {