Skip to content

Commit a7c7d91

Browse files
committed
add tests and improve formatting
1 parent 871502a commit a7c7d91

2 files changed

Lines changed: 119 additions & 23 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
})

src/components/ga4/EventBuilder/ValidateEvent/index.tsx

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import usePayload from "./usePayload"
3030
import { ValidationMessage } from "../types"
3131
import Spinner from "@/components/Spinner"
3232
import { EventCtx, Label } from ".."
33-
import { Card } from "@mui/material"
33+
import { Box, Card } from "@mui/material"
3434
import { green, red } from "@mui/material/colors"
3535
import WithHelpText from "@/components/WithHelpText"
3636

@@ -285,29 +285,30 @@ const ValidateEvent: React.FC<ValidateEventProps> = ({formatPayload, payloadErro
285285

286286
return (
287287
<div className={classes.form}>
288-
<WithHelpText
289-
notched
290-
shrink
291-
label="server endpoint"
292-
className={classes.endpointSwitch}
293-
helpText="Collect data in the European Union. If enabled, the https://region1.google-analytics.com endpoint will be used to validate and send events."
294-
>
295-
<Grid component="label" container alignItems="center" spacing={1}>
296-
<Grid item>Default</Grid>
297-
<Grid item>
298-
<Switch
299-
data-testid="use-eu-endpoint"
300-
checked={useEuEndpoint}
301-
onChange={e => {
302-
setUseEuEndpoint(e.target.checked)
303-
}}
304-
name="use-eu-endpoint"
305-
color="primary"
306-
/>
288+
<Box mb={1}>
289+
<WithHelpText
290+
notched
291+
shrink
292+
label="server endpoint"
293+
helpText="Collect data in the European Union. If enabled, the https://region1.google-analytics.com endpoint will be used to validate and send events."
294+
>
295+
<Grid component="label" container alignItems="center" spacing={1}>
296+
<Grid item>Default</Grid>
297+
<Grid item>
298+
<Switch
299+
data-testid="use-eu-endpoint"
300+
checked={useEuEndpoint}
301+
onChange={e => {
302+
setUseEuEndpoint(e.target.checked)
303+
}}
304+
name="use-eu-endpoint"
305+
color="primary"
306+
/>
307+
</Grid>
308+
<Grid item>EU</Grid>
307309
</Grid>
308-
<Grid item>EU</Grid>
309-
</Grid>
310-
</WithHelpText>
310+
</WithHelpText>
311+
</Box>
311312
<Loadable
312313
request={request}
313314
renderNotStarted={({ validateEvent }) => (

0 commit comments

Comments
 (0)