Refactored unit tests
This commit is contained in:
parent
904df04ac7
commit
f98b7f3f48
2 changed files with 149 additions and 81 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { shallowMount, flushPromises } from "@vue/test-utils";
|
import { shallowMount, flushPromises } from "@vue/test-utils";
|
||||||
import { getMountOptions } from "@/helpers/unit-test-helper.js";
|
import { getMountOptions } from "@/helpers/unit-test-helper.js";
|
||||||
import vehicleYear from "@/layouts/vehicle-year/vehicle-year.vue";
|
import vehicleYear from "@/layouts/vehicle-year/vehicle-year.vue";
|
||||||
|
import yearQuestion from "@/layouts/vehicle-year/year-question/year-question";
|
||||||
import { settleAllPromises } from "@/helpers/layout-helper.js";
|
import { settleAllPromises } from "@/helpers/layout-helper.js";
|
||||||
import { nextTick } from "vue";
|
import { nextTick } from "vue";
|
||||||
|
|
||||||
|
|
@ -10,72 +11,84 @@ jest.mock("@/helpers/layout-helper.js", () => ({
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe("vehicle-year.vue", () => {
|
describe("vehicle-year.vue", () => {
|
||||||
test("vehicle-year.vue should render data from CMS", async () => {
|
test("Year question component is initized with api data", async (done) => {
|
||||||
// Arrange
|
|
||||||
|
|
||||||
// Our mock data for our call to settleAllPromises
|
//Arange
|
||||||
const mockData = {
|
const radioQuestionCmsContent = { QuestionText: "What year is your vehicle?" };
|
||||||
getPageContent: {
|
const yearQuestionInitialData = ["2023", "2022", "2021"];
|
||||||
PageHeaderWidget: [{ HeaderText: "Select a year to get started" }],
|
const { wrapper, apiPromise } = setupMocks( {
|
||||||
RadioQuestionWidget: [{ QuestionText: "What year is your vehicle?" }],
|
radioQuestionCmsContent: radioQuestionCmsContent,
|
||||||
VehicleBannerWidget: [
|
yearQuestionInitialData: yearQuestionInitialData,
|
||||||
{
|
} );
|
||||||
GenericVehicleImage:
|
|
||||||
"https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/blurred-image.jpg?sfvrsn=a6ce3034_3",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
SiteHeaderWidget: [
|
|
||||||
{
|
|
||||||
LogoImage:
|
|
||||||
"https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/safelite-logo.svg?sfvrsn=45e7ed06_3",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
isCmsContentReady: true,
|
|
||||||
},
|
|
||||||
getVehicleYear: [2023, 2022, 2021],
|
|
||||||
store: {
|
|
||||||
commit: jest.fn(),
|
|
||||||
year: null,
|
|
||||||
},
|
|
||||||
router: {
|
|
||||||
push: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// our router information needed.
|
//Act
|
||||||
const to = {
|
vehicleYear.beforeRouteEnter.call(wrapper.vm, { query: { fmgPage: "vehicle-year" } }, undefined, (c) => c(wrapper.vm));
|
||||||
query: {
|
|
||||||
fmgPage: "vehicle-year",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const mountOptions = getMountOptions(mockData);
|
//Assert
|
||||||
|
apiPromise.finally(() => {
|
||||||
// our mock implementation of settleAllPromises
|
expect(yearQuestion.methods.initializeComponent).toHaveBeenCalledWith(radioQuestionCmsContent, yearQuestionInitialData);
|
||||||
settleAllPromises.mockImplementation(() => {
|
done();
|
||||||
return Promise.resolve(mockData);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Act
|
|
||||||
const wrapper = shallowMount(vehicleYear, mountOptions);
|
|
||||||
wrapper.vm.$options.watch.selectedYear.call(wrapper.vm);
|
|
||||||
|
|
||||||
// Call our beforeRouteEnter on the component.
|
|
||||||
// This passes (c) => c(wrapper.vm) so that next can be called and our
|
|
||||||
// data can be set.
|
|
||||||
vehicleYear.beforeRouteEnter.call(wrapper.vm, to, undefined, (c) =>
|
|
||||||
c(wrapper.vm)
|
|
||||||
);
|
|
||||||
|
|
||||||
await nextTick(); // Wait for the DOM to update.
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const header = await wrapper.find(".Header");
|
|
||||||
expect(header.attributes("text")).toEqual("Select a year to get started");
|
|
||||||
|
|
||||||
const yearQuestion = wrapper.findComponent({ name: "year-question" });
|
|
||||||
expect(yearQuestion.attributes("questiontext")).toEqual(
|
|
||||||
"What year is your vehicle?"
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("vehicle-year.vue", () => {
|
||||||
|
test("Page header is passed data from CMS", async (done) => {
|
||||||
|
|
||||||
|
//Arange
|
||||||
|
const { wrapper, apiPromise } = setupMocks( { pageHeaderWidgetHeaderText: "Select a year to get started" });
|
||||||
|
|
||||||
|
//Act
|
||||||
|
vehicleYear.beforeRouteEnter.call(wrapper.vm, { query: { fmgPage: "vehicle-year" } }, undefined, (c) => c(wrapper.vm));
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
const header = await wrapper.find(".Header");
|
||||||
|
apiPromise.finally(() => {
|
||||||
|
expect(header.attributes("text")).toEqual("Select a year to get started");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function setupMocks({
|
||||||
|
radioQuestionCmsContent = {},
|
||||||
|
yearQuestionInitialData = {},
|
||||||
|
pageHeaderWidgetHeaderText = {},
|
||||||
|
}) {
|
||||||
|
|
||||||
|
//Mock api responses
|
||||||
|
const apiResponses = {
|
||||||
|
cmsContent: {
|
||||||
|
PageHeaderWidget: [{ HeaderText: pageHeaderWidgetHeaderText }],
|
||||||
|
RadioQuestionWidget: [radioQuestionCmsContent],
|
||||||
|
VehicleBannerWidget: [
|
||||||
|
{
|
||||||
|
GenericVehicleImage:
|
||||||
|
"https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/blurred-image.jpg?sfvrsn=a6ce3034_3",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
SiteHeaderWidget: [
|
||||||
|
{
|
||||||
|
LogoImage:
|
||||||
|
"https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/safelite-logo.svg?sfvrsn=45e7ed06_3",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isCmsContentReady: true,
|
||||||
|
},
|
||||||
|
yearQuestionInitialData: yearQuestionInitialData,
|
||||||
|
};
|
||||||
|
const apiPromise = Promise.resolve(apiResponses);
|
||||||
|
settleAllPromises.mockImplementation(() => apiPromise);
|
||||||
|
|
||||||
|
//Mock year question methods
|
||||||
|
yearQuestion.methods = {
|
||||||
|
loadInitialData: jest.fn(),
|
||||||
|
initializeComponent: jest.fn(),
|
||||||
|
};
|
||||||
|
const mountOptions = getMountOptions({ });
|
||||||
|
const wrapper = shallowMount(vehicleYear, mountOptions);
|
||||||
|
const yearQuestionWrapper = wrapper.findComponent({ name: "yearQuestion" });
|
||||||
|
yearQuestionWrapper.vm.initializeComponent = yearQuestion.methods.initializeComponent;
|
||||||
|
|
||||||
|
return { wrapper, apiPromise };
|
||||||
|
}
|
||||||
|
|
@ -1,25 +1,80 @@
|
||||||
import yearQuestion from "@/layouts/vehicle-year/year-question/year-question";
|
import yearQuestion from "@/layouts/vehicle-year/year-question/year-question";
|
||||||
import { shallowMount } from "@vue/test-utils";
|
import { shallowMount } from "@vue/test-utils";
|
||||||
|
import { getMountOptions } from "@/helpers/unit-test-helper.js";
|
||||||
|
import store from "@/store";
|
||||||
|
jest.mock("@/store", () => { return {}; }, {virtual: true});
|
||||||
|
|
||||||
describe("year-question.vue", () => {
|
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 () => {
|
test("Selected year is emitted upon selection.", async () => {
|
||||||
// Act
|
|
||||||
const wrapper = shallowMount(yearQuestion);
|
|
||||||
await wrapper.setProps({
|
|
||||||
questionText: "What year is your vehicle?",
|
|
||||||
years: ["2023", "2022", "2021"],
|
|
||||||
modelValue: "2020",
|
|
||||||
});
|
|
||||||
wrapper.vm.$options.watch.selectedYear.call(wrapper.vm);
|
|
||||||
|
|
||||||
// Assert
|
//Arrange
|
||||||
const radioQuestion = await wrapper.findComponent({
|
const { wrapper } = setupMocks({ modelValueProp: "2020" });
|
||||||
name: "radioQuestion",
|
const yearToSelect = "2021";
|
||||||
});
|
|
||||||
expect(radioQuestion.attributes("questiontext")).toBe(
|
//Act
|
||||||
"What year is your vehicle?"
|
wrapper.setData({ selectedYear: yearToSelect });
|
||||||
);
|
await wrapper.vm.$nextTick();
|
||||||
expect(radioQuestion.attributes("answers")).toBe("2023,2022,2021");
|
|
||||||
expect(wrapper.componentVM.modelValue).toBe("2020");
|
//Assert
|
||||||
|
expect(wrapper.emitted()["update:modelValue"][0]).toEqual(["2021"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("year-question.vue", () => {
|
||||||
|
test("CMS question text is used as radio question text.", async () => {
|
||||||
|
|
||||||
|
//Arrange
|
||||||
|
const { wrapper, cmsContent } = setupMocks({ cmsQuestionText: "What year is your vehicle?" });
|
||||||
|
|
||||||
|
//Act
|
||||||
|
yearQuestion.methods.initializeComponent.call(wrapper.vm, cmsContent, null);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
const radioQuestionComponent = await wrapper.findComponent({ name: "radioQuestion" });
|
||||||
|
expect(radioQuestionComponent.attributes("questiontext")).toBe("What year is your vehicle?");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("year-question.vue", () => {
|
||||||
|
test("Data from store api are used as radio question answers.", async () => {
|
||||||
|
|
||||||
|
//Arrange
|
||||||
|
const { wrapper, cmsContent } = setupMocks({ dataFromStoreApi: ["2023", "2022", "2021"] });
|
||||||
|
|
||||||
|
//Act
|
||||||
|
const initialData = yearQuestion.methods.loadInitialData.call(wrapper.vm);
|
||||||
|
yearQuestion.methods.initializeComponent.call(wrapper.vm, cmsContent, initialData);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
const radioQuestionComponent = await wrapper.findComponent({ name: "radioQuestion" });
|
||||||
|
expect(radioQuestionComponent.attributes("answers")).toBe("2023,2022,2021");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function setupMocks({
|
||||||
|
modelValueProp = "1900",
|
||||||
|
cmsQuestionText = "CMS text goes here",
|
||||||
|
dataFromStoreApi = [],
|
||||||
|
}) {
|
||||||
|
|
||||||
|
//Mock store
|
||||||
|
store.dispatch = jest.fn(() => dataFromStoreApi);
|
||||||
|
const mountOptions = getMountOptions({
|
||||||
|
store: {
|
||||||
|
dispatch: store.dispatch,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
//Mock props
|
||||||
|
mountOptions.propsData = {
|
||||||
|
modelValue: modelValueProp,
|
||||||
|
};
|
||||||
|
const wrapper = shallowMount(yearQuestion, mountOptions);
|
||||||
|
|
||||||
|
//Mock CMS content
|
||||||
|
const cmsContent = {
|
||||||
|
QuestionText: cmsQuestionText
|
||||||
|
};
|
||||||
|
return { wrapper, cmsContent };
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue