Skip to content

Commit b0419ed

Browse files
fix(reddit-pixel): forward calls after pixel.js loads with the command as the first argument (#936)
Co-authored-by: Harlan GitHub Agent <harlan-github-agent[bot]@users.noreply.github.com>
1 parent b4f097c commit b0419ed

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎packages/script/src/registry-types.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@
11301130
{
11311131
"name": "RedditPixelApi",
11321132
"kind": "interface",
1133-
"code": "export interface RedditPixelApi {\n rdt: RdtFns & {\n sendEvent: (rdt: RedditPixelApi['rdt'], args: unknown[]) => void\n callQueue: unknown[]\n }\n}"
1133+
"code": "export interface RedditPixelApi {\n rdt: RdtFns & {\n sendEvent: (command: string, ...args: unknown[]) => void\n callQueue: unknown[]\n }\n}"
11341134
},
11351135
{
11361136
"name": "RedditPixelOptions",

‎packages/script/src/runtime/registry/reddit-pixel.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type RdtFns
1111

1212
export interface RedditPixelApi {
1313
rdt: RdtFns & {
14-
sendEvent: (rdt: RedditPixelApi['rdt'], args: unknown[]) => void
14+
sendEvent: (command: string, ...args: unknown[]) => void
1515
callQueue: unknown[]
1616
}
1717
}
@@ -39,7 +39,7 @@ export function useScriptRedditPixel<T extends RedditPixelApi>(_options?: Reddit
3939
: () => {
4040
const rdt = function (...args: unknown[]) {
4141
if ((rdt as any).sendEvent) {
42-
(rdt as any).sendEvent(rdt, args)
42+
(rdt as any).sendEvent(...args)
4343
}
4444
else {
4545
(rdt as any).callQueue.push(args)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @vitest-environment happy-dom
3+
*/
4+
import { describe, expect, it, vi } from 'vitest'
5+
import { useScriptRedditPixel } from '../../packages/script/src/runtime/registry/reddit-pixel'
6+
7+
const mocks = vi.hoisted(() => ({
8+
definition: undefined as any,
9+
}))
10+
11+
vi.mock('../../packages/script/src/runtime/utils', () => ({
12+
useRegistryScript: (_key: string, factory: (o: any) => any, options: any) => {
13+
mocks.definition = factory(options)
14+
return {}
15+
},
16+
}))
17+
18+
describe('reddit pixel stub', () => {
19+
it('forwards calls to sendEvent with the command as the first argument once pixel.js has loaded', () => {
20+
useScriptRedditPixel({ id: 'a2_test' })
21+
mocks.definition.clientInit()
22+
const sendEvent = vi.fn()
23+
window.rdt.sendEvent = sendEvent
24+
window.rdt('track', 'SignUp')
25+
expect(sendEvent).toHaveBeenCalledWith('track', 'SignUp')
26+
})
27+
})

0 commit comments

Comments
 (0)