import { storeActions } from "@/constants/store-actions"; import store from "@/store"; import { isPhoneContactMethod, normalizePhoneNumberForStore, saveProgressPopupContactMethods, saveProgressPopupContactToStore, } from "./save-progress-popup-contact-helper"; jest.mock("@/store", () => ({ getters: { pageData: jest.fn(), }, })); describe("save-progress-popup-contact-helper", () => { beforeEach(() => { jest.clearAllMocks(); store.getters.pageData.mockReturnValue({ servicePackageSelected: "premium" }); }); describe("isPhoneContactMethod", () => { it("should return true for the phone contact method", () => { expect(isPhoneContactMethod(saveProgressPopupContactMethods.PHONE)).toBe(true); }); it("should return false for the email contact method", () => { expect(isPhoneContactMethod(saveProgressPopupContactMethods.EMAIL)).toBe(false); }); }); describe("normalizePhoneNumberForStore", () => { it("should strip non-digit characters from a formatted phone number", () => { expect(normalizePhoneNumberForStore("555-123-4567")).toBe("5551234567"); }); it("should return empty values unchanged", () => { expect(normalizePhoneNumberForStore("")).toBe(""); expect(normalizePhoneNumberForStore(null)).toBe(null); }); }); describe("saveProgressPopupContactToStore", () => { it("should save phone number and persist sms consent on phone tab", async () => { const dispatchStoreAction = jest.fn().mockResolvedValue(undefined); const smsConsent = { transactional: true, marketing: false }; await saveProgressPopupContactToStore(dispatchStoreAction, { contactMethod: saveProgressPopupContactMethods.PHONE, userInput: "555-123-4567", smsConsent, pageName: "quote", }); expect(dispatchStoreAction).toHaveBeenCalledWith( storeActions.SAVE_PHONE_NUMBER, "5551234567", false ); expect(dispatchStoreAction).not.toHaveBeenCalledWith( storeActions.SAVE_EMAIL, expect.anything(), false ); expect(dispatchStoreAction).toHaveBeenCalledWith( storeActions.SAVE_IS_SMS_OPT_IN, true, false ); expect(dispatchStoreAction).toHaveBeenCalledWith( storeActions.SAVE_PAGE_DATA, { page: "quote", data: { servicePackageSelected: "premium", saveProgressSmsConsent: smsConsent, }, }, false ); }); it("should save email on email tab without updating phone or sms opt in", async () => { const dispatchStoreAction = jest.fn().mockResolvedValue(undefined); await saveProgressPopupContactToStore(dispatchStoreAction, { contactMethod: saveProgressPopupContactMethods.EMAIL, userInput: "test@example.com", smsConsent: { transactional: false, marketing: false }, pageName: "quote", }); expect(dispatchStoreAction).toHaveBeenCalledWith( storeActions.SAVE_EMAIL, "test@example.com", false ); expect(dispatchStoreAction).not.toHaveBeenCalledWith( storeActions.SAVE_PHONE_NUMBER, expect.anything(), false ); expect(dispatchStoreAction).not.toHaveBeenCalledWith( storeActions.SAVE_IS_SMS_OPT_IN, expect.anything(), false ); expect(dispatchStoreAction).not.toHaveBeenCalledWith( storeActions.SAVE_PAGE_DATA, expect.anything(), false ); }); }); });