From aa1236ec3861428293d955c9d812afe4ef043aa3 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Wed, 26 Jun 2024 10:41:42 -0400 Subject: [PATCH 01/11] Adding helper file --- src/helpers/capability-question-helper.js | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/helpers/capability-question-helper.js diff --git a/src/helpers/capability-question-helper.js b/src/helpers/capability-question-helper.js new file mode 100644 index 00000000..757c979d --- /dev/null +++ b/src/helpers/capability-question-helper.js @@ -0,0 +1,42 @@ + +/** + * Given a part and a capabilityAnswerResult, returns the same part but with update + * recalibrationType and requiresRecalibration values based on the capabilityAnswerResult + * @param {*} part + * @param {*} capabilityAnswerResult + * @returns part + */ +function setPartRecalibrationProperties(part, capabilityAnswerResult) { + part.RecalibrationType = capabilityAnswerResult.Result1; + part.RequiresRecalibration = capabilityAnswerResult.Result2 != "0"; + return part; +} + +// function x(capabilityQuestionAnswers, glassLocation) { + +// } + +// setAllPartRecalibrationProperties(parts, context, { payload, pageNameToLog }) { +// const glassLocation = payload; +// const pageData = context.getters.pageData(fmgPageValues.CAPABILITY_QUESTIONS); + +// const part = pageData.partsOrQuestions.find((x) => x.glassLocation === glassLocation) +// .parts[0]; +// const capabilityQuestionAnswers = context.getters.damage.capabilityQuestionAnswers; +// const capabilityQuestionAnswersForPart = capabilityQuestionAnswers.find( +// (x) => x.glassLocation === glassLocation +// ); + +// return globalMethods.callHttpClient({ +// method: endpoints.GetPartFromCapabilityAnswer.method, +// endpoint: endpoints.GetPartFromCapabilityAnswer.url, +// payload: { +// part, +// capabilityAnswerResults: capabilityQuestionAnswersForPart, +// }, +// logApiCall: true, +// pageNameToLog: pageNameToLog, +// }); +// }, + +export default setPartRecalibrationProperties; \ No newline at end of file From 1377b40b6e972daf43d428a3b5e72558ea4fe992 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Wed, 26 Jun 2024 17:27:15 -0400 Subject: [PATCH 02/11] Setting recal properties --- src/helpers/capability-question-helper.js | 38 +++---------------- .../capability-questions.vue | 34 +++++++++-------- 2 files changed, 23 insertions(+), 49 deletions(-) diff --git a/src/helpers/capability-question-helper.js b/src/helpers/capability-question-helper.js index 757c979d..9ee0efcb 100644 --- a/src/helpers/capability-question-helper.js +++ b/src/helpers/capability-question-helper.js @@ -6,37 +6,9 @@ * @param {*} capabilityAnswerResult * @returns part */ -function setPartRecalibrationProperties(part, capabilityAnswerResult) { - part.RecalibrationType = capabilityAnswerResult.Result1; - part.RequiresRecalibration = capabilityAnswerResult.Result2 != "0"; - return part; +export default function setPartRecalibrationProperties(part, capabilityAnswerResult) { + Object.assign(part, { + recalibrationType: capabilityAnswerResult.result1, + requiresRecalibration: capabilityAnswerResult.result2 !== "0" + }); } - -// function x(capabilityQuestionAnswers, glassLocation) { - -// } - -// setAllPartRecalibrationProperties(parts, context, { payload, pageNameToLog }) { -// const glassLocation = payload; -// const pageData = context.getters.pageData(fmgPageValues.CAPABILITY_QUESTIONS); - -// const part = pageData.partsOrQuestions.find((x) => x.glassLocation === glassLocation) -// .parts[0]; -// const capabilityQuestionAnswers = context.getters.damage.capabilityQuestionAnswers; -// const capabilityQuestionAnswersForPart = capabilityQuestionAnswers.find( -// (x) => x.glassLocation === glassLocation -// ); - -// return globalMethods.callHttpClient({ -// method: endpoints.GetPartFromCapabilityAnswer.method, -// endpoint: endpoints.GetPartFromCapabilityAnswer.url, -// payload: { -// part, -// capabilityAnswerResults: capabilityQuestionAnswersForPart, -// }, -// logApiCall: true, -// pageNameToLog: pageNameToLog, -// }); -// }, - -export default setPartRecalibrationProperties; \ No newline at end of file diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index 65d6f41a..051b80e4 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -31,6 +31,7 @@ import { useMainStore } from '@/store'; import globalRules from '@/constants/global-rules'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import questionsPageLayout from '@/iss-components/questions-page-layout/questions-page-layout.vue'; +import setPartRecalibrationProperties from '@/helpers/capability-question-helper'; export default { name: 'capability-questions', @@ -131,22 +132,18 @@ export default { }, async forwardButtonAction() { const questionAnswersArray = this.questionsData.map((glass) => { - // get answerResult2 of returned answer - let selectedAnswerResult2; - glass.questions.forEach((q) => { - const idx = q.answers.findIndex((a) => a.answerResult === glass.answerData.answerResult); - if (idx !== -1) { - selectedAnswerResult2 = q.answers[idx].answerResult2; - } - }); + const { glassLocation, glassName, isSuppressedPart, answerData, questions } = glass; + const { answerResult, answeredQuestions } = answerData return { - glassLocation: glass.glassLocation, - glassName: glass.glassName, - result: glass.answerData.answerResult, - result1: glass.answerData.answerResult, - result2: selectedAnswerResult2, - answeredQuestions: glass.answerData.answeredQuestions, - isSuppressedPart: glass.isSuppressedPart + glassLocation, + glassName, + result: answerResult, + result1: answerResult, + result2: questions + .flatMap(q => q.answers) + .find(a => a.answerResult === answerData.answerResult)?.answerResult2, + answeredQuestions, + isSuppressedPart }; }); // clear out answerData for future page loads; must occur prior to store save @@ -155,7 +152,12 @@ export default { }); // save to store as order.damage.moldingQuestionArrays (array) await this.mainStore.saveCapabilityQuestionAnswers(questionAnswersArray); - // TODO call get parts from the capabilityQuestionAnswers endpoint + + for (let answer of questionAnswersArray) { + const partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; + setPartRecalibrationProperties(partAssociatedWithAnswer, answer); + } + this.navigateForward(this.partsOrQuestionsData, null); } } From 498f36d50d5811ccaf5009b371fc6bcbc1f9ed29 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Wed, 26 Jun 2024 17:50:51 -0400 Subject: [PATCH 03/11] fixing tests --- .../capability-questions/capability-questions.spec.js | 9 ++++++++- .../capability-questions/capability-questions.vue | 9 +++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/layouts/capability-questions/capability-questions.spec.js b/src/layouts/capability-questions/capability-questions.spec.js index c09d07c0..925dcb56 100644 --- a/src/layouts/capability-questions/capability-questions.spec.js +++ b/src/layouts/capability-questions/capability-questions.spec.js @@ -8,6 +8,7 @@ import { useMainStore } from '@/store'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import { nextTick } from 'vue'; import baseMixin from '@/mixins/base-mixin'; +import setPartRecalibrationProperties from '@/helpers/capability-question-helper'; // Mock our module for promises. jest.mock('@/helpers/layout-helper.js', () => ({ @@ -19,6 +20,12 @@ jest.mock('@/helpers/cms-content-helper', () => ({ fetchCmsContentForPage: jest.fn() })); +// Mock setPartRecalibrationProperties +jest.mock('@/helpers/capability-question-helper', () => ({ + setPartRecalibrationProperties: jest.fn() +})); + + const baseStoreGettersPageData = () => ({ partsOrQuestions: [ { @@ -152,7 +159,7 @@ function setupMocks({ describe('capabilityQuestions.vue', () => { describe('method arePagePrerequisitesValid...', () => { - test('Should return true for valid page requisites if pageData exists', () => { + test.only('Should return true for valid page requisites if pageData exists', () => { // Arrange const { wrapper } = setupMocks({}); diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index 051b80e4..6075475b 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -153,10 +153,11 @@ export default { // save to store as order.damage.moldingQuestionArrays (array) await this.mainStore.saveCapabilityQuestionAnswers(questionAnswersArray); - for (let answer of questionAnswersArray) { - const partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; - setPartRecalibrationProperties(partAssociatedWithAnswer, answer); - } + // set recalibration properties of parts based on capability question answers + // for (let answer of questionAnswersArray) { + // const partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; + // setPartRecalibrationProperties(partAssociatedWithAnswer, answer); + // } this.navigateForward(this.partsOrQuestionsData, null); } From 74aa0ab15f98350590d8533967096cfc9d277df3 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Wed, 26 Jun 2024 18:31:05 -0400 Subject: [PATCH 04/11] Adding tests --- src/helpers/capability-question-helper.js | 2 +- .../capability-questions.spec.js | 45 +++---------------- .../capability-questions.vue | 10 ++--- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/src/helpers/capability-question-helper.js b/src/helpers/capability-question-helper.js index 9ee0efcb..d3631937 100644 --- a/src/helpers/capability-question-helper.js +++ b/src/helpers/capability-question-helper.js @@ -6,7 +6,7 @@ * @param {*} capabilityAnswerResult * @returns part */ -export default function setPartRecalibrationProperties(part, capabilityAnswerResult) { +export function setPartRecalibrationProperties(part, capabilityAnswerResult) { Object.assign(part, { recalibrationType: capabilityAnswerResult.result1, requiresRecalibration: capabilityAnswerResult.result2 !== "0" diff --git a/src/layouts/capability-questions/capability-questions.spec.js b/src/layouts/capability-questions/capability-questions.spec.js index 925dcb56..c86f6ec5 100644 --- a/src/layouts/capability-questions/capability-questions.spec.js +++ b/src/layouts/capability-questions/capability-questions.spec.js @@ -8,7 +8,7 @@ import { useMainStore } from '@/store'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import { nextTick } from 'vue'; import baseMixin from '@/mixins/base-mixin'; -import setPartRecalibrationProperties from '@/helpers/capability-question-helper'; +import { setPartRecalibrationProperties } from '@/helpers/capability-question-helper.js'; // Mock our module for promises. jest.mock('@/helpers/layout-helper.js', () => ({ @@ -159,7 +159,7 @@ function setupMocks({ describe('capabilityQuestions.vue', () => { describe('method arePagePrerequisitesValid...', () => { - test.only('Should return true for valid page requisites if pageData exists', () => { + test('Should return true for valid page requisites if pageData exists', () => { // Arrange const { wrapper } = setupMocks({}); @@ -284,8 +284,7 @@ describe('capabilityQuestions.vue', () => { wrapper.unmount(); }); - // TODO: Add () to toHaveBeenCalled and ensure test passes. - test.skip('Should save to pinia store', async () => { + test('Should save to pinia store', async () => { // Arrange const { wrapper } = setupMocks({}); @@ -310,6 +309,7 @@ describe('capabilityQuestions.vue', () => { partsOrQuestions: [] } })); + const saveCapabilityQuestionAnswersSpy = jest.spyOn(useMainStore(), 'saveCapabilityQuestionAnswers'); // Act wrapper.vm.forwardButtonAction(); @@ -317,42 +317,7 @@ describe('capabilityQuestions.vue', () => { await nextTick(); // Assert - expect(wrapper.vm.saveCapabilityQuestionAnswers).toHaveBeenCalled; - wrapper.unmount(); - }); - // TODO: Add () to toHaveBeenCalled and ensure test passes. - test.skip('Should call GET_PART_FROM_CAPABILITY_QUESTION_ANSWER API', async () => { - // Arrange - const { wrapper } = setupMocks({}); - - wrapper.vm.questionsData = [ - { - glassLocation: 'Windshield', - glassName: 'Single', - answerData: { - answerResult: 'FW04848', - answeredQuestions: [] - }, - questions: [], - parts: [ - { - childPartQuestions: [] - } - ] - } - ]; - wrapper.vm.dispatchStoreAction = jest.fn(() => ({ - data: [] - })); - - // Act - wrapper.vm.forwardButtonAction(); - - await nextTick(); - - // Assert - expect(wrapper.vm.getPartFromCapabilityQuestionAnswer).toHaveBeenCalled; - + expect(saveCapabilityQuestionAnswersSpy).toHaveBeenCalled(); wrapper.unmount(); }); diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index 6075475b..ea299883 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -31,7 +31,7 @@ import { useMainStore } from '@/store'; import globalRules from '@/constants/global-rules'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import questionsPageLayout from '@/iss-components/questions-page-layout/questions-page-layout.vue'; -import setPartRecalibrationProperties from '@/helpers/capability-question-helper'; +import { setPartRecalibrationProperties } from '@/helpers/capability-question-helper'; export default { name: 'capability-questions', @@ -154,10 +154,10 @@ export default { await this.mainStore.saveCapabilityQuestionAnswers(questionAnswersArray); // set recalibration properties of parts based on capability question answers - // for (let answer of questionAnswersArray) { - // const partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; - // setPartRecalibrationProperties(partAssociatedWithAnswer, answer); - // } + for (let answer of questionAnswersArray) { + const partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; + setPartRecalibrationProperties(partAssociatedWithAnswer, answer); + } this.navigateForward(this.partsOrQuestionsData, null); } From 66be672e6fcb8a291f6777b7b8f47072aab17c19 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Wed, 26 Jun 2024 18:33:58 -0400 Subject: [PATCH 05/11] Clarifying method --- src/helpers/capability-question-helper.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/helpers/capability-question-helper.js b/src/helpers/capability-question-helper.js index d3631937..b9b8dafc 100644 --- a/src/helpers/capability-question-helper.js +++ b/src/helpers/capability-question-helper.js @@ -1,10 +1,10 @@ /** - * Given a part and a capabilityAnswerResult, returns the same part but with update - * recalibrationType and requiresRecalibration values based on the capabilityAnswerResult + * Modifies the recalibration properties of the provided part based on the + * provided capabilityAnswerResult * @param {*} part * @param {*} capabilityAnswerResult - * @returns part + * @returns void */ export function setPartRecalibrationProperties(part, capabilityAnswerResult) { Object.assign(part, { From 7f820f0080e4aa7444df15251c8f03d1f4dc74bd Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 11:50:26 -0400 Subject: [PATCH 06/11] Calling store method --- .../capability-questions/capability-questions.vue | 5 +++-- src/store/index.js | 13 +++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index ea299883..c4c15c91 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -155,8 +155,9 @@ export default { // set recalibration properties of parts based on capability question answers for (let answer of questionAnswersArray) { - const partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; - setPartRecalibrationProperties(partAssociatedWithAnswer, answer); + let partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; + partAssociatedWithAnswer = await useMainStore().getPartFromCapabilityQuestionAnswer(partAssociatedWithAnswer, answer); + console.log(JSON.stringify(partAssociatedWithAnswer)); } this.navigateForward(this.partsOrQuestionsData, null); diff --git a/src/store/index.js b/src/store/index.js index 28dbc3d7..0f527f1c 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -815,22 +815,15 @@ export const useMainStore = defineStore({ }); }, - getPartFromCapabilityQuestionAnswer(glassLocation) { - const pageData = this.pageData(issPageValues.CAPABILITY_QUESTIONS); - - const part = pageData.partsOrQuestions.find((x) => x.glassLocation === glassLocation) - .parts[0]; - const { capabilityQuestionAnswers } = this.order.damage; - const capabilityQuestionAnswersForPart = capabilityQuestionAnswers.find((x) => x.glassLocation === glassLocation); - - return globalMethods.callHttpClient({ + async getPartFromCapabilityQuestionAnswer(part, capabilityQuestionAnswersForPart) { + return (await globalMethods.callHttpClient({ method: endpoints.GetPartFromCapabilityAnswer.method, endpoint: endpoints.GetPartFromCapabilityAnswer.url, payload: { part, capabilityAnswerResults: capabilityQuestionAnswersForPart } - }); + })).data[0]; }, getMobilePremiumFee() { const damageType = this.damage.isRepair ? 'Repair' : 'Replace'; From b21a3d82580d453d4adbd41ab6ad6c3095674bf5 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 11:52:44 -0400 Subject: [PATCH 07/11] Removing old content --- src/helpers/capability-question-helper.js | 14 -------------- .../capability-questions.spec.js | 7 ------- .../capability-questions/capability-questions.vue | 1 - 3 files changed, 22 deletions(-) delete mode 100644 src/helpers/capability-question-helper.js diff --git a/src/helpers/capability-question-helper.js b/src/helpers/capability-question-helper.js deleted file mode 100644 index b9b8dafc..00000000 --- a/src/helpers/capability-question-helper.js +++ /dev/null @@ -1,14 +0,0 @@ - -/** - * Modifies the recalibration properties of the provided part based on the - * provided capabilityAnswerResult - * @param {*} part - * @param {*} capabilityAnswerResult - * @returns void - */ -export function setPartRecalibrationProperties(part, capabilityAnswerResult) { - Object.assign(part, { - recalibrationType: capabilityAnswerResult.result1, - requiresRecalibration: capabilityAnswerResult.result2 !== "0" - }); -} diff --git a/src/layouts/capability-questions/capability-questions.spec.js b/src/layouts/capability-questions/capability-questions.spec.js index c86f6ec5..193b078e 100644 --- a/src/layouts/capability-questions/capability-questions.spec.js +++ b/src/layouts/capability-questions/capability-questions.spec.js @@ -8,7 +8,6 @@ import { useMainStore } from '@/store'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import { nextTick } from 'vue'; import baseMixin from '@/mixins/base-mixin'; -import { setPartRecalibrationProperties } from '@/helpers/capability-question-helper.js'; // Mock our module for promises. jest.mock('@/helpers/layout-helper.js', () => ({ @@ -20,12 +19,6 @@ jest.mock('@/helpers/cms-content-helper', () => ({ fetchCmsContentForPage: jest.fn() })); -// Mock setPartRecalibrationProperties -jest.mock('@/helpers/capability-question-helper', () => ({ - setPartRecalibrationProperties: jest.fn() -})); - - const baseStoreGettersPageData = () => ({ partsOrQuestions: [ { diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index c4c15c91..f032c18c 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -31,7 +31,6 @@ import { useMainStore } from '@/store'; import globalRules from '@/constants/global-rules'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import questionsPageLayout from '@/iss-components/questions-page-layout/questions-page-layout.vue'; -import { setPartRecalibrationProperties } from '@/helpers/capability-question-helper'; export default { name: 'capability-questions', From 5d07842a6aede2888025e29ea1d68a4e5787ea35 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 12:13:18 -0400 Subject: [PATCH 08/11] Fixing bug in implementation --- .../capability-questions/capability-questions.spec.js | 1 + src/layouts/capability-questions/capability-questions.vue | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/layouts/capability-questions/capability-questions.spec.js b/src/layouts/capability-questions/capability-questions.spec.js index 193b078e..2ddec929 100644 --- a/src/layouts/capability-questions/capability-questions.spec.js +++ b/src/layouts/capability-questions/capability-questions.spec.js @@ -277,6 +277,7 @@ describe('capabilityQuestions.vue', () => { wrapper.unmount(); }); + // TODO add test spying on getPartFromCapQuestionAnswer test('Should save to pinia store', async () => { // Arrange const { wrapper } = setupMocks({}); diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index f032c18c..c19c5250 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -154,9 +154,10 @@ export default { // set recalibration properties of parts based on capability question answers for (let answer of questionAnswersArray) { - let partAssociatedWithAnswer = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation).parts[0]; - partAssociatedWithAnswer = await useMainStore().getPartFromCapabilityQuestionAnswer(partAssociatedWithAnswer, answer); - console.log(JSON.stringify(partAssociatedWithAnswer)); + const partData = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation); + if (partData) { + partData.parts[0] = await useMainStore().getPartFromCapabilityQuestionAnswer(partData.parts[0], answer); + } } this.navigateForward(this.partsOrQuestionsData, null); From 6433b6e951e41cd93c0c884502ea5db8b9ffca99 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 12:29:50 -0400 Subject: [PATCH 09/11] Fixing tests --- .../capability-questions.spec.js | 45 ++++++++++++++++--- src/mixins/vehicle-questions-mixin.js | 30 +++++++------ 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/layouts/capability-questions/capability-questions.spec.js b/src/layouts/capability-questions/capability-questions.spec.js index 2ddec929..2a1e7125 100644 --- a/src/layouts/capability-questions/capability-questions.spec.js +++ b/src/layouts/capability-questions/capability-questions.spec.js @@ -244,7 +244,7 @@ describe('capabilityQuestions.vue', () => { }); describe('forwardButtonAction', () => { - test('Should clear out answerData', () => { + test('Should clear out answerData', async () => { // Arrange const { wrapper } = setupMocks({}); @@ -269,7 +269,7 @@ describe('capabilityQuestions.vue', () => { })); // Act - wrapper.vm.forwardButtonAction(); + await wrapper.vm.forwardButtonAction(); // Assert expect(wrapper.vm.questionsData[0].answerData).toEqual({}); @@ -277,7 +277,6 @@ describe('capabilityQuestions.vue', () => { wrapper.unmount(); }); - // TODO add test spying on getPartFromCapQuestionAnswer test('Should save to pinia store', async () => { // Arrange const { wrapper } = setupMocks({}); @@ -306,15 +305,49 @@ describe('capabilityQuestions.vue', () => { const saveCapabilityQuestionAnswersSpy = jest.spyOn(useMainStore(), 'saveCapabilityQuestionAnswers'); // Act - wrapper.vm.forwardButtonAction(); - - await nextTick(); + await wrapper.vm.forwardButtonAction(); // Assert expect(saveCapabilityQuestionAnswersSpy).toHaveBeenCalled(); wrapper.unmount(); }); + test('Should call getPartFromCapabilityQuestionAnswer', async () => { + // Arrange + const { wrapper } = setupMocks({}); + + wrapper.vm.questionsData = [ + { + glassLocation: 'Windshield', + glassName: 'Single', + answerData: { + answerResult: 'FW04848', + answeredQuestions: [] + }, + questions: [], + parts: [ + { + childPartQuestions: [] + } + ] + } + ]; + useMainStore().getPartsOrQuestions = jest.fn(() => ({ + data: { + partsOrQuestions: [] + } + })); + const getPartFromCapabilityQuestionAnswerSpy = jest.spyOn(useMainStore(), 'getPartFromCapabilityQuestionAnswer'); + getPartFromCapabilityQuestionAnswerSpy.mockClear(); + + // Act + await wrapper.vm.forwardButtonAction(); + + // Assert + expect(getPartFromCapabilityQuestionAnswerSpy).toHaveBeenCalledTimes(1); + wrapper.unmount(); + }); + test('Should trigger navigateForward', async () => { // Arrange const { wrapper } = setupMocks({}); diff --git a/src/mixins/vehicle-questions-mixin.js b/src/mixins/vehicle-questions-mixin.js index 88f6b2b6..c19984d3 100644 --- a/src/mixins/vehicle-questions-mixin.js +++ b/src/mixins/vehicle-questions-mixin.js @@ -11,10 +11,10 @@ export default { return partsOrQuestions?.some((pq) => pq.parts?.length > 1); }, hasChildPartQuestions(partsOrQuestions) { - return partsOrQuestions?.some((pq) => pq.parts?.some((part) => part.childPartQuestions?.length > 0)); + return partsOrQuestions?.some((pq) => pq.parts?.some((part) => part?.childPartQuestions?.length > 0)); }, hasCapabilityQuestions(partsOrQuestions) { - return partsOrQuestions?.some((pq) => pq.parts?.some((part) => part.requiresCapabilityQuestions === true)); + return partsOrQuestions?.some((pq) => pq.parts?.some((part) => part?.requiresCapabilityQuestions === true)); }, // method to only include keys listed for lineItems.glassParts in @@ -23,18 +23,20 @@ export default { glassParts.forEach((glass) => { if (Array.isArray(glass.parts) && glass.parts.length === 1) { const singlePart = glass.parts[0]; - reducedGlassParts.push({ - partNumber: singlePart.partNumber, - description: singlePart.description, - color: singlePart.color, - partType: singlePart.partType, - canSafeliteRecalibrate: singlePart.canSafeliteRecalibrate, - requiresRecalibration: singlePart.requiresRecalibration, - requiresCapabilityQuestions: singlePart.requiresCapabilityQuestions, - recalibrationType: singlePart.recalibrationType, - childParts: singlePart.childParts, - price: singlePart.price - }); + if (singlePart){ + reducedGlassParts.push({ + partNumber: singlePart.partNumber, + description: singlePart.description, + color: singlePart.color, + partType: singlePart.partType, + canSafeliteRecalibrate: singlePart.canSafeliteRecalibrate, + requiresRecalibration: singlePart.requiresRecalibration, + requiresCapabilityQuestions: singlePart.requiresCapabilityQuestions, + recalibrationType: singlePart.recalibrationType, + childParts: singlePart.childParts, + price: singlePart.price + }); + } } }); return reducedGlassParts; From 8e9245b37f3c2804165f613f95335026151b2f3a Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 14:18:21 -0400 Subject: [PATCH 10/11] resolving all promises at once --- .../capability-questions.vue | 16 +++++++++++----- src/store/index.js | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/layouts/capability-questions/capability-questions.vue b/src/layouts/capability-questions/capability-questions.vue index c19c5250..494fcacd 100644 --- a/src/layouts/capability-questions/capability-questions.vue +++ b/src/layouts/capability-questions/capability-questions.vue @@ -153,12 +153,18 @@ export default { await this.mainStore.saveCapabilityQuestionAnswers(questionAnswersArray); // set recalibration properties of parts based on capability question answers - for (let answer of questionAnswersArray) { + const promiseResultMap = questionAnswersArray.map((answer, i) => { + const partData = this.partsOrQuestionsData.find(x => x.glassLocation === answer.glassLocation); + return { + resultKey: i, + promise: useMainStore().updatePartFromCapabilityQuestionAnswer(partData.parts[0], answer) + }; + }); + const resultMap = await settleAllPromises(promiseResultMap); + questionAnswersArray.forEach((answer, i) => { const partData = this.partsOrQuestionsData.find((x) => x.glassLocation === answer.glassLocation); - if (partData) { - partData.parts[0] = await useMainStore().getPartFromCapabilityQuestionAnswer(partData.parts[0], answer); - } - } + partData.parts[0] = resultMap[i]; + }); this.navigateForward(this.partsOrQuestionsData, null); } diff --git a/src/store/index.js b/src/store/index.js index 0f527f1c..95532981 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -815,7 +815,7 @@ export const useMainStore = defineStore({ }); }, - async getPartFromCapabilityQuestionAnswer(part, capabilityQuestionAnswersForPart) { + async updatePartFromCapabilityQuestionAnswer(part, capabilityQuestionAnswersForPart) { return (await globalMethods.callHttpClient({ method: endpoints.GetPartFromCapabilityAnswer.method, endpoint: endpoints.GetPartFromCapabilityAnswer.url, From 7e3ded3dca40bc7d9c67152af992c7f56e0367ef Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 14:33:05 -0400 Subject: [PATCH 11/11] Fixing test --- .../capability-questions.spec.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/layouts/capability-questions/capability-questions.spec.js b/src/layouts/capability-questions/capability-questions.spec.js index 2a1e7125..7f970443 100644 --- a/src/layouts/capability-questions/capability-questions.spec.js +++ b/src/layouts/capability-questions/capability-questions.spec.js @@ -8,11 +8,10 @@ import { useMainStore } from '@/store'; import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin'; import { nextTick } from 'vue'; import baseMixin from '@/mixins/base-mixin'; +import settleAllPromises from '@/helpers/layout-helper.js'; // Mock our module for promises. -jest.mock('@/helpers/layout-helper.js', () => ({ - settleAllPromises: jest.fn() -})); +jest.mock('@/helpers/layout-helper.js', () => jest.fn()); // Mock fetchCmsContentForPage jest.mock('@/helpers/cms-content-helper', () => ({ @@ -244,6 +243,7 @@ describe('capabilityQuestions.vue', () => { }); describe('forwardButtonAction', () => { + settleAllPromises.mockImplementation(() => []); test('Should clear out answerData', async () => { // Arrange const { wrapper } = setupMocks({}); @@ -312,7 +312,7 @@ describe('capabilityQuestions.vue', () => { wrapper.unmount(); }); - test('Should call getPartFromCapabilityQuestionAnswer', async () => { + test('Should call updatePartFromCapabilityQuestionAnswer', async () => { // Arrange const { wrapper } = setupMocks({}); @@ -337,14 +337,14 @@ describe('capabilityQuestions.vue', () => { partsOrQuestions: [] } })); - const getPartFromCapabilityQuestionAnswerSpy = jest.spyOn(useMainStore(), 'getPartFromCapabilityQuestionAnswer'); - getPartFromCapabilityQuestionAnswerSpy.mockClear(); + const updatePartFromCapabilityQuestionAnswerSpy = jest.spyOn(useMainStore(), 'updatePartFromCapabilityQuestionAnswer'); + updatePartFromCapabilityQuestionAnswerSpy.mockClear(); // Act await wrapper.vm.forwardButtonAction(); // Assert - expect(getPartFromCapabilityQuestionAnswerSpy).toHaveBeenCalledTimes(1); + expect(updatePartFromCapabilityQuestionAnswerSpy).toHaveBeenCalledTimes(1); wrapper.unmount(); });