CSR-319: fix syncing error between selectedValues and validation values

This commit is contained in:
Adam Caouette 2022-03-15 13:21:42 -04:00
parent f18d6f609e
commit acc9518675
5 changed files with 51 additions and 9 deletions

View file

@ -109,11 +109,15 @@ export default {
const fieldOptions = { const fieldOptions = {
type: inputType, type: inputType,
checkedValue: props.value, checkedValue: props.value,
potentialInitialValue: props.selectedValues,
}; };
if (Array.isArray(props.selectedValues) && props.selectedValues.length == 1) { // Set initialValue for validation setup if pre-selected
fieldOptions['initialValue'] = fieldOptions.checkedValue; // NOTE: props.selectedValues could be an array of strings, or an array of integers...
if (props.selectedValues && (props.selectedValues.includes(props.value) || props.selectedValues.includes(parseInt(props.value)))) {
fieldOptions['initialValue'] = fieldOptions.potentialInitialValue;
} }
const { const {
checked, checked,
handleChange, handleChange,
@ -124,6 +128,7 @@ export default {
checked, checked,
handleChange, handleChange,
errors, errors,
fieldOptions, // only need to expose this for unit test purposes
}; };
}, },
}; };

View file

@ -166,11 +166,15 @@ describe("list-button.vue", () => {
isRequired: true, isRequired: true,
isWide: false, isWide: false,
modelValue: ["List Card Checkbox"], modelValue: ["List Card Checkbox"],
buttonID: 'list-card-id'
}, },
}); });
wrapper.vm.handleCheckChange(); wrapper.vm.handleCheckChange();
// Assert // Assert
expect(wrapper.emitted()["isCheckedChanged"][0]).toEqual([{value: "List Card Checkbox", checkValue: Boolean}]); expect(wrapper.emitted()["isCheckedChanged"][0]).toEqual([{value: "List Card Checkbox", checkValue: Boolean, buttonId: 'list-card-id'}]);
}); });
it("Should set checkValue data if selectedButtonIDs has value(s)", async () => { it("Should set checkValue data if selectedButtonIDs has value(s)", async () => {
@ -192,4 +196,5 @@ describe("list-button.vue", () => {
// Assert // Assert
expect(wrapper.componentVM.checkValue).toEqual("Car-Front"); expect(wrapper.componentVM.checkValue).toEqual("Car-Front");
}); });
}); });

View file

@ -100,7 +100,13 @@ export default {
handleCheckChange(newValue, oldValue){ handleCheckChange(newValue, oldValue){
const isInitialization = typeof(oldValue) === 'function'; const isInitialization = typeof(oldValue) === 'function';
if (!isInitialization) { if (!isInitialization) {
this.$emit('isCheckedChanged', { checkValue: this.checkValue, value: this.value.toString() }); const emitEvent = {
checkValue: this.checkValue,
value: this.value.toString(),
buttonId: this.buttonID.toString(),
};
this.$emit('isCheckedChanged', emitEvent);
this.$emit("update:modelValue", emitEvent);
} }
}, },
}, },
@ -109,14 +115,19 @@ export default {
}, },
setup(props) { setup(props) {
const inputType = props.isMultiSelect ? "checkbox" : "radio"; const inputType = props.isMultiSelect ? "checkbox" : "radio";
const fieldOptions = { const fieldOptions = {
type: inputType, type: inputType,
checkedValue: props.value, checkedValue: props.value,
potentialInitialValue: props.selectedValues,
}; };
if (Array.isArray(props.selectedValues) && props.selectedValues.length == 1) { // Set initialValue for validation setup if pre-selected
fieldOptions['initialValue'] = fieldOptions.checkedValue; // NOTE: props.selectedValues could be an array of strings, or an array of integers...
if (props.selectedValues && (props.selectedValues.includes(props.value) || props.selectedValues.includes(parseInt(props.value)))) {
fieldOptions['initialValue'] = fieldOptions.potentialInitialValue;
} }
const { const {
checked, checked,
handleChange, handleChange,
@ -127,6 +138,7 @@ export default {
checked, checked,
handleChange, handleChange,
errors, errors,
fieldOptions, // only need to expose this for unit test purposes
}; };
}, },
}; };

View file

@ -232,5 +232,20 @@ describe("list-card.vue", () => {
expect(wrapper.componentVM.checkValue).toEqual("Car-Front"); expect(wrapper.componentVM.checkValue).toEqual("Car-Front");
}); });
it("Should set an initial value for validation if selectedValues include the value", async () => {
// Arrange
const wrapper = shallowMount(listCard, {
propsData: {
value: "Windshield",
groupName: "radio 1",
modelValue: ["Windshield"],
selectedValues: ["Windshield"],
},
});
// Assert
expect(wrapper.vm.fieldOptions.initialValue).toEqual([ 'Windshield' ]);
});
}); });

View file

@ -58,6 +58,7 @@
<script> <script>
import { useField } from "vee-validate"; import { useField } from "vee-validate";
export default { export default {
name: "listCard", name: "listCard",
props: { props: {
@ -135,11 +136,14 @@ export default {
const fieldOptions = { const fieldOptions = {
type: inputType, type: inputType,
checkedValue: props.value, checkedValue: props.value, // EX: "Single" or "Passenger"
potentialInitialValue: props.selectedValues,
}; };
if (Array.isArray(props.selectedValues) && props.selectedValues.length == 1) { // Set initialValue for validation setup if pre-selected
fieldOptions['initialValue'] = fieldOptions.checkedValue; // NOTE: props.selectedValues could be an array of strings, or an array of integers...
if (props.selectedValues && (props.selectedValues.includes(props.value) || props.selectedValues.includes(parseInt(props.value)))) {
fieldOptions['initialValue'] = fieldOptions.potentialInitialValue;
} }
const { const {
@ -150,6 +154,7 @@ export default {
return { return {
handleChange, handleChange,
errors, errors,
fieldOptions, // only need to expose this for unit test purposes
}; };
}, },
}; };