|
| 1 | +/* eslint-disable jest/no-mocks-import */ |
| 2 | +// @ts-check |
| 3 | +import { render, fireEvent, cleanup } from "@testing-library/react"; |
| 4 | +import React from "react"; |
| 5 | +import { trackGAEvent } from "../../../../../../../shared"; |
| 6 | + |
| 7 | +jest.mock("../../../../../../../shared", () => ({ |
| 8 | + idGenerator: jest.fn().mockImplementation(function* () { |
| 9 | + yield "test-id"; |
| 10 | + }), |
| 11 | + trackGAEvent: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +import { NavbarContainer, BUTTON_ERROR_MESSAGE } from "."; |
| 15 | +import { AppContextProvider } from "../../../core/context/app-context"; |
| 16 | + |
| 17 | +const stateWithButtons = { |
| 18 | + navTree: [ |
| 19 | + { |
| 20 | + href: "/", |
| 21 | + text: "Home", |
| 22 | + type: "icon-home", |
| 23 | + class: "home", |
| 24 | + }, |
| 25 | + { |
| 26 | + text: "Link option 1", |
| 27 | + href: "#", |
| 28 | + items: [ |
| 29 | + [ |
| 30 | + { |
| 31 | + href: "https://www.asu.edu", |
| 32 | + text: "Sublink 1", |
| 33 | + }, |
| 34 | + { |
| 35 | + href: "https://www.asu.edu", |
| 36 | + text: "Sublink 2", |
| 37 | + }, |
| 38 | + { |
| 39 | + href: "https://www.asu.edu", |
| 40 | + text: "Sublink 3", |
| 41 | + }, |
| 42 | + { |
| 43 | + href: "https://www.asu.edu", |
| 44 | + text: "Sublink 4", |
| 45 | + }, |
| 46 | + { |
| 47 | + href: "https://www.asu.edu", |
| 48 | + text: "Sublink 5", |
| 49 | + }, |
| 50 | + { |
| 51 | + href: "https://www.asu.edu", |
| 52 | + text: "Sublink 6", |
| 53 | + }, |
| 54 | + { |
| 55 | + href: "https://www.asu.edu", |
| 56 | + text: "Sublink 7", |
| 57 | + }, |
| 58 | + { |
| 59 | + href: "https://www.asu.edu", |
| 60 | + text: "Sublink 8", |
| 61 | + }, |
| 62 | + ], |
| 63 | + ], |
| 64 | + }, |
| 65 | + ], |
| 66 | + buttons: [ |
| 67 | + { text: "Login", href: "/login" }, |
| 68 | + { text: "Sign Up", onClick: jest.fn() }, |
| 69 | + { text: "Contact", onClick: jest.fn(), href: "#" } |
| 70 | + ], |
| 71 | + breakpoint: "lg" |
| 72 | +}; |
| 73 | + |
| 74 | +const renderNavbarContainer = (props) => { |
| 75 | + return render( |
| 76 | + <AppContextProvider initialValue={props}> |
| 77 | + <NavbarContainer /> |
| 78 | + </AppContextProvider> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +describe("#NavbarContainer Button Tests", () => { |
| 83 | + /** @type {import("@testing-library/react").RenderResult} */ |
| 84 | + let component; |
| 85 | + |
| 86 | + beforeEach(() => { |
| 87 | + jest.clearAllMocks(); |
| 88 | + component = renderNavbarContainer(stateWithButtons); |
| 89 | + }); |
| 90 | + |
| 91 | + afterEach(cleanup); |
| 92 | + |
| 93 | + it("should render all buttons from context", () => { |
| 94 | + const buttonsContainer = component.getByTestId("buttons-container"); |
| 95 | + expect(buttonsContainer).toBeInTheDocument(); |
| 96 | + |
| 97 | + const buttons = component.getAllByRole("button"); |
| 98 | + expect(buttons.length).toBe(stateWithButtons.buttons.length); |
| 99 | + |
| 100 | + stateWithButtons.buttons.forEach((buttonData, index) => { |
| 101 | + expect(buttons[index]).toHaveTextContent(buttonData.text); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should call the onClick handler when a button is clicked", () => { |
| 106 | + const buttons = component.getAllByRole("button"); |
| 107 | + |
| 108 | + fireEvent.click(buttons[1]); |
| 109 | + |
| 110 | + expect(stateWithButtons.buttons[1].onClick).toHaveBeenCalledTimes(1); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should track GA event when any button is clicked", () => { |
| 114 | + const buttons = component.getAllByRole("button"); |
| 115 | + |
| 116 | + buttons.forEach((button, index) => { |
| 117 | + fireEvent.click(button); |
| 118 | + |
| 119 | + expect(trackGAEvent).toHaveBeenCalledWith({ |
| 120 | + event: "link", |
| 121 | + action: "click", |
| 122 | + name: "onclick", |
| 123 | + region: "navbar", |
| 124 | + type: "internal link", |
| 125 | + section: "main navbar", |
| 126 | + text: stateWithButtons.buttons[index].text, |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + expect(trackGAEvent).toHaveBeenCalledTimes(buttons.length); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should log an error for buttons with both onClick and href", () => { |
| 134 | + cleanup(); |
| 135 | + |
| 136 | + const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 137 | + |
| 138 | + renderNavbarContainer(stateWithButtons); |
| 139 | + |
| 140 | + expect(consoleErrorSpy).toHaveBeenCalledWith(BUTTON_ERROR_MESSAGE); |
| 141 | + |
| 142 | + consoleErrorSpy.mockRestore(); |
| 143 | + }); |
| 144 | + |
| 145 | + it("should not render buttons section when no buttons exist in context", () => { |
| 146 | + const stateWithoutButtons = { |
| 147 | + ...stateWithButtons, |
| 148 | + buttons: [] |
| 149 | + }; |
| 150 | + |
| 151 | + cleanup(); |
| 152 | + const noButtonsComponent = renderNavbarContainer(stateWithoutButtons); |
| 153 | + |
| 154 | + const buttonsContainer = noButtonsComponent.queryByTestId("buttons-container"); |
| 155 | + expect(buttonsContainer).not.toBeInTheDocument(); |
| 156 | + }); |
| 157 | +}); |
0 commit comments