From dc4bdd3782c0bb8f7800648f4cbac6e207b7d1b5 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 3 Dec 2021 13:43:23 -0500 Subject: [PATCH] Updating unit test so that mock data can be implemented --- src/helpers/unit-test-helper.js | 10 ++++--- .../year-question/year-question.spec.js | 27 ++++++++++++++----- .../year-question/year-question.vue | 7 ++--- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/helpers/unit-test-helper.js b/src/helpers/unit-test-helper.js index 4210b56bc..5b5deb521 100644 --- a/src/helpers/unit-test-helper.js +++ b/src/helpers/unit-test-helper.js @@ -1,5 +1,5 @@ import { storeActions } from "@/constants/store-actions"; -import store from "@/store"; +import { storeMutations } from "@/constants/store-mutations.js"; export function getMountOptions(mockData) { // Define our mocks to attached to the 'global' object for Vue/Jest. @@ -18,12 +18,16 @@ export function getMountOptions(mockData) { } }); - // Mock store actions from js file + // Mock const files mocks.storeActions = storeActions; + mocks.storeMutations = storeMutations; + + // Mock $store and $router when accessing this.$store/$router + mocks.$store = mockData.store; + mocks.$router = mockData.router; const global = { mocks: mocks, - plugins: [store] }; return { global }; diff --git a/src/layouts/vehicle-year/year-question/year-question.spec.js b/src/layouts/vehicle-year/year-question/year-question.spec.js index d3e5ccc5e..769dcece8 100644 --- a/src/layouts/vehicle-year/year-question/year-question.spec.js +++ b/src/layouts/vehicle-year/year-question/year-question.spec.js @@ -1,13 +1,26 @@ -import { shallowMount } from "@vue/test-utils"; import yearQuestion from "@/layouts/vehicle-year/year-question/year-question"; -import store from "@/store"; +import { shallowMount } from "@vue/test-utils"; +import { getMountOptions } from "../../../helpers/unit-test-helper"; describe('year-question.vue', () => { - test('year-question should take a prop for years and questionText, and trigger a selectYear function when an option is clicked.', async () => { - + // Arrange + const mockData = { + store: { + commit: jest.fn(), + year: null + }, + router: { + push: jest.fn() + } + } + mockData.store.commit.mockImplementation((func, year) => { + mockData.store.year = year; + }); + + const mountOptions = getMountOptions(mockData); // Act - const wrapper = shallowMount(yearQuestion, global); + const wrapper = shallowMount(yearQuestion, mountOptions); await wrapper.setProps({ questionText: 'What year is your vehicle?', years: ['2023', '2022', '2021'] @@ -18,6 +31,6 @@ describe('year-question.vue', () => { const radioQuestion = await wrapper.findComponent({name: 'radioQuestion'}); expect(radioQuestion.attributes('questiontext')).toBe("What year is your vehicle?"); expect(radioQuestion.attributes('answers')).toBe("2023,2022,2021"); - expect(store.state.order.vehicle.year).toBe(2020); + expect(mockData.store.year).toBe(2020); }); -}); +}); \ No newline at end of file diff --git a/src/layouts/vehicle-year/year-question/year-question.vue b/src/layouts/vehicle-year/year-question/year-question.vue index 7be9d47f8..283e38bb7 100644 --- a/src/layouts/vehicle-year/year-question/year-question.vue +++ b/src/layouts/vehicle-year/year-question/year-question.vue @@ -8,9 +8,6 @@