fix: url query change record cookie

This commit is contained in:
Joel
2026-04-02 11:22:46 +08:00
parent 2d29345f26
commit 20ddc9c48a
3 changed files with 22 additions and 4 deletions

View File

@@ -2,6 +2,8 @@ import { render } from '@testing-library/react'
import PartnerStackCookieRecorder from '../cookie-recorder' import PartnerStackCookieRecorder from '../cookie-recorder'
let isCloudEdition = true let isCloudEdition = true
let psPartnerKey: string | undefined
let psClickId: string | undefined
const saveOrUpdate = vi.fn() const saveOrUpdate = vi.fn()
@@ -13,6 +15,8 @@ vi.mock('@/config', () => ({
vi.mock('../use-ps-info', () => ({ vi.mock('../use-ps-info', () => ({
default: () => ({ default: () => ({
psPartnerKey,
psClickId,
saveOrUpdate, saveOrUpdate,
}), }),
})) }))
@@ -21,6 +25,8 @@ describe('PartnerStackCookieRecorder', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks() vi.clearAllMocks()
isCloudEdition = true isCloudEdition = true
psPartnerKey = undefined
psClickId = undefined
}) })
it('should call saveOrUpdate once on mount when running in cloud edition', () => { it('should call saveOrUpdate once on mount when running in cloud edition', () => {
@@ -42,4 +48,16 @@ describe('PartnerStackCookieRecorder', () => {
expect(container.innerHTML).toBe('') expect(container.innerHTML).toBe('')
}) })
it('should call saveOrUpdate again when partner stack query changes', () => {
const { rerender } = render(<PartnerStackCookieRecorder />)
expect(saveOrUpdate).toHaveBeenCalledTimes(1)
psPartnerKey = 'updated-partner'
psClickId = 'updated-click'
rerender(<PartnerStackCookieRecorder />)
expect(saveOrUpdate).toHaveBeenCalledTimes(2)
})
}) })

View File

@@ -5,13 +5,13 @@ import { IS_CLOUD_EDITION } from '@/config'
import usePSInfo from './use-ps-info' import usePSInfo from './use-ps-info'
const PartnerStackCookieRecorder = () => { const PartnerStackCookieRecorder = () => {
const { saveOrUpdate } = usePSInfo() const { psPartnerKey, psClickId, saveOrUpdate } = usePSInfo()
useEffect(() => { useEffect(() => {
if (!IS_CLOUD_EDITION) if (!IS_CLOUD_EDITION)
return return
saveOrUpdate() saveOrUpdate()
}, []) }, [psPartnerKey, psClickId, saveOrUpdate])
return null return null
} }

View File

@@ -6,7 +6,7 @@ import { IS_CLOUD_EDITION } from '@/config'
import usePSInfo from './use-ps-info' import usePSInfo from './use-ps-info'
const PartnerStack: FC = () => { const PartnerStack: FC = () => {
const { saveOrUpdate, bind } = usePSInfo() const { psPartnerKey, psClickId, saveOrUpdate, bind } = usePSInfo()
useEffect(() => { useEffect(() => {
if (!IS_CLOUD_EDITION) if (!IS_CLOUD_EDITION)
return return
@@ -14,7 +14,7 @@ const PartnerStack: FC = () => {
saveOrUpdate() saveOrUpdate()
// bind PartnerStack info after user logged in // bind PartnerStack info after user logged in
bind() bind()
}, []) }, [psPartnerKey, psClickId, saveOrUpdate, bind])
return null return null
} }