Unit Test helper for mock functions

This commit is contained in:
Frank 2021-11-12 16:36:02 -05:00
parent 968ade9afa
commit 16fc60c12a
2 changed files with 63 additions and 34 deletions

View file

@ -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 }
}

View file

@ -1,48 +1,54 @@
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue'
import { getMountOptions } from '@/helpers/unitTestHelper.js';
import selectYear from './selectYear.vue'; import selectYear from './selectYear.vue';
describe('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 () => { 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 // Arrange
const mountOptions = getMountOptions({}); const mockDataAndAction = {
const wrapper = shallowMount(selectYear, mountOptions); 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({ const mountOptions = getMountOptions(mockDataAndAction);
years: ['2023', '2022', '2021'],
pageHeader: { // Act
Text: "Test page header" const wrapper = shallowMount(selectYear, mountOptions);
}, await nextTick()
radioQuestion: {
Question: "Test question"
}
});
// Assert // Assert
const header = wrapper.findComponent('.Header'); const header = await wrapper.find('.Header');
expect(header.attributes('text')).toEqual('Test page 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('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}
}
}) })