From 6433b6e951e41cd93c0c884502ea5db8b9ffca99 Mon Sep 17 00:00:00 2001 From: Michaela Brydon Date: Mon, 1 Jul 2024 12:29:50 -0400 Subject: [PATCH] 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;