import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
import { dispatch } from "@/store";

jest.mock("@/store", () => ({
    dispatch: jest.fn(),
    state: {
        order: { vehicle: { year: "2019", make: "Acura" } },
    },
}));

describe("cms-content-helper.js", () => {
    it("Should return data from CMS", () => {
        // Arrange
        const cmsMockData = {
            Result: [
                {
                    Name: "FunnelHeaderWidget",
                    Model: {
                        HeaderText: "Select a year to get started",
                    },
                },
                {
                    Name: "FunnelSubHeaderWidget",
                    Model: {
                        HeaderText: "Select a model",
                    },
                },
                {
                    Name: "VehicleYearQuestion",
                    Model: {
                        QuestionText: "What year is your vehicle?",
                    },
                },
            ],
        };

        dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData }));

        // Act
        fetchCmsContentForPage("testPage").then((response) => {
            // Assert
            expect(response.FunnelHeaderWidget.HeaderText).toEqual("Select a year to get started");
        });
    });
});

describe("cms-content-helper.js", () => {
    it("Should replace strings for global state", () => {
        const cmsMockData = {
            Result: [
                {
                    Name: "FunnelSubHeaderWidget",
                    Model: {
                        HeaderText: "{globalState:order.vehicle.year}",
                    },
                },
            ],
        };

        dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData }));

        fetchCmsContentForPage("testPage").then((response) => {
            // Assert
            expect(response.FunnelSubHeaderWidget.HeaderText).toEqual("2019");
        });
    });
});

describe("cms-content-helper.js", () => {
    it("Should replace strings for global state, and leave others the same", () => {
        const cmsMockData = {
            Result: [
                {
                    Name: "FunnelSubHeaderWidget",
                    Model: {
                        HeaderText: "{globalState:order.vehicle.year}",
                    },
                },
                {
                    Name: "VehicleYearQuestion",
                    Model: {
                        ExampleText: "My widget value!",
                    },
                },
            ],
        };

        dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData }));

        fetchCmsContentForPage("testPage").then((response) => {
            // Assert
            expect(response.FunnelSubHeaderWidget.HeaderText).toEqual("2019");
            expect(response.VehicleYearQuestion.ExampleText).toEqual("My widget value!");
        });
    });
});

describe("cms-content-helper.js", () => {
    it("Should replace strings for global state in nested objects", () => {
        const cmsMockData = {
            Result: [
                {
                    Name: "FunnelSubHeaderWidget",
                    Model: {
                        HeaderText: "{globalState:order.vehicle.year}",
                    },
                },
                {
                    Name: "VehicleMakeQuestion",
                    Model: {
                        OtherObjectInside: {
                            ExampleText: "{globalState:order.vehicle.make}",
                        },
                    },
                },
            ],
        };

        dispatch.mockImplementation(() => Promise.resolve({ data: cmsMockData }));

        fetchCmsContentForPage("testPage").then((response) => {
            // Assert

            expect(response.FunnelSubHeaderWidget.HeaderText).toEqual("2019");
            expect(response.VehicleMakeQuestion.OtherObjectInside.ExampleText).toEqual("Acura");
        });
    });
});

test.todo("String cannot be mapped to global state");
