From b779b948d623783b08c1838cb76e03a74dd03790 Mon Sep 17 00:00:00 2001 From: Leah Schumann Date: Fri, 3 Mar 2023 10:15:48 -0500 Subject: [PATCH] Completed unit tests --- src/helpers/object-cloning-helper.js | 27 +++++++++++ src/helpers/object-cloning-helper.spec.js | 48 +++++++++++++++++++ src/helpers/service-location-helper.spec.js | 10 ++-- .../mobile-location-modal-questions.vue | 20 ++++---- .../service-location/service-location.vue | 11 +++-- 5 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 src/helpers/object-cloning-helper.js create mode 100644 src/helpers/object-cloning-helper.spec.js diff --git a/src/helpers/object-cloning-helper.js b/src/helpers/object-cloning-helper.js new file mode 100644 index 000000000..45899973f --- /dev/null +++ b/src/helpers/object-cloning-helper.js @@ -0,0 +1,27 @@ +// For nested objects, spread operator only creates new references to the top level fields, +// the remaining nested fields actually reference the original object which can introduce problems. + +// The purpose of this method is to deep clone the data in an object recursively, this is useful +// for cloning modelValues to internal models when regular two-way binding is not an option. +// See: mobile-location-modal-questions.vue + +// Creates a deep clone of an object. Clones primitives, arrays and objects, excluding class instances. +// https://www.30secondsofcode.org/js/s/deep-clone +export function deepClone(object) { + if (object === null) { + return null; + } + + let clone = Object.assign({}, object); + Object.keys(clone).forEach( + (key) => + (clone[key] = typeof object[key] === "object" ? deepClone(object[key]) : object[key]) + ); + + if (Array.isArray(object)) { + clone.length = object.length; + return Array.from(clone); + } + + return clone; +} diff --git a/src/helpers/object-cloning-helper.spec.js b/src/helpers/object-cloning-helper.spec.js new file mode 100644 index 000000000..6408992d8 --- /dev/null +++ b/src/helpers/object-cloning-helper.spec.js @@ -0,0 +1,48 @@ +import { deepClone } from "./object-cloning-helper"; + +describe("object-cloning-helper.js", () => { + it("Should return null if no object is passed in", async () => { + // Arrange + const expected = null; + + // Act + const result = deepClone(null); + + // Assert + expect(result).toEqual(expected); + }); + + it("Should return a deep copy of the object", async () => { + // Arrange + + const object = { + addressQuestions: { + streetAddress: "555 Some St", + apartmentNumberOrBusinessName: "Apt 1", + city: "Funkytown", + state: "OH", + zipCode: "55555", + }, + isVehicleProtected: true, + serviceZipCode: "55555", + }; + + const expected = { + addressQuestions: { + streetAddress: "555 Some St", + apartmentNumberOrBusinessName: "Apt 1", + city: "Funkytown", + state: "OH", + zipCode: "55555", + }, + isVehicleProtected: true, + serviceZipCode: "55555", + }; + + // Act + const result = deepClone(object); + + // Assert + expect(result).toStrictEqual(expected); + }); +}); diff --git a/src/helpers/service-location-helper.spec.js b/src/helpers/service-location-helper.spec.js index ac3eace06..345e4c807 100644 --- a/src/helpers/service-location-helper.spec.js +++ b/src/helpers/service-location-helper.spec.js @@ -45,11 +45,12 @@ jest.mock("@/mixins/base-mixin.js", () => ({ })); describe("service-location-helper.js", () => { - it("Should return the null if no service zip code is passed in", async () => { - // Arrange / Act + it("Should return null if no service zip code is passed in", async () => { + // Arrange const serviceZipCode = null; const expected = null; + // Act const result = await getPricedMobileFeePart(serviceZipCode); // Assert @@ -57,7 +58,7 @@ describe("service-location-helper.js", () => { }); it("Should return the priced mobile fee part", async () => { - // Arrange / Act + // Arrange const serviceZipCode = "43235"; const expected = { partNumber: "MOBILE FEE", @@ -68,11 +69,10 @@ describe("service-location-helper.js", () => { kitPrice: 0, }; + // Act const result = await getPricedMobileFeePart(serviceZipCode); // Assert expect(result).toEqual(expected); }); }); - -//test.todo("some test to be written in the future"); 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 644a875d3..0632ef9ae 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 @@ -10,7 +10,7 @@ ref="mobileLocationLink" id="mobileLocationLinkPromptId" linkType="text" - :text="this.mobileLocationLinkText" + :text="mobileLocationLinkText" href="#!" @click-event="openModal" aria-label="Modal window" /> @@ -47,18 +47,21 @@