From 16fc60c12ab848ddab707d8d9a430bafb273337f Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 12 Nov 2021 16:36:02 -0500 Subject: [PATCH] Unit Test helper for mock functions --- src/helpers/unitTestHelper.js | 23 +++++++ src/layouts/selectYear/selectYear.spec.js | 74 ++++++++++++----------- 2 files changed, 63 insertions(+), 34 deletions(-) create mode 100644 src/helpers/unitTestHelper.js diff --git a/src/helpers/unitTestHelper.js b/src/helpers/unitTestHelper.js new file mode 100644 index 000000000..54c1e4d0e --- /dev/null +++ b/src/helpers/unitTestHelper.js @@ -0,0 +1,23 @@ +export function getMountOptions(mockData) { + // Define our mocks to attached to the 'global' object for Vue/Jest. + const mocks = {}; + + mocks.dispatchNonBlockingStoreAction = jest.fn(); + mocks.dispatchNonBlockingStoreAction.mockImplementation((actionName) => { + + let actionFilterResult = mockData.actionList.filter(x => x.actionName == actionName); + + if (actionFilterResult.length > 0 && actionFilterResult.length === 1) { + + return Promise.resolve({ + data: actionFilterResult[0].data + }); + } + }); + + const global = { + mocks: mocks + }; + + return { global } +} \ No newline at end of file diff --git a/src/layouts/selectYear/selectYear.spec.js b/src/layouts/selectYear/selectYear.spec.js index 760063ff9..d19ed0a47 100644 --- a/src/layouts/selectYear/selectYear.spec.js +++ b/src/layouts/selectYear/selectYear.spec.js @@ -1,48 +1,54 @@ import { shallowMount } from '@vue/test-utils'; +import { nextTick } from 'vue' +import { getMountOptions } from '@/helpers/unitTestHelper.js'; import selectYear from './selectYear.vue'; describe('selectYear.vue', () => { test("Should render the 'pageHeader.Text' data value 'text' prop value for the Header component, 'radioQuestion.Question' data value as 'questionText' prop value for the 'radioQuestion' component and 'years' values for 'answers' prop values for the 'radioQuestion' component.", async () => { - // Act - const mountOptions = getMountOptions({}); - const wrapper = shallowMount(selectYear, mountOptions); + // Arrange + const mockDataAndAction = { + actionList: [ + { + actionName: "getMockPageData", + data: { + "Result": [ + { + Type: "PageHeadingWidget", + Model: { + Text: "Mock Data" + } + }, + { + Type: "RadioQuestionWidget", + Model: { + Question: "Mock Data" + } + } + ] + } + }, + { + actionName: "getYears", + data: ['2023', '2022', '2021'] + } + ] + }; - await wrapper.setData({ - years: ['2023', '2022', '2021'], - pageHeader: { - Text: "Test page header" - }, - radioQuestion: { - Question: "Test question" - } - }); + const mountOptions = getMountOptions(mockDataAndAction); + + // Act + const wrapper = shallowMount(selectYear, mountOptions); + await nextTick() // Assert - const header = wrapper.findComponent('.Header'); - expect(header.attributes('text')).toEqual('Test page header'); + const header = await wrapper.find('.Header'); + expect(header.attributes('text')).toEqual("Mock Data"); - const radioQuestion = wrapper.findComponent('.radioQuestion'); + const radioQuestion = await wrapper.findComponent('.radioQuestion'); expect(radioQuestion.attributes('answers')).toEqual('2023,2022,2021'); - expect(radioQuestion.attributes('questiontext')).toEqual('Test question'); + expect(radioQuestion.attributes('questiontext')).toEqual("Mock Data"); }); - function getMountOptions({data}){ - let response = data; - const mocks = {}; - mocks.$store = { - getters: { - } - } - mocks.dispatchNonBlockingStoreAction = jest.fn(); - mocks.dispatchNonBlockingStoreAction.mockImplementation((actionName) => { - return Promise.resolve(response); - }); - const global = { - mocks: mocks - } - return {global} - - } -}) \ No newline at end of file +})