import { globalEvents, globalEventTypes } from "@/constants/events"; import eventBus from "@/helpers/event-bus/event-bus"; import { useMainStore } from '@/store'; import { createTestingPinia } from '@pinia/testing'; const pinia = createTestingPinia(); useMainStore(pinia); useMainStore().addEventToBus = jest.fn(); useMainStore().removeEventFromBus = jest.fn(); useMainStore().eventBusItem = jest.fn(); describe("event-bus.js", () => { let event = { isDismissible: true, messageCopy: "You can get a quote by starting on this page.", messageHeadline: "We're sorry, something went wrong.", type: globalEventTypes.Danger, }; afterEach(() => { jest.resetAllMocks(); }) it("removes items when readandpop is called", () => { useMainStore().eventBusItem.mockReturnValueOnce(event); const eventValue = eventBus.readAndPopEventFromBus( globalEvents.Categories.GLOBAL_ALERT, globalEvents.SubCategories.PAGE_NOT_FOUND ); expect(useMainStore().eventBusItem).toBeCalledTimes(1); expect(useMainStore().removeEventFromBus).toBeCalledTimes(1); }); it("doesn't try to remove items when readandpop is called and item doesn't exist", () => { useMainStore().eventBusItem.mockReturnValueOnce(undefined); const eventValue = eventBus.readAndPopEventFromBus( globalEvents.Categories.GLOBAL_ALERT, globalEvents.SubCategories.PAGE_NOT_FOUND ); expect(useMainStore().eventBusItem).toBeCalledTimes(1); expect(useMainStore().removeEventFromBus).toBeCalledTimes(0); }); it("returns event from bus", () => { useMainStore().eventBusItem.mockReturnValueOnce(event); const eventValue = eventBus.readEventFromBus( globalEvents.Categories.GLOBAL_ALERT, globalEvents.SubCategories.PAGE_NOT_FOUND ); expect(eventValue).toBe(event); }); it("Reads event from bus, should have event value.", () => { // Arrange / Act eventBus.addEventToBus( globalEvents.Categories.GLOBAL_ALERT, globalEvents.SubCategories.PAGE_NOT_FOUND, event ); expect(useMainStore().addEventToBus).toHaveBeenCalled(); }); });