|
| 1 | +import React from "react" |
| 2 | +import { render, screen, fireEvent, within } from "@testing-library/react" |
| 3 | +import "@testing-library/jest-dom" |
| 4 | + |
| 5 | +import ValidateEvent, { ValidateEventProps } from "." |
| 6 | +import { EventCtx, EventPayload } from ".." |
| 7 | + |
| 8 | +// Mock the useValidateEvent hook. This allows us to control its output and check if it's called correctly. |
| 9 | +jest.mock("./useValidateEvent", () => ({ |
| 10 | + __esModule: true, |
| 11 | + default: jest.fn(), |
| 12 | +})) |
| 13 | +import useValidateEvent from "./useValidateEvent" |
| 14 | +import { EventType } from "../types" |
| 15 | +const mockedUseValidateEvent = useValidateEvent as jest.Mock |
| 16 | + |
| 17 | +// Mock child components that are not relevant to this test. |
| 18 | +jest.mock("@/components/PrettyJson", () => () => <div>PrettyJson</div>) |
| 19 | +jest.mock("@/components/Spinner", () => () => <div>Spinner</div>) |
| 20 | + |
| 21 | +const mockValidateEventFn = jest.fn() |
| 22 | + |
| 23 | +// A minimal set of props to render the component. |
| 24 | +const defaultProps: ValidateEventProps = { |
| 25 | + measurement_id: "", |
| 26 | + app_instance_id: "", |
| 27 | + firebase_app_id: "", |
| 28 | + api_secret: "", |
| 29 | + client_id: "", |
| 30 | + user_id: "", |
| 31 | + formatPayload: jest.fn(), |
| 32 | + payloadErrors: undefined, |
| 33 | + useTextBox: false, |
| 34 | +} |
| 35 | + |
| 36 | +// A helper to render the component with context. |
| 37 | +const renderComponent = (props: Partial<ValidateEventProps> = {}) => { |
| 38 | + // The component relies on EventCtx for some data. This should be a valid |
| 39 | + // EventPayload. |
| 40 | + const contextValue: EventPayload = { |
| 41 | + instanceId: { |
| 42 | + measurement_id: "G-12345", |
| 43 | + firebase_app_id: "app:12345", |
| 44 | + }, |
| 45 | + eventName: "test_event", |
| 46 | + type: EventType.CustomEvent, |
| 47 | + parameters: [], |
| 48 | + items: [], |
| 49 | + userProperties: [], |
| 50 | + timestamp_micros: "", |
| 51 | + non_personalized_ads: false, |
| 52 | + useTextBox: false, |
| 53 | + payloadObj: [], |
| 54 | + api_secret: "secret123", |
| 55 | + clientIds: {}, |
| 56 | + } |
| 57 | + |
| 58 | + return render( |
| 59 | + <EventCtx.Provider value={contextValue}> |
| 60 | + <ValidateEvent {...defaultProps} {...props} /> |
| 61 | + </EventCtx.Provider> |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +describe("ValidateEvent EU endpoint functionality", () => { |
| 66 | + beforeEach(() => { |
| 67 | + // Reset mocks before each test |
| 68 | + jest.clearAllMocks() |
| 69 | + // Setup the default mock implementation for useValidateEvent to render the initial state. |
| 70 | + mockedUseValidateEvent.mockReturnValue({ |
| 71 | + status: "not-started", |
| 72 | + validateEvent: mockValidateEventFn, |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + it("should render with the default endpoint and allow switching to the EU endpoint", () => { |
| 77 | + renderComponent() |
| 78 | + |
| 79 | + // 1. Check initial state (default endpoint) |
| 80 | + expect(screen.getByText("HOST: www.google-analytics.com", { exact: false })).toBeInTheDocument() |
| 81 | + expect(screen.queryByText("HOST: region1.google-analytics.com", { exact: false })).not.toBeInTheDocument() |
| 82 | + expect(mockedUseValidateEvent).toHaveBeenCalledWith(false) |
| 83 | + |
| 84 | + // 2. Find and interact with the switch |
| 85 | + const euSwitch = within(screen.getByTestId("use-eu-endpoint")).getByRole('checkbox') |
| 86 | + expect(euSwitch).toHaveProperty('checked', false) |
| 87 | + fireEvent.click(euSwitch) |
| 88 | + |
| 89 | + // 3. Check the new state (EU endpoint) |
| 90 | + expect(euSwitch).toHaveProperty('checked', true) |
| 91 | + expect(screen.getByText("HOST: region1.google-analytics.com", { exact: false })).toBeInTheDocument() |
| 92 | + expect(mockedUseValidateEvent).toHaveBeenCalledTimes(2) |
| 93 | + expect(mockedUseValidateEvent).toHaveBeenLastCalledWith(true) |
| 94 | + }) |
| 95 | +}) |
0 commit comments