dify/web/app/components/base/analytics-consent/__tests__/cloud-analytics-runtime.spec.tsx
Coding On Star 8d293023cc
feat: gate cloud analytics behind cookie consent (#39408)
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
2026-07-23 02:40:54 +00:00

46 lines
1.5 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import { CloudAnalyticsRuntime } from '../cloud-analytics-runtime'
const mockState = vi.hoisted(() => ({ pathname: '/signin' }))
vi.mock('@/next/navigation', () => ({
usePathname: () => mockState.pathname,
}))
vi.mock('../cookieyes-consent-bridge', () => ({
CookieYesConsentBridge: () => <span data-testid="cookieyes-consent-bridge" />,
}))
vi.mock('@/app/components/base/amplitude', () => ({
default: ({ active }: { active: boolean }) => (
<span data-active={String(active)} data-testid="amplitude-provider" />
),
}))
describe('CloudAnalyticsRuntime', () => {
beforeEach(() => {
mockState.pathname = '/signin'
})
it('keeps consent active across first-party Cloud routes', () => {
const { rerender } = render(<CloudAnalyticsRuntime />)
expect(screen.getByTestId('cookieyes-consent-bridge')).toBeInTheDocument()
expect(screen.getByTestId('amplitude-provider')).toHaveAttribute('data-active', 'true')
mockState.pathname = '/integrations'
rerender(<CloudAnalyticsRuntime />)
expect(screen.getByTestId('amplitude-provider')).toHaveAttribute('data-active', 'true')
})
it('opts Amplitude out after a client transition into a share route', () => {
const { rerender } = render(<CloudAnalyticsRuntime />)
mockState.pathname = '/workflow/token'
rerender(<CloudAnalyticsRuntime />)
expect(screen.getByTestId('amplitude-provider')).toHaveAttribute('data-active', 'false')
})
})