Commit 5943cfe2 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Added tests for initializeFormData function

Ensures we set the form fields and errors
correctly when the form loads.
parent a16a23cd
...@@ -32,6 +32,28 @@ const defaultFields = { ...@@ -32,6 +32,28 @@ const defaultFields = {
endEventLabelId: null, endEventLabelId: null,
}; };
export const initializeFormData = ({ emptyFieldState, initialFields, errors }) => {
const defaultErrors = initialFields?.endEventIdentifier
? { ...emptyFieldState, endEventIdentifier: null }
: {
...emptyFieldState,
endEventIdentifier:
initialFields && !initialFields?.startEventIdentifier
? [s__('CustomCycleAnalytics|Please select a start event first')]
: null,
};
return {
fields: {
...emptyFieldState,
...initialFields,
},
fieldErrors: {
...defaultErrors,
...errors,
},
};
};
export default { export default {
components: { components: {
GlFormGroup, GlFormGroup,
...@@ -74,22 +96,11 @@ export default { ...@@ -74,22 +96,11 @@ export default {
}, },
}, },
data() { data() {
const defaultErrors = this?.initialFields?.endEventIdentifier const { initialFields = {}, errors = null } = this;
? { ...defaultFields, endEventIdentifier: null } const formData = initializeFormData({ emptyFieldState: defaultFields, initialFields, errors });
: {
...defaultFields,
endEventIdentifier: [s__('CustomCycleAnalytics|Please select a start event first')],
};
return { return {
labelEvents: getLabelEventsIdentifiers(this.events), labelEvents: getLabelEventsIdentifiers(this.events),
fields: { ...formData,
...defaultFields,
...this.initialFields,
},
fieldErrors: {
...defaultErrors,
...this.errors,
},
}; };
}, },
computed: { computed: {
...@@ -169,15 +180,15 @@ export default { ...@@ -169,15 +180,15 @@ export default {
}, },
watch: { watch: {
initialFields(newFields) { initialFields(newFields) {
console.log('newFields', newFields);
this.fields = { this.fields = {
...defaultFields, ...defaultFields,
...newFields, ...newFields,
}; };
},
errors(newErrors) {
this.fieldErrors = { this.fieldErrors = {
...defaultFields, ...defaultFields,
...this.errors, ...newErrors,
}; };
}, },
}, },
......
...@@ -2,7 +2,9 @@ import Vue from 'vue'; ...@@ -2,7 +2,9 @@ import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import createStore from 'ee/analytics/cycle_analytics/store'; import createStore from 'ee/analytics/cycle_analytics/store';
import { createLocalVue, mount } from '@vue/test-utils'; import { createLocalVue, mount } from '@vue/test-utils';
import CustomStageForm from 'ee/analytics/cycle_analytics/components/custom_stage_form.vue'; import CustomStageForm, {
initializeFormData,
} from 'ee/analytics/cycle_analytics/components/custom_stage_form.vue';
import { STAGE_ACTIONS } from 'ee/analytics/cycle_analytics/constants'; import { STAGE_ACTIONS } from 'ee/analytics/cycle_analytics/constants';
import { import {
groupLabels, groupLabels,
...@@ -749,4 +751,118 @@ describe('CustomStageForm', () => { ...@@ -749,4 +751,118 @@ describe('CustomStageForm', () => {
}); });
}); });
}); });
describe('initializeFormData', () => {
describe('without a startEventIdentifier', () => {
it('with no errors', () => {
const res = initializeFormData({
initialFields: {},
});
expect(res.fields).toEqual({});
expect(res.fieldErrors).toEqual({
endEventIdentifier: ['Please select a start event first'],
});
});
it('with field errors', () => {
const res = initializeFormData({
initialFields: {},
errors: {
name: ['is reserved'],
},
});
expect(res.fields).toEqual({});
expect(res.fieldErrors).toEqual({
endEventIdentifier: ['Please select a start event first'],
name: ['is reserved'],
});
});
});
describe('with a startEventIdentifier', () => {
it('with no errors', () => {
const res = initializeFormData({
initialFields: {
startEventIdentifier: 'start-event',
},
errors: {},
});
expect(res.fields).toEqual({ startEventIdentifier: 'start-event' });
expect(res.fieldErrors).toEqual({
endEventIdentifier: null,
});
});
it('with field errors', () => {
const res = initializeFormData({
initialFields: {
startEventIdentifier: 'start-event',
},
errors: {
name: ['is reserved'],
},
});
expect(res.fields).toEqual({ startEventIdentifier: 'start-event' });
expect(res.fieldErrors).toEqual({
endEventIdentifier: null,
name: ['is reserved'],
});
});
});
describe('with all fields set', () => {
it('with no errors', () => {
const res = initializeFormData({
initialFields: {
id: 1,
name: 'cool-stage',
startEventIdentifier: 'start-event',
endEventIdentifier: 'end-event',
startEventLabelId: 10,
endEventLabelId: 20,
},
errors: {},
});
expect(res.fields).toEqual({
id: 1,
name: 'cool-stage',
startEventIdentifier: 'start-event',
endEventIdentifier: 'end-event',
startEventLabelId: 10,
endEventLabelId: 20,
});
expect(res.fieldErrors).toEqual({
endEventIdentifier: null,
});
});
it('with field errors', () => {
const res = initializeFormData({
initialFields: {
id: 1,
name: 'cool-stage',
startEventIdentifier: 'start-event',
endEventIdentifier: 'end-event',
startEventLabelId: 10,
endEventLabelId: 20,
},
errors: {
name: ['is reserved'],
},
});
expect(res.fields).toEqual({
id: 1,
name: 'cool-stage',
startEventIdentifier: 'start-event',
endEventIdentifier: 'end-event',
startEventLabelId: 10,
endEventLabelId: 20,
});
expect(res.fieldErrors).toEqual({
endEventIdentifier: null,
name: ['is reserved'],
});
});
});
});
}); });
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