|
| 1 | +import { eventCreator } from "./index"; |
| 2 | +import { EventHandler } from "../../types/event/EventHandlers"; |
| 3 | +import { EventCreatorParams } from "../../types/event/EventCreatorParams"; |
| 4 | + |
| 5 | +describe("eventCreator", () => { |
| 6 | + const mockCallback = jest.fn(); |
| 7 | + |
| 8 | + const defaultParams: EventCreatorParams = { |
| 9 | + callback: mockCallback, |
| 10 | + options: { |
| 11 | + page: { |
| 12 | + currentPage: "Home", |
| 13 | + showOnLabel: true, |
| 14 | + showOnMetadata: true, |
| 15 | + }, |
| 16 | + action: { |
| 17 | + possibleActions: ["click", "scroll"], |
| 18 | + showOnMetadata: true, |
| 19 | + }, |
| 20 | + element: { |
| 21 | + possibleElements: ["button", "link"], |
| 22 | + showOnMetadata: true, |
| 23 | + showElementIdOnLabel: true, |
| 24 | + }, |
| 25 | + labelOptions: { |
| 26 | + stringCase: "capitalize", |
| 27 | + stringFormat: "snake_case", |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should create handlers for all elements and actions", () => { |
| 37 | + const handlers: EventHandler = eventCreator(defaultParams); |
| 38 | + |
| 39 | + expect(handlers).toHaveProperty("button"); |
| 40 | + expect(handlers).toHaveProperty("link"); |
| 41 | + expect(handlers.button).toHaveProperty("click"); |
| 42 | + expect(handlers.button).toHaveProperty("scroll"); |
| 43 | + expect(handlers.link).toHaveProperty("click"); |
| 44 | + expect(handlers.link).toHaveProperty("scroll"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should call the callback with the correct label and metadata", () => { |
| 48 | + const handlers: EventHandler = eventCreator(defaultParams); |
| 49 | + |
| 50 | + const eventMetadata = { elementId: "123", customData: "test" }; |
| 51 | + handlers.button.click(eventMetadata); |
| 52 | + |
| 53 | + expect(mockCallback).toHaveBeenCalledWith({ |
| 54 | + label: "Home_Button_Click_123", |
| 55 | + metadata: { |
| 56 | + ...eventMetadata, |
| 57 | + page: "Home", |
| 58 | + element: "button", |
| 59 | + action: "click", |
| 60 | + }, |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should exclude page from metadata if showOnMetadata is false", () => { |
| 65 | + const params = { |
| 66 | + ...defaultParams, |
| 67 | + options: { |
| 68 | + ...defaultParams.options, |
| 69 | + page: { |
| 70 | + ...defaultParams.options?.page, |
| 71 | + showOnMetadata: false, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + const handlers: EventHandler = eventCreator(params); |
| 77 | + |
| 78 | + const eventMetadata = { elementId: "789" }; |
| 79 | + handlers.button.click(eventMetadata); |
| 80 | + |
| 81 | + expect(mockCallback).toHaveBeenCalledWith({ |
| 82 | + label: "Home_Button_Click_789", |
| 83 | + metadata: { |
| 84 | + ...eventMetadata, |
| 85 | + element: "button", |
| 86 | + action: "click", |
| 87 | + }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle default actions and elements if none are provided", () => { |
| 92 | + const params = { |
| 93 | + callback: mockCallback, |
| 94 | + options: {}, |
| 95 | + }; |
| 96 | + |
| 97 | + const handlers: EventHandler = eventCreator(params); |
| 98 | + |
| 99 | + expect(Object.keys(handlers).length).toBeGreaterThan(0); |
| 100 | + expect( |
| 101 | + Object.keys(handlers[Object.keys(handlers)[0]]).length |
| 102 | + ).toBeGreaterThan(0); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should format the label correctly with stringFormat set to 'noCase'", () => { |
| 106 | + const params: EventCreatorParams = { |
| 107 | + ...defaultParams, |
| 108 | + options: { |
| 109 | + ...defaultParams.options, |
| 110 | + labelOptions: { |
| 111 | + stringCase: "lowercase", |
| 112 | + stringFormat: "noCase", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }; |
| 116 | + |
| 117 | + const handlers: EventHandler = eventCreator(params); |
| 118 | + |
| 119 | + const eventMetadata = { elementId: "456" }; |
| 120 | + handlers.link.click(eventMetadata); |
| 121 | + |
| 122 | + expect(mockCallback).toHaveBeenCalledWith({ |
| 123 | + label: "home link click 456", |
| 124 | + metadata: { |
| 125 | + ...eventMetadata, |
| 126 | + page: "Home", |
| 127 | + element: "link", |
| 128 | + action: "click", |
| 129 | + }, |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments