diff --git a/src/layouts/molding-questions/molding-questions.spec.js b/src/layouts/molding-questions/molding-questions.spec.js new file mode 100644 index 00000000..ee5a11b1 --- /dev/null +++ b/src/layouts/molding-questions/molding-questions.spec.js @@ -0,0 +1,388 @@ +// Components +import moldingQuestions from "@/layouts/molding-questions/molding-questions"; + +// Supporting Files +import { shallowMount } from "@vue/test-utils"; +import { getMountOptions } from "@/helpers/unit-test-helper.js"; +import { useMainStore } from "@/store"; +import baseMixin from "../../mixins/base-mixin"; +import vehicleQuestionsMixin from "@/mixins/vehicle-questions-mixin"; +import { nextTick } from "vue"; + +// Mock our module for promises. +jest.mock("@/helpers/layout-helper.js", () => ({ + settleAllPromises: jest.fn(), +})); + +// Mock fetchCmsContentForPage +jest.mock("@/helpers/cms-content-helper", () => ({ + fetchCmsContentForPage: jest.fn(), +})); + +const baseStoreGettersPageData = () => { + return { + partsOrQuestions: [ + { + parts: [ + { + childPartQuestions: [ + { + questionSequence: 1, + questionText: + "Does the rubber seal around your windshield have a chrome strip running through it?", + answers: [ + { + answerResult: "WKT D1106 C", + answerText: "Yes", + nextQuestionSequence: null, + }, + { + answerResult: "WKT D1106 B", + answerText: "No", + nextQuestionSequence: null, + }, + ], + }, + ], + basePartNumber: "DW01105", + color: "Green Tint, Blue Shade", + requiresRecalibration: false, + recalibrationType: "", + canSafeliteRecalibrate: false, + requiresCapabilityQuestions: false, + childParts: null, + partNumber: "DW01105GBNN", + description: "solar", + partType: "WINDSHIELD", + }, + ], + partQuestions: [], + glassLocation: "Windshield", + glassName: "Single", + answerKey: "Windshield-Single", + answerData: null, + }, + ], + }; +}; +const baseStoreGettersDamage = () => { + return { + partsQuestionAnswers: [ + { + glassLocation: "Windshield", + glassName: "Single", + result: "FW04848", + answeredQuestions: [ + { + questionText: + "Is your vehicle equipped with the Panoramic Sunroof which can be identified by having a glass panel over the rear seats?", + selectedAnswer: "1|nextQuestion|3|Yes", + selectedAnswerText: "Yes", + questionNum: 1, + }, + { + questionText: + "Is your vehicle equipped with a heated windshield that melts snow and ice from underneath the windshield wiper blades?", + selectedAnswer: "2|nextQuestion|3|Yes", + selectedAnswerText: "Yes", + questionNum: 2, + }, + ], + }, + ], + }; +}; + +useMainStore().pageData = baseStoreGettersPageData; +useMainStore().damage = baseStoreGettersDamage; + +describe("moldingQuestions.vue", () => { + describe("method arePagePrerequisitesValid...", () => { + test("Should return true for valid page requisites if pageData exists", () => { + // Arrange + const { wrapper } = setupMocks({}); + + // Act + const result = wrapper.vm.arePagePrerequisitesValid(); + + //Assert + expect(result).toBe(true); + + wrapper.unmount(); + }); + + test("Should return false for valid page requisites if partsOrQuestions in pageData is missing", () => { + // Arrange + const { wrapper } = setupMocks({}); + useMainStore().pageData = jest.fn(() => { + return undefined; + }); + + // Act + const result = wrapper.vm.arePagePrerequisitesValid(); + + //Assert + expect(result).toBeFalsy(); + + wrapper.unmount(); + }); + + test("Should be at least one item in partsOrQuestions", () => { + // Arrange + useMainStore().pageData = jest.fn(() => { + return { + partsOrQuestions: [], + }; + }); + useMainStore().damage = baseStoreGettersDamage; + + const { wrapper } = setupMocks({}); + + // Act + const result = wrapper.vm.arePagePrerequisitesValid(); + + //Assert + expect(result).toBeFalsy(); + + wrapper.unmount(); + }); + }); + + describe("watch on selectedAnswers should be set up...", () => { + test("Should trigger handleAnswerUpdates if watched data changes", async () => { + // Arrange + useMainStore().pageData = baseStoreGettersPageData; + useMainStore().damage = baseStoreGettersDamage; + + const { wrapper } = setupMocks({}); + const spy = jest.spyOn(wrapper.vm, "handleAnswerUpdates"); + + // Act + wrapper.setData({ + selectedAnswers: { + "Windshield-Single": { + answerResult: "FW04848", + answeredQuestions: [ + { + questionText: + "Is your vehicle equipped with the Panoramic Sunroof which can be identified by having a glass panel over the rear seats?", + selectedAnswer: "1|nextQuestion|3|Yes", + selectedAnswerText: "Yes", + questionNum: 1, + }, + { + questionText: + "Is your vehicle equipped with a heated windshield that melts snow and ice from underneath the windshield wiper blades?", + selectedAnswer: "2|nextQuestion|3|Yes", + selectedAnswerText: "Yes", + questionNum: 2, + }, + ], + index: 0, + }, + }, + }); + + await nextTick(); + + //Assert + expect(spy).toHaveBeenCalled(); + + wrapper.unmount(); + }); + }); + + describe("forwardButtonAction", () => { + test("Should clear out answerData", () => { + // Arrange + const { wrapper } = setupMocks({}); + + wrapper.vm.questionsData = [ + { + glassLocation: "Windshield", + glassName: "Single", + answerData: { + answerResult: "FW04848", + answeredQuestions: [], + }, + }, + ]; + wrapper.vm.dispatchStoreAction = jest.fn(() => { + return { + data: { + partsOrQuestions: [], + }, + }; + }); + + // Act + wrapper.vm.forwardButtonAction(); + + //Assert + expect(wrapper.vm.questionsData[0].answerData).toEqual({}); + + wrapper.unmount(); + }); + + test("Should save to pinia store", async () => { + // Arrange + const { wrapper } = setupMocks({}); + + wrapper.vm.questionsData = [ + { + glassLocation: "Windshield", + glassName: "Single", + answerData: { + answerResult: "FW04848", + answeredQuestions: [], + }, + }, + ]; + useMainStore().getPartsOrQuestions = jest.fn(() => { + return { + data: { + partsOrQuestions: [], + }, + }; + }); + + // Act + wrapper.vm.forwardButtonAction(); + + await nextTick(); + + //Assert + expect(wrapper.vm.saveMoldingQuestionAnswers).toHaveBeenCalled; + wrapper.unmount(); + }); + + test("Should call GET_PARTS_OR_QUESTIONS API", async () => { + // Arrange + const { wrapper } = setupMocks({}); + + wrapper.vm.questionsData = [ + { + glassLocation: "Windshield", + glassName: "Single", + answerData: { + answerResult: "FW04848", + answeredQuestions: [], + }, + }, + ]; + useMainStore().getPartsOrQuestions = jest.fn(() => { + return { + data: { + partsOrQuestions: [], + }, + }; + }); + + // Act + wrapper.vm.forwardButtonAction(); + + await nextTick(); + + //Assert + expect(wrapper.vm.getPartsOrQuestions).toHaveBeenCalled; + + wrapper.unmount(); + }); + + test("Should trigger navigateForward", async () => { + // Arrange + const { wrapper } = setupMocks({}); + + wrapper.vm.questionsData = [ + { + glassLocation: "Windshield", + glassName: "Single", + answerData: { + answerResult: "FW04848", + answeredQuestions: [], + }, + }, + ]; + useMainStore().getPartsOrQuestions = jest.fn(() => { + return { + data: { + partsOrQuestions: [], + }, + }; + }); + wrapper.vm.navigateForward = jest.fn(); + + // Act + await wrapper.vm.forwardButtonAction(); + + //Assert + expect(wrapper.vm.navigateForward).toHaveBeenCalled(); + + wrapper.unmount(); + }); + }); +}); + +function setupMocks({ + mountOptionsMockData = { + router: { + navigate: jest.fn(), + }, + actionList: [ + { + actionName: "saveMoldingQuestionAnswers", + data: {}, + }, + { + actionName: "getPartsOrQuestions", + data: {}, + }, + ], + route: { + query: { + issPage: "molding-questions", + }, + }, + data() { + return { + computedSwitcher: [ + { + glassLocation: "Windshield", + glassName: "Single", + answerData: { + answerResult: "FW04848", + answeredQuestions: [], + }, + }, + ], + }; + }, + questionsData: { + get() { + return this.computedSwitcher; + }, + set(val) { + this.computedSwitcher = val; + }, + }, + }, +}) { + + useMainStore().getPartsOrQuestions = jest.fn(() => { + return { + data: { + partsOrQuestions: [], + } + }; + }); + + const mountOptions = getMountOptions({ + ...mountOptionsMockData, + mixins: [baseMixin, vehicleQuestionsMixin], + }); + mountOptions["attachTo"] = document.body; + + const wrapper = shallowMount(moldingQuestions, mountOptions); + + return { wrapper }; +} \ No newline at end of file diff --git a/src/layouts/molding-questions/molding-questions.vue b/src/layouts/molding-questions/molding-questions.vue index bdf3e956..e06dfe58 100644 --- a/src/layouts/molding-questions/molding-questions.vue +++ b/src/layouts/molding-questions/molding-questions.vue @@ -2,66 +2,54 @@
+ + \ No newline at end of file diff --git a/src/router/router-constants/routing-table.js b/src/router/router-constants/routing-table.js index 95179d81..78dbb55a 100644 --- a/src/router/router-constants/routing-table.js +++ b/src/router/router-constants/routing-table.js @@ -235,7 +235,7 @@ const routingTable = function(store) { }, { scenario: navigationScenarios.CLICKED_BACK_WITH_NO_VIN_NOR_QUESTIONS, - destinationIssPageValue: issPageValues.ESTIMATE, + destinationIssPageValue: issPageValues.VEHICLE_LOOKUP, }, { scenario: navigationScenarios.CLICKED_BACK_WITH_PART_QUESTIONS, diff --git a/src/store/index.js b/src/store/index.js index 1cd57344..db172dde 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -341,7 +341,7 @@ export const useMainStore = defineStore({ payload: { carId: carId, glassPieces: glassArrayForPayload, - zip: zipCode ? zipCode : "", + zip: zipCode, vin: vin, }, }); @@ -701,6 +701,34 @@ export const useMainStore = defineStore({ //Save new values this.updatePartQuestionAnswers(partQuestionAnswersArray); }, + saveMoldingQuestionAnswers(moldingQuestionAnswersArray) { + const sortedPreviousResultsArray = sortArrayOfObjectsByPropertyValue( + this.order.damage.moldingQuestionAnswers, + "result" + ); + const sortedMoldingQuestionAnswersArray = sortArrayOfObjectsByPropertyValue( + moldingQuestionAnswersArray, + "result" + ); + const haveMoldingQuestionAnswersChanged = + sortedPreviousResultsArray?.length !== sortedMoldingQuestionAnswersArray.length || + !sortedPreviousResultsArray?.every( + (x, i) => x.result === sortedMoldingQuestionAnswersArray[i].result + ); + + if (haveMoldingQuestionAnswersChanged) { + this.updateGlassParts(null); + this.updateSupportingItems(null); + this.updateCapabilityQuestionAnswers(null); + this.updatePageData({ + page: issPageValues.CAPABILITY_QUESTIONS, + data: null, + }); + } + + // Save new values + this.updateMoldingQuestionAnswers(moldingQuestionAnswersArray); + }, addEventToBus (event) { this.applicationUser.eventBus.push(event);