Commit a9243d6f authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Move prepareStageErrors to utils

Extracts the prepareStageErrors method to the
utils and adds related specs
parent e1427bc3
...@@ -109,7 +109,8 @@ export default { ...@@ -109,7 +109,8 @@ export default {
stageErrors: stageErrors || initializeStageErrors(initialPreset), stageErrors: stageErrors || initializeStageErrors(initialPreset),
...initialData, ...initialData,
} }
: { stages: [], nameError }; // TODO: not sure if i should pass empty stages here : { stages: [], nameError };
return { return {
selectedPreset: initialPreset, selectedPreset: initialPreset,
presetOptions: PRESET_OPTIONS, presetOptions: PRESET_OPTIONS,
...@@ -131,7 +132,6 @@ export default { ...@@ -131,7 +132,6 @@ export default {
return this.nameError?.length ? this.nameError.join('\n\n') : null; return this.nameError?.length ? this.nameError.join('\n\n') : null;
}, },
hasInitialFormErrors() { hasInitialFormErrors() {
// TODO: do we need this + should we check the contained arrays instead
const { initialFormErrors } = this; const { initialFormErrors } = this;
return Boolean(Object.keys(initialFormErrors).length); return Boolean(Object.keys(initialFormErrors).length);
}, },
...@@ -171,6 +171,8 @@ export default { ...@@ -171,6 +171,8 @@ export default {
...mapActions(['createValueStream']), ...mapActions(['createValueStream']),
onSubmit() { onSubmit() {
const { name, stages } = this; const { name, stages } = this;
console.log('onSubmit::this.nameError', this.nameError);
console.log('onSubmit::this.stageErrors', this.stageErrors);
// TODO: validate before submission // TODO: validate before submission
return this.createValueStream({ return this.createValueStream({
name, name,
......
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { transformRawStages } from '../utils';
import * as types from './mutation_types'; import * as types from './mutation_types';
import { transformRawStages, prepareStageErrors } from '../utils';
export default { export default {
[types.SET_FEATURE_FLAGS](state, featureFlags) { [types.SET_FEATURE_FLAGS](state, featureFlags) {
...@@ -124,18 +124,8 @@ export default { ...@@ -124,18 +124,8 @@ export default {
[types.RECEIVE_CREATE_VALUE_STREAM_ERROR](state, { data: { stages = [] }, errors = {} }) { [types.RECEIVE_CREATE_VALUE_STREAM_ERROR](state, { data: { stages = [] }, errors = {} }) {
state.isCreatingValueStream = false; state.isCreatingValueStream = false;
// TODO: move to utils + add additional specs
// TODO: should test that we end up with the same amount of errors as stages
// This is because the JSON response only includes failed stages with an index of the stage
const { stages: stageErrors = {}, ...rest } = errors; const { stages: stageErrors = {}, ...rest } = errors;
const fullStageErrors = Object.keys(stageErrors).length state.createValueStreamErrors = { ...rest, stages: prepareStageErrors(stages, stageErrors) };
? stages.map((_, index) => {
return convertObjectPropsToCamelCase(stageErrors[index]) || {};
})
: {};
// NOTE: BE currently returns the equivalent of a JS hash for the stages errors, an array simplifies things
state.createValueStreamErrors = { ...rest, stages: fullStageErrors };
}, },
[types.RECEIVE_CREATE_VALUE_STREAM_SUCCESS](state, valueStream) { [types.RECEIVE_CREATE_VALUE_STREAM_SUCCESS](state, valueStream) {
state.isCreatingValueStream = false; state.isCreatingValueStream = false;
......
...@@ -105,6 +105,19 @@ export const transformRawTasksByTypeData = (data = []) => { ...@@ -105,6 +105,19 @@ export const transformRawTasksByTypeData = (data = []) => {
return data.map((d) => convertObjectPropsToCamelCase(d, { deep: true })); return data.map((d) => convertObjectPropsToCamelCase(d, { deep: true }));
}; };
/**
* Prepares the stage errors for use in the create value stream form
*
* The JSON error response returns a key value pair, the key corresponds to the
* index of the stage with errors and the value is the returned error(s)
*
* @param {Array} stages - Array of value stream stages
* @param {Object} errors - Key value pair of stage errors
* @returns {Array} Returns and array of stage error objects
*/
export const prepareStageErrors = (stages, errors) =>
stages.length ? stages.map((_, index) => convertObjectPropsToCamelCase(errors[index]) || {}) : [];
/** /**
* Takes the duration data for selected stages, transforms the date values and returns * Takes the duration data for selected stages, transforms the date values and returns
* the data in a flattened array * the data in a flattened array
......
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
toggleSelectedLabel, toggleSelectedLabel,
transformStagesForPathNavigation, transformStagesForPathNavigation,
prepareTimeMetricsData, prepareTimeMetricsData,
prepareStageErrors,
} from 'ee/analytics/cycle_analytics/utils'; } from 'ee/analytics/cycle_analytics/utils';
import { toYmd } from 'ee/analytics/shared/utils'; import { toYmd } from 'ee/analytics/shared/utils';
import { getDatesInRange } from '~/lib/utils/datetime_utility'; import { getDatesInRange } from '~/lib/utils/datetime_utility';
...@@ -176,6 +177,29 @@ describe('Value Stream Analytics utils', () => { ...@@ -176,6 +177,29 @@ describe('Value Stream Analytics utils', () => {
}); });
}); });
describe('prepareStageErrors', () => {
const stages = [{ name: 'stage 1' }, { name: 'stage 2' }, { name: 'stage 3' }];
const nameError = { name: "Can't be blank" };
const stageErrors = { 1: nameError };
it('returns an object for each stage', () => {
const res = prepareStageErrors(stages, stageErrors);
expect(res[0]).toEqual({});
expect(res[1]).toEqual(nameError);
expect(res[2]).toEqual({});
});
it('returns the same number of error objects as stages', () => {
const res = prepareStageErrors(stages, stageErrors);
expect(res.length).toEqual(stages.length);
});
it('returns an empty object for each stage if there are no errors', () => {
const res = prepareStageErrors(stages, {});
expect(res).toEqual([{}, {}, {}]);
});
});
describe('isPersistedStage', () => { describe('isPersistedStage', () => {
it.each` it.each`
custom | id | expected custom | id | expected
......
...@@ -1692,9 +1692,6 @@ msgstr "" ...@@ -1692,9 +1692,6 @@ msgstr ""
msgid "Add another link" msgid "Add another link"
msgstr "" msgstr ""
msgid "Add another stage"
msgstr ""
msgid "Add approval rule" msgid "Add approval rule"
msgstr "" msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment