-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrack.test.tsx
More file actions
151 lines (120 loc) · 3.75 KB
/
Copy pathTrack.test.tsx
File metadata and controls
151 lines (120 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
import { render, screen } from "@testing-library/react"
import { TrackRoot } from "../src/context"
import { Track } from "../src/Track"
import React, { forwardRef } from "react"
const observers: any[] = []
const setupIntersectionObserverMock = () => {
class IntersectionObserverMock {
observe = vi.fn()
disconnect = vi.fn()
unobserve = vi.fn()
thresholds = [0]
constructor(public callback: IntersectionObserverCallback) {
observers.push(this)
}
}
vi.stubGlobal('IntersectionObserver', IntersectionObserverMock)
return IntersectionObserverMock
}
describe("Track.OnImpression (deeply mocked, not very useful)", () => {
beforeEach(() => {
observers.length = 0
setupIntersectionObserverMock()
})
afterEach(() => {
vi.unstubAllGlobals()
})
const triggerIntersection = (isIntersecting: boolean) => {
const observer = observers[0]
if (observer) {
const callback = observer.callback
callback([{ isIntersecting, intersectionRatio: isIntersecting ? 1 : 0 } as any], observer)
}
}
it("should send event when element becomes visible", () => {
const onEvent = vi.fn()
render(
<TrackRoot onEvent={onEvent}>
<Track.OnImpression event="banner_view">
<div>Test Banner</div>
</Track.OnImpression>
</TrackRoot>
)
expect(onEvent).not.toHaveBeenCalled()
triggerIntersection(true)
expect(onEvent).toHaveBeenCalledTimes(1)
expect(onEvent).toHaveBeenCalledWith("banner_view", {})
})
it("should send event with params", () => {
const onEvent = vi.fn()
const params = { banner_id: "123" }
render(
<TrackRoot onEvent={onEvent}>
<Track.OnImpression event="banner_view" params={params}>
<div>Test Banner</div>
</Track.OnImpression>
</TrackRoot>
)
triggerIntersection(true)
expect(onEvent).toHaveBeenCalledWith("banner_view", params)
})
it("should only send event once by default (freezeOnceVisible: true)", () => {
const onEvent = vi.fn()
render(
<TrackRoot onEvent={onEvent}>
<Track.OnImpression event="banner_view">
<div>Test Banner</div>
</Track.OnImpression>
</TrackRoot>
)
triggerIntersection(true)
triggerIntersection(false)
triggerIntersection(true)
expect(onEvent).toHaveBeenCalledTimes(1)
})
it("should send event multiple times if freezeOnceVisible is false", () => {
const onEvent = vi.fn()
render(
<TrackRoot onEvent={onEvent}>
<Track.OnImpression event="banner_view" options={{ freezeOnceVisible: false }}>
<div>Test Banner</div>
</Track.OnImpression>
</TrackRoot>
)
triggerIntersection(true)
triggerIntersection(false)
triggerIntersection(true)
expect(onEvent).toHaveBeenCalledTimes(2)
})
it("should wrap non-ref children in a div", () => {
const onEvent = vi.fn()
const { container } = render(
<TrackRoot onEvent={onEvent}>
<Track.OnImpression event="banner_view">
<span>Test Banner</span>
</Track.OnImpression>
</TrackRoot>
)
expect(container.innerHTML).toBe('<div><span>Test Banner</span></div>')
})
it("should merge refs for children that already have a ref", () => {
const onEvent = vi.fn()
const innerRef = vi.fn()
const ComponentWithRef = forwardRef<HTMLDivElement, { children: React.ReactNode }>(
({ children }, ref) => <div ref={ref} data-testid="inner">{children}</div>
)
render(
<TrackRoot onEvent={onEvent}>
<Track.OnImpression event="banner_view">
<ComponentWithRef ref={innerRef}>Test Banner</ComponentWithRef>
</Track.OnImpression>
</TrackRoot>
)
// innerRef should be called with the DOM element
expect(innerRef).toHaveBeenCalledWith(screen.getByTestId("inner"))
// And tracking should still work
triggerIntersection(true)
expect(onEvent).toHaveBeenCalledTimes(1)
})
})