From b1ac5130e82a6bdd066e71fb9716738a10731667 Mon Sep 17 00:00:00 2001 From: Jason Wheeler Date: Fri, 31 Mar 2023 10:51:30 -0400 Subject: [PATCH] added modal vue and started test page --- src/digital-components/modal/modal.spec.js | 269 +++++++++++++++--- src/digital-components/modal/modal.vue | 125 +++++--- src/digital-components/modal/modal_old.vue | 144 ++++++++++ src/helpers/unit-test-helper.js | 2 + .../content-group-modal.spec.js | 67 +++++ .../content-group-modal.vue | 86 ++++++ src/layouts/entry-page/entry-page.vue | 4 +- .../part-questions/part-questions.spec.js | 2 +- src/layouts/z-test-page-jg/z-test-page-jg.vue | 87 ++++++ .../list-button/list-button.spec.js | 6 +- .../modal-button-main.spec.js | 188 ++++++++++++ .../modal-button-main/modal-button-main.vue | 153 ++++++++++ 12 files changed, 1043 insertions(+), 90 deletions(-) create mode 100644 src/digital-components/modal/modal_old.vue create mode 100644 src/iss-components/content-group-modal/content-group-modal.spec.js create mode 100644 src/iss-components/content-group-modal/content-group-modal.vue create mode 100644 src/layouts/z-test-page-jg/z-test-page-jg.vue create mode 100644 src/ux-components/modal-button-main/modal-button-main.spec.js create mode 100644 src/ux-components/modal-button-main/modal-button-main.vue diff --git a/src/digital-components/modal/modal.spec.js b/src/digital-components/modal/modal.spec.js index 2664accf..e4d8692d 100644 --- a/src/digital-components/modal/modal.spec.js +++ b/src/digital-components/modal/modal.spec.js @@ -1,68 +1,255 @@ +jest.mock("vee-validate", () => ({ + useForm: jest.fn(), +})); + +const mockValidate = (returnValue) => jest.fn(async () => Promise.resolve({ valid: returnValue })); +const mockMeta = (returnValue) => jest.fn(async () => Promise.resolve(returnValue)); + import { shallowMount } from "@vue/test-utils"; -import Modal from "./modal"; +import modal from "./modal"; +import crypto from "crypto"; +import { useForm } from "vee-validate"; +import { Modal } from "bootstrap"; + +const footerButtonText = "Sample footer text here."; +const headerText = "Sample header text here."; + +global.crypto = crypto; describe("modal.vue", () => { - it("Should display header text when HeaderText is defined in the CMS", async () => { - // Act - const wrapper = shallowMount(Modal, { - mixins: [mockMixin], + it("Should display modal header text when headerText is defined", async () => { + // Arrange / Act + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + + const wrapper = shallowMount(modal, { props: { - cmsWidgetName: "test", + headerText: headerText, + footerButtonText: footerButtonText, }, attachTo: document.body, }); - expect(wrapper.html()).toEqual(expect.stringContaining(mockCmsContent["HeaderText"])); + + // Assert + expect(wrapper.html()).toEqual(expect.stringContaining(headerText)); }); - it("Should display subheader text when SubheaderText is defined in the CMS", async () => { - // Act - const wrapper = shallowMount(Modal, { - mixins: [mockMixin], + it("Should display footer button text when footerButtonText is defined", async () => { + // Arrange / Act + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + const wrapper = shallowMount(modal, { props: { - cmsWidgetName: "test", + footerButtonText: footerButtonText, }, attachTo: document.body, }); - expect(wrapper.html()).toEqual(expect.stringContaining(mockCmsContent["SubheaderText"])); + + // Assert + expect(wrapper.html()).toEqual(expect.stringContaining(footerButtonText)); }); - it("Should insert image url when Image is defined in the CMS", async () => { - // Act - const wrapper = shallowMount(Modal, { - mixins: [mockMixin], + it("Should emit 'footer-button-event' if the form is valid", async () => { + // Arrange + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + + const resetButtonStyle = jest.fn(); + const wrapper = shallowMount(modal, { props: { - cmsWidgetName: "test", + footerButtonText: footerButtonText, + headerText: headerText, }, attachTo: document.body, }); - expect(wrapper.html()).toEqual(expect.stringContaining(mockCmsContent["Image"])); + wrapper.vm.resetButtonStyle = resetButtonStyle; + + // Act + const buttonMain = wrapper.findComponent({ ref: "modalButtonMain" }); + await buttonMain.trigger("click-event"); + + // Assert + expect(wrapper.emitted("footer-button-event")).toBeTruthy(); }); - it("Should display body text when BodyText is defined in the CMS", async () => { - // Act - const wrapper = shallowMount(Modal, { - mixins: [mockMixin], + it("Should not emit 'footer-button-event' if the form is invalid", async () => { + // Arrange + const fakeMeta = { + touched: true, + dirty: true, + valid: false, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(false), + }); + + const resetButtonStyle = jest.fn(); + const wrapper = shallowMount(modal, { props: { - cmsWidgetName: "test", + footerButtonText: footerButtonText, + headerText: headerText, }, attachTo: document.body, }); - expect(wrapper.html()).toEqual(expect.stringContaining(mockCmsContent["BodyText"])); + wrapper.vm.resetButtonStyle = resetButtonStyle; + + // Act + const buttonMain = wrapper.findComponent({ ref: "modalButtonMain" }); + await buttonMain.trigger("click-event"); + + // Assert + expect(wrapper.emitted("footer-button-event")).toBeFalsy(); + }); + + it("Should have a disabled footer button when the form has not been touched", async () => { + // Arrange / Act + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + + const resetButtonStyle = jest.fn(); + const wrapper = shallowMount(modal, { + props: { + footerButtonText: footerButtonText, + headerText: headerText, + }, + attachTo: document.body, + }); + wrapper.vm.resetButtonStyle = resetButtonStyle; + + // Assert + expect(wrapper.vm.isFooterButtonDisabled).toBe(true); + }); + + it("Should have a disabled footer button when the form is invalid", async () => { + // Arrange / Act + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + + const resetButtonStyle = jest.fn(); + const wrapper = shallowMount(modal, { + props: { + footerButtonText: footerButtonText, + headerText: headerText, + }, + attachTo: document.body, + }); + wrapper.vm.resetButtonStyle = resetButtonStyle; + + // Assert + expect(wrapper.vm.isFooterButtonDisabled).toBe(true); + }); + + it("Should call bootstrap Modal method 'show' when calling 'openModal'", async () => { + // Arrange + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + + const showMock = jest.spyOn(Modal.prototype, "show"); + + const resetButtonStyle = jest.fn(); + const wrapper = shallowMount(modal, { + props: { + footerButtonText: footerButtonText, + headerText: headerText, + }, + attachTo: document.body, + }); + wrapper.vm.resetButtonStyle = resetButtonStyle; + + // Act + wrapper.vm.openModal(); + + // Assert + expect(showMock).toHaveBeenCalled(); + }); + + it("Should call bootstrap Modal method 'hide' when calling 'closeModal'", async () => { + // Arrange + const fakeMeta = { + touched: true, + dirty: true, + valid: true, + validated: true, + }; + + useForm.mockReturnValue({ + meta: mockMeta(fakeMeta), + validate: mockValidate(true), + }); + const hideMock = jest.spyOn(Modal.prototype, "hide"); + + const resetButtonStyle = jest.fn(); + const wrapper = shallowMount(modal, { + props: { + footerButtonText: footerButtonText, + headerText: headerText, + }, + attachTo: document.body, + }); + wrapper.vm.resetButtonStyle = resetButtonStyle; + + // Act + wrapper.vm.openModal(); + wrapper.vm.closeModal(); + + // Assert + expect(hideMock).toHaveBeenCalled(); }); }); - -const mockMixin = { - methods: { - getCmsContent: jest.fn((widgetName, cmsFieldName) => { - return mockCmsContent[cmsFieldName]; - }), - }, -}; - -const mockCmsContent = { - HeaderText: "Sample header text here.", - SubheaderText: "Sample subheader text here.", - Image: "https://www.sampleImage.sample", - BodyText: "Sample body text here.", - FooterText: "Sample footer text here.", -}; diff --git a/src/digital-components/modal/modal.vue b/src/digital-components/modal/modal.vue index 8d961830..ba7e60eb 100644 --- a/src/digital-components/modal/modal.vue +++ b/src/digital-components/modal/modal.vue @@ -2,39 +2,38 @@