From 4a6b3fc147d36f86bcd9272b6e1033b16359c32a Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Wed, 26 Apr 2023 15:52:57 -0400 Subject: [PATCH 1/2] Update vins-by-image call for new endpoint. --- src/global-methods.js | 9 ++------- src/store/index.js | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/global-methods.js b/src/global-methods.js index 2df5c29af..cba64e908 100644 --- a/src/global-methods.js +++ b/src/global-methods.js @@ -7,16 +7,11 @@ import { GaCategories, GaActions, GaLabels } from "@/constants/analytics"; import { headerKeys } from "@/constants/header-keys"; export default { - callHttpClient({ method, endpoint, payload, logApiCall = true, isFormData = false }) { + callHttpClient({ method, endpoint, payload, logApiCall = true }) { return new Promise((resolve, reject) => { const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO; let payloadAndAnalyticsData = {}; - if (isFormData) { - payloadAndAnalyticsData = payload; - payloadAndAnalyticsData.append("AppName", "FixMyGlass"); - } else { - Object.assign(payloadAndAnalyticsData, payload, { AppName: "FixMyGlass" }); - } + Object.assign(payloadAndAnalyticsData, payload, { AppName: "FixMyGlass" }); const headers = { [headerKeys.EXPERIMENT]: JSON.stringify(store.getters.experimentSettings), }; diff --git a/src/store/index.js b/src/store/index.js index 661dc7339..da63d3ab6 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -531,13 +531,28 @@ export const actions = { }); }, lookupVinByImage(context, image) { - const data = new FormData(); - data.append("vinImage", image); - return globalMethods.callHttpClient({ - method: endpoints.LookupVinByImage.method, - endpoint: endpoints.LookupVinByImage.url, - payload: data, - isFormData: true, + return new Promise((resolve, reject) => { + let reader = new FileReader(); + reader.onload = (e) => { + resolve(reader.result); + }; + reader.readAsDataURL(image); + }).then((result) => { + const components = result.split(","); + const contentType = image.type; + const imageBase64 = components[1]; + + const data = { + imageData: imageBase64, + contentType: contentType, + fileName: image.name, + }; + + return globalMethods.callHttpClient({ + method: endpoints.LookupVinByImage.method, + endpoint: endpoints.LookupVinByImage.url, + payload: data, + }); }); }, isVinByAddressPermissible(context, zip) { From 06ec6dbcb22badb61b9f18d199c4d345032f16fd Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Wed, 26 Apr 2023 16:36:53 -0400 Subject: [PATCH 2/2] Update unit tests. --- src/store/store.spec.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/store/store.spec.js b/src/store/store.spec.js index 9c2dbb0a7..c09b20a93 100644 --- a/src/store/store.spec.js +++ b/src/store/store.spec.js @@ -406,14 +406,16 @@ describe("Actions", () => { it("lookupVinByImage action, should return list of vins", async () => { // Arrange const context = state; - const dummyImage = {}; + const image = new File([], "test.jpg", { + type: "image/jpeg", + }); globalMethods.callHttpClient.mockImplementation(() => { return Promise.resolve({ data: ["1C6JJTAG3NL134044"] }); }); // Act - const response = await actions.lookupVinByImage(context, dummyImage); + const response = await actions.lookupVinByImage(context, image); // Assert expect(response.data).toEqual(["1C6JJTAG3NL134044"]); @@ -422,7 +424,9 @@ describe("Actions", () => { it("lookupVinByImage action, should reject if error in calling API", async () => { // Arrange const context = state; - const dummyImage = {}; + const image = new File([], "test.jpg", { + type: "image/jpeg", + }); globalMethods.callHttpClient.mockImplementation(() => { return Promise.reject("An error occurred"); @@ -431,9 +435,7 @@ describe("Actions", () => { // Act // Assert - await expect(actions.lookupVinByImage(context, dummyImage)).rejects.toEqual( - "An error occurred" - ); + await expect(actions.lookupVinByImage(context, image)).rejects.toEqual("An error occurred"); }); it("getVehicleMakes action, should return makes list", async () => {