diff --git a/src/constants/endpoints.js b/src/constants/endpoints.js index c317c9b90..64703d682 100644 --- a/src/constants/endpoints.js +++ b/src/constants/endpoints.js @@ -54,6 +54,10 @@ const endpoints = { url: "/vehicle/api/v1/vehicle/lookup-vin-by-address", method: "POST", }, + LookupVinByImage: { + url: "/vehicle/api/v1/vehicle/vins-by-image", + method: "POST", + }, IsVinByAddressPermissible: { url: "/vehicle/api/v1/vehicle/is-vin-by-address-permissible", method: "GET", diff --git a/src/constants/store-actions.js b/src/constants/store-actions.js index 52fa6d877..019ac2310 100644 --- a/src/constants/store-actions.js +++ b/src/constants/store-actions.js @@ -20,6 +20,7 @@ const storeActions = { LOOKUP_VEHICLE_BY_VIN: "lookupVehicleByVin", LOOKUP_VIN_BY_PLATE: "lookupVinByPlate", LOOKUP_VIN_BY_ADDRESS: "lookupVinByAddress", + LOOKUP_VIN_BY_IMAGE: "lookupVinByImage", GET_PARTS_OR_QUESTIONS: "getPartsOrQuestions", GET_PARTS: "getParts", GET_WIPERS: "getWipers", diff --git a/src/digital-components/button-question/button-question.spec.js b/src/digital-components/button-question/button-question.spec.js index e9e1d70a4..ce8d87fab 100644 --- a/src/digital-components/button-question/button-question.spec.js +++ b/src/digital-components/button-question/button-question.spec.js @@ -1,6 +1,9 @@ import { shallowMount, mount } from "@vue/test-utils"; import buttonQuestion from "./button-question"; import { getMountOptions } from "@/helpers/unit-test-helper.js"; +import crypto from "crypto"; + +global.crypto = crypto; describe("buttonQuestion.vue", () => { it("Should show overflow classes on fieldset if isOverflowScrollable is true", () => { diff --git a/src/digital-components/button-question/button-question.vue b/src/digital-components/button-question/button-question.vue index 44a427275..ec7b165a0 100644 --- a/src/digital-components/button-question/button-question.vue +++ b/src/digital-components/button-question/button-question.vue @@ -86,7 +86,7 @@ import listButton from "@/ux-components/list-button/list-button"; import listButtonHorizontal from "@/ux-components/list-button-horizontal/list-button-horizontal"; import listCard from "@/ux-components/list-card/list-card"; import radio from "@/ux-components/radio/radio"; -import { ErrorMessage } from "vee-validate"; +import { useField, ErrorMessage } from "vee-validate"; export default { name: "buttonQuestion", @@ -128,6 +128,29 @@ export default { valueToLogType: String, additionalButtonStyling: String, isSmallQuestionText: Boolean, + customButtonQuestionId: String, + }, + setup(props) { + const propsClone = Object.assign({}, props); + const modelValue = propsClone.modelValue; + + const fieldOptions = { + value: modelValue, + initialValue: modelValue, + }; + + const { errorMessage, handleBlur, handleChange, meta, validate, errors, resetField } = + useField(props.groupName, props.validationRules, fieldOptions); + + return { + errorMessage, + handleBlur, + handleChange, + validate, + meta, + errors, + resetField, + }; }, beforeMount() { if (this.buttonTypeObject) { @@ -226,6 +249,11 @@ export default { this.$emit(`buttonEvent.${eventName}`, event.args); }, }, + watch: { + modelValue() { + this.resetField(); + }, + }, components: { listButton, listButtonHorizontal, diff --git a/src/digital-components/dropdown-question/dropdown-question.vue b/src/digital-components/dropdown-question/dropdown-question.vue index e122837c4..5c32b298c 100644 --- a/src/digital-components/dropdown-question/dropdown-question.vue +++ b/src/digital-components/dropdown-question/dropdown-question.vue @@ -3,15 +3,15 @@ class="dropdown-question" :class="(errors && errors.length) || hasError ? 'has-error' : ''"> { // Assert expect(wrapper.vm.handleChange).toHaveBeenCalled; }); + + it("Should not display camera or spinner icon when disabled", () => { + // Arrange + const inputId = "test"; + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + includeImageQuestion: true, + inputId: inputId, + isDisabled: true, + }, + mixins: [mockMixin], + attachTo: document.body, + }); + + // Act + const cameraIcon = wrapper.find('[data-test="image-upload"]'); + const loadingIcon = wrapper.findComponent("loader"); + + // Assert + expect(cameraIcon.exists()).toBe(false); + expect(loadingIcon.exists()).toBe(false); + }); + + describe("Image Uploading", () => { + it("Should call this.handleChange & emit update when fully valid image is submitted.", async () => { + // Arrange + const inputId = "test"; + const responseValue = "testResponse"; + + const imageHandler = jest.fn().mockImplementation( + () => + new Promise((resolve, reject) => { + resolve({ + data: [responseValue], + }); + }) + ); + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + "onUpdate:modelValue": (v) => wrapper.setProps({ modelValue: v }), + includeImageQuestion: true, + inputId: inputId, + imageQuestionSubmitHandler: imageHandler, + }, + mixins: [mockMixin], + attachTo: document.body, + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + wrapper.vm.isImageValid = jest.fn().mockImplementation(() => true); + + // Act + const imageUploadField = wrapper.find('[data-test="image-upload"]'); + + await imageUploadField.trigger("change"); + + await wrapper.vm.$nextTick(); + + // Assert + + expect(wrapper.vm.imageQuestionSubmitHandler).toHaveBeenCalled(); + expect(wrapper.vm.handleChange).toHaveBeenCalled(); + expect(wrapper.emitted()).toHaveProperty("update:modelValue"); + }); + + it("Should not call handler and emit image-lookup-error when image is invalid.", async () => { + // Arrange + const inputId = "test"; + const responseValue = "testResponse"; + + const imageHandler = jest.fn().mockImplementation( + () => + new Promise((resolve, reject) => { + resolve({ + data: [responseValue], + }); + }) + ); + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + "onUpdate:modelValue": (v) => wrapper.setProps({ modelValue: v }), + includeImageQuestion: true, + inputId: inputId, + imageQuestionSubmitHandler: imageHandler, + }, + mixins: [mockMixin], + attachTo: document.body, + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + wrapper.vm.isImageValid = jest.fn().mockImplementation(() => false); + + // Act + const imageUploadField = wrapper.find('[data-test="image-upload"]'); + + await imageUploadField.trigger("change"); + + await wrapper.vm.$nextTick(); + + // Assert + expect(wrapper.vm.imageQuestionSubmitHandler).not.toHaveBeenCalled(); + expect(wrapper.vm.handleChange).not.toHaveBeenCalled(); + expect(wrapper.emitted()).toHaveProperty("imageValidityError"); + }); + + it("Should emit image-lookup-error when image lookup responds with an error.", async () => { + // Arrange + const inputId = "test"; + + const imageHandler = jest.fn().mockImplementation( + () => + new Promise((resolve, reject) => { + reject({}); + }) + ); + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + "onUpdate:modelValue": (v) => wrapper.setProps({ modelValue: v }), + includeImageQuestion: true, + inputId: inputId, + imageQuestionSubmitHandler: imageHandler, + }, + mixins: [mockMixin], + attachTo: document.body, + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + wrapper.vm.isImageValid = jest.fn().mockImplementation(() => true); + + // Act + const imageUploadField = wrapper.find('[data-test="image-upload"]'); + + await imageUploadField.trigger("change"); + + await wrapper.vm.$nextTick(); + + // Assert + expect(wrapper.vm.imageQuestionSubmitHandler).toHaveBeenCalled(); + expect(wrapper.vm.handleChange).not.toHaveBeenCalled(); + expect(wrapper.emitted()).toHaveProperty("imageLookupError"); + }); + }); + + describe("Image Validation", () => { + it("Should return true if a valid image is tested.", () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + inputId: "input ID", + maxFileSize: 5140 * 1028, + }, + mixins: [mockMixin], + }); + + const image = { + size: 100 * 1028, + }; + + // Act + const result = wrapper.vm.isImageValid(image); + + // Assert + expect(result).toBeTruthy(); + }); + + it("Should return false if null image is tested.", () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + inputId: "input ID", + maxFileSize: 5140 * 1028, + }, + mixins: [mockMixin], + }); + + const image = null; + + // Act + const result = wrapper.vm.isImageValid(image); + + // Assert + expect(result).toBeFalsy(); + }); + + it("Should return false if oversized image is tested.", () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + inputId: "input ID", + maxFileSize: 5140 * 1028, + }, + mixins: [mockMixin], + }); + + const image = { + size: 10000000 * 1028, + }; + + // Act + const result = wrapper.vm.isImageValid(image); + + // Assert + expect(result).toBeFalsy(); + }); + }); }); diff --git a/src/digital-components/textbox-question/textbox-question.vue b/src/digital-components/textbox-question/textbox-question.vue index 335c47a60..a4de11254 100644 --- a/src/digital-components/textbox-question/textbox-question.vue +++ b/src/digital-components/textbox-question/textbox-question.vue @@ -15,7 +15,12 @@ hideInput ? 'hide-input' : '', ]" v-html="questionText"> - + + + + + + + + + import { useField, validate } from "vee-validate"; +import { storeActions } from "@/constants/store-actions"; +import loader from "@/ux-components/loader/loader.vue"; +import { ref } from "vue"; export default { name: "textbox-question", @@ -88,6 +113,9 @@ export default { questionAlignment: String, // Left or center. Left is default. cornerStyle: String, // Rounded or square. Square is default. includeSearchIcon: Boolean, + includeImageQuestion: Boolean, + imageQuestionSubmitHandler: Function, + maxFileSize: Number, hideInput: Boolean, centerErrorMessage: Boolean, }, @@ -97,6 +125,7 @@ export default { const propsClone = Object.assign({}, props); const modelValue = propsClone.modelValue; let initialValue; + let isImageProcessing = ref(false); switch (typeof modelValue) { case "number": @@ -127,8 +156,37 @@ export default { validate, meta, errors, + isImageProcessing, }; }, + methods: { + async imageChanged(e) { + let file = e.target.files[0]; + + if (!this.isImageValid(file)) { + this.$emit("imageValidityError"); + return; + } + + this.isImageProcessing = true; + + try { + this.value = await this.imageQuestionSubmitHandler(file); + + await this.$nextTick(); + + document.getElementById(this.inputId).dispatchEvent(new Event("change")); + } catch (error) { + this.$emit("imageLookupError"); + } + + this.isImageProcessing = false; + }, + + isImageValid(imageFile) { + return imageFile && imageFile.size < this.maxFileSize; + }, + }, computed: { questionText() { return this.getCmsContent(this.cmsWidgetName, "QuestionText"); @@ -153,6 +211,9 @@ export default { } }, }, + components: { + loader, + }, }; @@ -191,6 +252,38 @@ export default { display: flex; } } + &.has-camera-icon { + label { + &.camera-icon-input { + position: absolute; + top: 50%; + transform: translateY(-50%); + right: 1rem; + background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 40 36' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M19.9774 16.5228C17.3559 16.5228 15.1864 18.6621 15.1864 21.3476C15.1864 24.0331 17.3107 26.1724 19.9774 26.1724C22.6441 26.1724 24.7684 24.0331 24.7684 21.3476C24.7684 18.6621 22.6441 16.5228 19.9774 16.5228Z' fill='%231574A1'/%3E%3Cpath d='M38.4181 7.23725H29.8701C29.1469 2.64 24.7684 0 19.9774 0C15.1864 0 10.8531 2.64 10.0847 7.23725H1.58192C0.723164 7.23725 0 7.96553 0 8.83035V33.7738C0 34.6387 0.723164 35.3669 1.58192 35.3669H38.4181C39.2768 35.3669 40 34.6387 40 33.7738V8.83035C40 7.96553 39.2768 7.23725 38.4181 7.23725ZM19.9774 29.7683C15.3672 29.7683 11.5706 25.9904 11.5706 21.3021C11.5706 16.6138 15.3672 12.8814 19.9774 12.8814C24.5876 12.8814 28.3842 16.6593 28.3842 21.3476C28.3842 26.0359 24.6328 29.7683 19.9774 29.7683ZM36.565 14.5655H33.0395V11.0152H36.565V14.5655Z' fill='%231574A1'/%3E%3C/svg%3E%0A"); + background-repeat: no-repeat; + background-position: center; + width: 1rem; + height: 100%; + display: flex; + border: none; + background-color: transparent; + &:hover { + cursor: pointer; + } + input[type="file"] { + position: absolute; + left: -9999px; + } + } + } + + .loading-icon { + position: absolute; + top: 50%; + transform: translateY(-50%); + right: 1rem; + } + } } input { &.has-icon { diff --git a/src/global-methods.js b/src/global-methods.js index 14e5cca0e..2df5c29af 100644 --- a/src/global-methods.js +++ b/src/global-methods.js @@ -7,10 +7,16 @@ import { GaCategories, GaActions, GaLabels } from "@/constants/analytics"; import { headerKeys } from "@/constants/header-keys"; export default { - callHttpClient({ method, endpoint, payload, logApiCall = true }) { + callHttpClient({ method, endpoint, payload, logApiCall = true, isFormData = false }) { return new Promise((resolve, reject) => { const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO; - const payloadAndAnalyticsData = Object.assign({}, payload, { AppName: "FixMyGlass" }); + let payloadAndAnalyticsData = {}; + if (isFormData) { + payloadAndAnalyticsData = payload; + payloadAndAnalyticsData.append("AppName", "FixMyGlass"); + } else { + Object.assign(payloadAndAnalyticsData, payload, { AppName: "FixMyGlass" }); + } const headers = { [headerKeys.EXPERIMENT]: JSON.stringify(store.getters.experimentSettings), }; diff --git a/src/layouts/service-location/appointment-type-question/appointment-type-question.spec.js b/src/layouts/service-location/appointment-type-question/appointment-type-question.spec.js index cdbe474a2..65fba87f2 100644 --- a/src/layouts/service-location/appointment-type-question/appointment-type-question.spec.js +++ b/src/layouts/service-location/appointment-type-question/appointment-type-question.spec.js @@ -21,7 +21,7 @@ const mockCmsContent = { }, { AnswerImageUrl: "", - Name: "DropOff", + Name: "Dropoff", SubText: "", SubWidgetName: "", Text: "Drop-off", @@ -75,7 +75,7 @@ describe("appointment-type-question.vue", () => { }, { AnswerImageUrl: "", - Name: "DropOff", + Name: "Dropoff", SubText: "", SubWidgetName: "", Text: "Drop-off", @@ -108,7 +108,7 @@ describe("appointment-type-question.vue", () => { }, { AnswerImageUrl: "", - Name: "DropOff", + Name: "Dropoff", SubText: "", SubWidgetName: "", Text: "Drop-off", diff --git a/src/layouts/service-location/appointment-type-question/appointment-type-question.vue b/src/layouts/service-location/appointment-type-question/appointment-type-question.vue index 0394b8a84..6260df9d1 100644 --- a/src/layouts/service-location/appointment-type-question/appointment-type-question.vue +++ b/src/layouts/service-location/appointment-type-question/appointment-type-question.vue @@ -2,6 +2,7 @@ answer.Name == "Mobile"); } else if (this.isServiceableInshop) { filteredAnswers = this.answersFromCms.filter( - (answer) => answer.Name == "Inshop" || answer.Name == "DropOff" + (answer) => answer.Name == "Inshop" || answer.Name == "Dropoff" ); } else { filteredAnswers = []; diff --git a/src/layouts/service-location/mobile-location-modal-questions/mobile-location-modal-questions.vue b/src/layouts/service-location/mobile-location-modal-questions/mobile-location-modal-questions.vue index acc0c9787..4b0a4a1ac 100644 --- a/src/layouts/service-location/mobile-location-modal-questions/mobile-location-modal-questions.vue +++ b/src/layouts/service-location/mobile-location-modal-questions/mobile-location-modal-questions.vue @@ -1,7 +1,7 @@ - + + + + {{ errorMessage }} + +