Merge pull request #97 from Safelite/archived/apiRefactoring-encapsulated
Archived/api refactoring encapsulated
This commit is contained in:
commit
20fbdb588c
4 changed files with 177 additions and 105 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 };
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
<div class="select-car-form rounded text-center">
|
<div class="select-car-form rounded text-center">
|
||||||
<vehicleBanner :vehicleImageSrc="vehicleBannerWidget.GenericVehicleImage" />
|
<vehicleBanner :vehicleImageSrc="vehicleBannerWidget.GenericVehicleImage" />
|
||||||
<pageHeader :text="pageHeaderWidgets.HeaderText" class="Header" />
|
<pageHeader :text="pageHeaderWidgets.HeaderText" class="Header" />
|
||||||
<yearQuestion :questionText="radioQuestionWidgets.QuestionText" :years="vehicleYears" v-model="selectedYear" />
|
<yearQuestion v-model="selectedYear" ref="yearQuestion" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -20,15 +20,11 @@ import vehicleBanner from "@/common-components/vehicle-banner/vehicle-banner";
|
||||||
// Supporting files
|
// Supporting files
|
||||||
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
|
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
|
||||||
import { settleAllPromises } from "@/helpers/layout-helper";
|
import { settleAllPromises } from "@/helpers/layout-helper";
|
||||||
import store from "@/store";
|
|
||||||
import { storeActions } from "@/constants/store-actions.js";
|
|
||||||
export default {
|
export default {
|
||||||
name: "vehicle-year",
|
name: "vehicle-year",
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
pageHeaderWidgets: {},
|
pageHeaderWidgets: {},
|
||||||
radioQuestionWidgets: {},
|
|
||||||
vehicleYears: [],
|
|
||||||
siteHeaderWidget: {},
|
siteHeaderWidget: {},
|
||||||
vehicleBannerWidget: {},
|
vehicleBannerWidget: {},
|
||||||
selectedYear: null,
|
selectedYear: null,
|
||||||
|
|
@ -37,34 +33,30 @@ export default {
|
||||||
computed: {},
|
computed: {},
|
||||||
|
|
||||||
beforeRouteEnter(to, from, next) {
|
beforeRouteEnter(to, from, next) {
|
||||||
|
|
||||||
// Call APIs
|
// Call APIs
|
||||||
const contentPromise = fetchCmsContentForPage(to.query.fmgPage);
|
const cmsContentPromise = fetchCmsContentForPage(to.query.fmgPage);
|
||||||
const getVehicleYearPromise = store.dispatch(
|
const yearQuestionInitialDataPromise = yearQuestion.methods.loadInitialData();
|
||||||
storeActions.GET_VEHICLE_YEARS,
|
|
||||||
{}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Settle promises and get results
|
// Settle promises and get results
|
||||||
const promiseResultMap = [
|
const promiseResultMap = [
|
||||||
{
|
{
|
||||||
resultKey: "getPageContent",
|
resultKey: "cmsContent",
|
||||||
promise: contentPromise,
|
promise: cmsContentPromise,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
resultKey: "getVehicleYear",
|
resultKey: "yearQuestionInitialData",
|
||||||
promise: getVehicleYearPromise,
|
promise: yearQuestionInitialDataPromise,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
settleAllPromises(promiseResultMap).then((resultMap) => {
|
settleAllPromises(promiseResultMap).then((resultMap) => {
|
||||||
|
|
||||||
// Call the "next" function to complete the transition to this page.
|
// Call the "next" function to complete the transition to this page.
|
||||||
next((vm) => {
|
next((vm) => {
|
||||||
vm.pageHeaderWidgets = resultMap.getPageContent.PageHeaderWidget[0];
|
vm.pageHeaderWidgets = resultMap.cmsContent.PageHeaderWidget[0];
|
||||||
vm.siteHeaderWidget = resultMap.getPageContent.SiteHeaderWidget[0];
|
vm.siteHeaderWidget = resultMap.cmsContent.SiteHeaderWidget[0];
|
||||||
vm.radioQuestionWidgets =
|
vm.vehicleBannerWidget = resultMap.cmsContent.VehicleBannerWidget[0];
|
||||||
resultMap.getPageContent.RadioQuestionWidget[0];
|
vm.$refs.yearQuestion.initializeComponent(resultMap.cmsContent.RadioQuestionWidget[0], resultMap.yearQuestionInitialData);
|
||||||
vm.vehicleBannerWidget =
|
|
||||||
resultMap.getPageContent.VehicleBannerWidget[0];
|
|
||||||
vm.vehicleYears = resultMap.getVehicleYear;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -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: "buttonQuestion",
|
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 buttonQuestionComponent = await wrapper.findComponent({ name: "buttonQuestion" });
|
||||||
|
expect(buttonQuestionComponent.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 buttonQuestionComponent = await wrapper.findComponent({ name: "buttonQuestion" });
|
||||||
|
expect(buttonQuestionComponent.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 };
|
||||||
|
}
|
||||||
|
|
@ -9,22 +9,34 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import buttonQuestion from "@/common-components/button-question/button-question";
|
import buttonQuestion from "@/common-components/button-question/button-question";
|
||||||
|
// Supporting files
|
||||||
|
import store from "@/store";
|
||||||
|
import { storeActions } from "@/constants/store-actions.js";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "year-question",
|
name: "year-question",
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
questionText: null,
|
||||||
selectedYear: null,
|
selectedYear: null,
|
||||||
};
|
years: Array,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
questionText: String,
|
|
||||||
years: Array,
|
|
||||||
modelValue: String,
|
modelValue: String,
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
buttonQuestion,
|
buttonQuestion,
|
||||||
},
|
},
|
||||||
|
methods: {
|
||||||
|
loadInitialData() {
|
||||||
|
return store.dispatch(storeActions.GET_VEHICLE_YEARS, {});
|
||||||
|
},
|
||||||
|
initializeComponent(cmsContent, initialData) {
|
||||||
|
this.questionText = cmsContent.QuestionText;
|
||||||
|
this.years = initialData;
|
||||||
|
}
|
||||||
|
},
|
||||||
watch: {
|
watch: {
|
||||||
selectedYear(val) {
|
selectedYear(val) {
|
||||||
this.$emit("update:modelValue", val);
|
this.$emit("update:modelValue", val);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue