SSR-1064 moved getInputQuestionWidgetAnswersNullSafe into base-mixin

This commit is contained in:
Matt Caimi 2024-02-08 14:37:56 -05:00
parent 5f6cc8d38f
commit 57b61ac0bf
6 changed files with 75 additions and 31 deletions

View file

@ -1,13 +1,13 @@
import { shallowMount } from '@vue/test-utils';
import { getMountOptions } from '@/helpers/unit-test-helper.js';
import widgetFields from '@/constants/cms-widget-fields.js';
import paymentMethodQuestion from '@/layouts/payment-method/payment-method-question/payment-method-question.vue';
let cmsContent;
const mockCmsContent = {
QuestionText: 'Choose a payment option',
Answers: [
[widgetFields.INPUT_QUESTION_WIDGET.QUESTION_TEXT]: 'Choose a payment option',
[widgetFields.INPUT_QUESTION_WIDGET.ANSWERS]: [
{
Name: 'NameA',
Text: 'TextA',

View file

@ -49,7 +49,7 @@ export default {
return this.getCmsContent(this.cmsWidgetName, widgetFields.INPUT_QUESTION_WIDGET.QUESTION_TEXT);
},
paymentMethodAnswerData() {
const cmsData = this.getAnswersNullSafe(this.cmsWidgetName);
const cmsData = this.getInputQuestionWidgetAnswersNullSafe(this.cmsWidgetName);
return cmsData.map((answer) => ({
buttonLabel: answer.Text,
@ -59,17 +59,6 @@ export default {
buttonImage: answer.AnswerImageUrl
}));
}
},
methods: {
getAnswersNullSafe(widgetName) {
const rawData = this.getCmsContent(widgetName, widgetFields.INPUT_QUESTION_WIDGET.ANSWERS);
if (!rawData) {
return [];
}
return rawData;
}
}
};
</script>

View file

@ -33,20 +33,14 @@ export default {
},
driverSideDamageAnswers() {
const answerContent = getLocationAnswer(damageLocationsSelected.DRIVER, this.locationAnswers);
return this.getAnswersNullSafe(answerContent?.SubWidgetName);
return this.getInputQuestionWidgetAnswersNullSafe(answerContent?.SubWidgetName);
},
passengerSideDamageAnswers() {
const answerContent = getLocationAnswer(damageLocationsSelected.PASSENGER, this.locationAnswers);
return this.getAnswersNullSafe(answerContent?.SubWidgetName);
return this.getInputQuestionWidgetAnswersNullSafe(answerContent?.SubWidgetName);
},
locationAnswers() {
return this.getAnswersNullSafe(this.damageLocationsWidgetName);
}
},
methods: {
getAnswersNullSafe(widgetName) {
const rawAnswers = this.getCmsContent(widgetName, 'Answers');
return rawAnswers || [];
return this.getInputQuestionWidgetAnswersNullSafe(this.damageLocationsWidgetName);
}
}
};

View file

@ -208,15 +208,15 @@ export default {
return [line];
},
locationAnswers() {
return this.getAnswersNullSafe(this.widget.damageLocations);
return this.getInputQuestionWidgetAnswersNullSafe(this.widget.damageLocations);
},
driverSideDamageAnswers() {
const answerContent = getLocationAnswer(damageLocationsSelected.DRIVER, this.locationAnswers);
return this.getAnswersNullSafe(answerContent?.SubWidgetName);
return this.getInputQuestionWidgetAnswersNullSafe(answerContent?.SubWidgetName);
},
passengerSideDamageAnswers() {
const answerContent = getLocationAnswer(damageLocationsSelected.PASSENGER, this.locationAnswers);
return this.getAnswersNullSafe(answerContent?.SubWidgetName);
return this.getInputQuestionWidgetAnswersNullSafe(answerContent?.SubWidgetName);
},
getDamageLines() {
const { glassToReplace, isRepair } = useMainStore().order.damage;
@ -285,10 +285,6 @@ export default {
return null;
}
},
getAnswersNullSafe(widgetName) {
const rawAnswers = this.getCmsContent(widgetName, 'Answers');
return rawAnswers || [];
},
openContactDetailsModal() {
this.$refs.contactDetailsDrawer.openModal();
}

View file

@ -5,6 +5,7 @@ import vehicleCategories from '@/constants/vehicle-categories.js';
import queryStrings from '@/constants/query-strings';
import dynamicStrings from '@/constants/dynamic-strings';
import routerParams from '@/router/router-constants/router-params';
import widgetFields from '@/constants/cms-widget-fields.js';
export default {
data() {
@ -19,6 +20,10 @@ export default {
getCmsContent(widgetName, fieldName) {
return this.$root.cmsContentByWidget?.[widgetName]?.[fieldName] ? this.$root.cmsContentByWidget[widgetName][fieldName] : '';
},
getInputQuestionWidgetAnswersNullSafe(widgetName) {
const rawAnswers = this.getCmsContent(widgetName, widgetFields.INPUT_QUESTION_WIDGET.ANSWERS);
return rawAnswers || [];
},
getFooterInfoBoxHeight() {
const footerInfoBox = document.querySelector('.footer#infoBox');
return footerInfoBox ? footerInfoBox.offsetHeight : 0;

View file

@ -1,5 +1,6 @@
import baseMixin from '@/mixins/base-mixin';
import { shallowMount } from '@vue/test-utils';
import widgetFields from '@/constants/cms-widget-fields.js';
describe('base-mixin', () => {
it('should set and get cms content', () => {
@ -20,4 +21,63 @@ describe('base-mixin', () => {
const localThis = { cmsWidgetName: 'testWidget' };
expect(baseMixin.computed.cssClassNameForCmsWidget.call(localThis)).toBe('widget-name-testWidget');
});
it('getInputQuestionWidgetAnswersNullSafe returns an empty array when the input question widget has no answers', () => {
// Arrange
const widgetName = 'inputQuestionWidget';
const wrapper = shallowMount(baseMixin, {
propsData: {
cmsContentByWidget: {}
}
});
const cmsContent = {
[widgetName]: {
[widgetFields.INPUT_QUESTION_WIDGET.QUESTION_TEXT]: 'Choose an option',
[widgetFields.INPUT_QUESTION_WIDGET.ANSWERS]: []
}
};
wrapper.componentVM.setCmsContent(cmsContent);
// Act
const answers = wrapper.componentVM.getInputQuestionWidgetAnswersNullSafe(widgetName);
// Assert
expect(answers).not.toBeNull(); // Check that answers is not null
expect(Array.isArray(answers)).toBeTruthy(); // Check that answers is an array
expect(answers).toHaveLength(0); // Check that answers is empty
});
it('getInputQuestionWidgetAnswersNullSafe returns a non-empty array when the input question widget has answers', () => {
// Arrange
const widgetName = 'inputQuestionWidget';
const wrapper = shallowMount(baseMixin, {
propsData: {
cmsContentByWidget: {}
}
});
const cmsContent = {
[widgetName]: {
[widgetFields.INPUT_QUESTION_WIDGET.QUESTION_TEXT]: 'Choose an option',
[widgetFields.INPUT_QUESTION_WIDGET.ANSWERS]: [
{
Name: 'NameA',
Text: 'TextA'
},
{
Name: 'NameB',
Text: 'TextB'
}
]
}
};
wrapper.componentVM.setCmsContent(cmsContent);
// Act
const answers = wrapper.componentVM.getInputQuestionWidgetAnswersNullSafe(widgetName);
// Assert
expect(answers).not.toBeNull(); // Check that answers is not null
expect(Array.isArray(answers)).toBeTruthy(); // Check that answers is an array
expect(answers).not.toHaveLength(0); // Check that answers is not empty
});
});