Files
dify/web/app/components/base/amplitude/utils.spec.ts
+42 f50e44b24a test: improve coverage for some test files (#32916)
Signed-off-by: edvatar <[email protected]>
Signed-off-by: -LAN- <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: majiayu000 <[email protected]>
Co-authored-by: Poojan <[email protected]>
Co-authored-by: sahil-infocusp <[email protected]>
Co-authored-by: 非法操作 <[email protected]>
Co-authored-by: Pandaaaa906 <[email protected]>
Co-authored-by: Asuka Minato <[email protected]>
Co-authored-by: heyszt <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Ijas <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: 木之本澪 <[email protected]>
Co-authored-by: KinomotoMio <[email protected]>
Co-authored-by: 不做了睡大觉 <[email protected]>
Co-authored-by: User <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: edvatar <[email protected]>
Co-authored-by: -LAN- <[email protected]>
Co-authored-by: Leilei <[email protected]>
Co-authored-by: HaKu <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: wangxiaolei <[email protected]>
Co-authored-by: Varun Chawla <[email protected]>
Co-authored-by: Stephen Zhou <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: tda <[email protected]>
Co-authored-by: root <root@DESKTOP-KQLO90N>
Co-authored-by: Sisyphus <[email protected]>
Co-authored-by: Niels Kaspers <[email protected]>
Co-authored-by: hj24 <[email protected]>
Co-authored-by: Tyson Cung <[email protected]>
Co-authored-by: Stephen Zhou <[email protected]>
Co-authored-by: FFXN <[email protected]>
Co-authored-by: slegarraga <[email protected]>
Co-authored-by: 99 <[email protected]>
Co-authored-by: Br1an <[email protected]>
Co-authored-by: L1nSn0w <[email protected]>
Co-authored-by: Yunlu Wen <[email protected]>
Co-authored-by: akkoaya <[email protected]>
Co-authored-by: 盐粒 Yanli <[email protected]>
Co-authored-by: lif <[email protected]>
Co-authored-by: weiguang li <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: crazywoola <[email protected]>
Co-authored-by: HanWenbo <[email protected]>
Co-authored-by: Coding On Star <[email protected]>
Co-authored-by: CodingOnStar <[email protected]>
Co-authored-by: Stable Genius <[email protected]>
Co-authored-by: Stable Genius <[email protected]>
Co-authored-by: ふるい <[email protected]>
Co-authored-by: Xiyuan Chen <[email protected]>
2026-03-06 18:59:16 +08:00

120 lines
3.2 KiB
TypeScript

import { resetUser, setUserId, setUserProperties, trackEvent } from './utils'
const mockState = vi.hoisted(() => ({
enabled: true,
}))
const mockTrack = vi.hoisted(() => vi.fn())
const mockSetUserId = vi.hoisted(() => vi.fn())
const mockIdentify = vi.hoisted(() => vi.fn())
const mockReset = vi.hoisted(() => vi.fn())
const MockIdentify = vi.hoisted(() =>
class {
setCalls: Array<[string, unknown]> = []
set(key: string, value: unknown) {
this.setCalls.push([key, value])
return this
}
},
)
vi.mock('./AmplitudeProvider', () => ({
isAmplitudeEnabled: () => mockState.enabled,
}))
vi.mock('@amplitude/analytics-browser', () => ({
track: (...args: unknown[]) => mockTrack(...args),
setUserId: (...args: unknown[]) => mockSetUserId(...args),
identify: (...args: unknown[]) => mockIdentify(...args),
reset: (...args: unknown[]) => mockReset(...args),
Identify: MockIdentify,
}))
describe('amplitude utils', () => {
beforeEach(() => {
vi.clearAllMocks()
mockState.enabled = true
})
describe('trackEvent', () => {
it('should call amplitude.track when amplitude is enabled', () => {
trackEvent('dataset_created', { source: 'wizard' })
expect(mockTrack).toHaveBeenCalledTimes(1)
expect(mockTrack).toHaveBeenCalledWith('dataset_created', { source: 'wizard' })
})
it('should not call amplitude.track when amplitude is disabled', () => {
mockState.enabled = false
trackEvent('dataset_created', { source: 'wizard' })
expect(mockTrack).not.toHaveBeenCalled()
})
})
describe('setUserId', () => {
it('should call amplitude.setUserId when amplitude is enabled', () => {
setUserId('user-123')
expect(mockSetUserId).toHaveBeenCalledTimes(1)
expect(mockSetUserId).toHaveBeenCalledWith('user-123')
})
it('should not call amplitude.setUserId when amplitude is disabled', () => {
mockState.enabled = false
setUserId('user-123')
expect(mockSetUserId).not.toHaveBeenCalled()
})
})
describe('setUserProperties', () => {
it('should build identify event and call amplitude.identify when amplitude is enabled', () => {
const properties: Record<string, unknown> = {
role: 'owner',
seats: 3,
verified: true,
}
setUserProperties(properties)
expect(mockIdentify).toHaveBeenCalledTimes(1)
const identifyArg = mockIdentify.mock.calls[0][0] as InstanceType<typeof MockIdentify>
expect(identifyArg).toBeInstanceOf(MockIdentify)
expect(identifyArg.setCalls).toEqual([
['role', 'owner'],
['seats', 3],
['verified', true],
])
})
it('should not call amplitude.identify when amplitude is disabled', () => {
mockState.enabled = false
setUserProperties({ role: 'owner' })
expect(mockIdentify).not.toHaveBeenCalled()
})
})
describe('resetUser', () => {
it('should call amplitude.reset when amplitude is enabled', () => {
resetUser()
expect(mockReset).toHaveBeenCalledTimes(1)
})
it('should not call amplitude.reset when amplitude is disabled', () => {
mockState.enabled = false
resetUser()
expect(mockReset).not.toHaveBeenCalled()
})
})
})