From 8958dfe0bb6070df6a44e0f7fd6f2b0f86304bd3 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 17 Dec 2021 13:11:59 -0500 Subject: [PATCH] Unit tests updates --- jest.config.js | 2 +- src/helpers/cms-helper.spec.js | 170 +++++++++++++++++++++++++-------- 2 files changed, 130 insertions(+), 42 deletions(-) diff --git a/jest.config.js b/jest.config.js index e36f1a70d..fd7713966 100644 --- a/jest.config.js +++ b/jest.config.js @@ -16,7 +16,7 @@ module.exports = { testMatch: ["**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"], coverageThreshold: { global: { - statements: 70, + statements: 80, }, }, }; diff --git a/src/helpers/cms-helper.spec.js b/src/helpers/cms-helper.spec.js index e9bd66653..0f03ab604 100644 --- a/src/helpers/cms-helper.spec.js +++ b/src/helpers/cms-helper.spec.js @@ -3,50 +3,138 @@ import { dispatch } from "@/store"; jest.mock("@/store", () => ({ dispatch: jest.fn(), + state: { + order: { vehicle: { year: "2019", make: "Acura" } } + } })); -it("cms-content-helper: Should return data from CMS", () => { - // Arrange - const cmsMockData = { - Result: [ - { - Type: "VehicleBannerWidget", - Model: { - ImageId: "28452dcb-7762-4cc9-ab09-7643d0b89203", - GenericVehicleImage: - "https://digitalconsumercms-dev.safelite.com/images/default-source/default-album/blurred-image.jpg?sfvrsn=a6ce3034_3", - GenericVehicleImageFilePath: - "images/default-source/default-album/blurred-image.jpg", - }, - }, - { - Type: "PageHeaderWidget", - Model: { - HeaderText: "Select a year to get started", - }, - }, - { - Type: "PageHeaderWidget", - Model: { - HeaderText: "Select a model", - }, - }, - { - Type: "RadioQuestionWidget", - Model: { - QuestionText: "What year is your vehicle?", - }, - }, - ], - }; - dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData })); +describe("cms-content-helper.js", () => { + it("Should return data from CMS", () => { + // Arrange + const cmsMockData = { + Result: [ + { + Type: "PageHeaderWidget", + Model: { + HeaderText: "Select a year to get started", + }, + }, + { + Type: "PageHeaderWidget", + Model: { + HeaderText: "Select a model", + }, + }, + { + Type: "RadioQuestionWidget", + Model: { + QuestionText: "What year is your vehicle?", + }, + }, + ], + }; + + dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData })); + + // Act + fetchCmsContentForPage("testPage").then((response) => { + // Assert + expect(response.PageHeaderWidget[0].HeaderText).toEqual( + "Select a year to get started" + ); + }); + }); - // Act - fetchCmsContentForPage("testPage").then((response) => { - // Assert - expect(response.PageHeaderWidget[0].HeaderText).toEqual( - "Select a year to get started" - ); +}); + +describe("cms-content-helper.js", () => { + it("Should replace strings for global state", () => { + const cmsMockData = { + Result: [ + { + Type: "PageHeaderWidget", + Model: { + HeaderText: "{globalState:order.vehicle.year}", + }, + }, + ], + }; + + dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData })); + + fetchCmsContentForPage("testPage").then((response) => { + // Assert + expect(response.PageHeaderWidget[0].HeaderText).toEqual( + "2019" + ); + }); }); }); + +describe("cms-content-helper.js", () => { + it("Should replace strings for global state, and leave others the same", () => { + + const cmsMockData = { + Result: [ + { + Type: "PageHeaderWidget", + Model: { + HeaderText: "{globalState:order.vehicle.year}", + }, + }, + { + Type: "RadioQuestionWidget", + Model: { + ExampleText: "My widget value!", + }, + }, + ], + }; + + dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData })); + + fetchCmsContentForPage("testPage").then((response) => { + // Assert + expect(response.PageHeaderWidget[0].HeaderText).toEqual("2019"); + expect(response.RadioQuestionWidget[0].ExampleText).toEqual("My widget value!"); + + }); + }); +}); + +describe("cms-content-helper.js", () => { + it("Should replace strings for global state in nested objects", () => { + + const cmsMockData = { + Result: [ + { + Type: "PageHeaderWidget", + Model: { + HeaderText: "{globalState:order.vehicle.year}", + }, + }, + { + Type: "RadioQuestionWidget", + Model: { + OtherObjectInside: { + ExampleText: "{globalState:order.vehicle.make}", + } + }, + }, + ], + }; + + dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData })); + + fetchCmsContentForPage("testPage").then((response) => { + // Assert + + expect(response.PageHeaderWidget[0].HeaderText).toEqual("2019"); + expect(response.RadioQuestionWidget[0].OtherObjectInside.ExampleText).toEqual("Acura"); + + }); + }); +}); + +test.todo("String cannot be mapped to global state"); \ No newline at end of file