Files
canina/frontend/application/lib/__tests__/catalogAnalytics.test.ts
T

22 lines
1.9 KiB
TypeScript

import {describe,it,expect,vi} from 'vitest';
import {catalogRoute, analyticsAllowed, trackCatalogClick} from '../catalogAnalytics';
describe('catalog analytics privacy and intent',()=>{
it('uses bounded public route templates without identifiers or private URLs',()=>{
expect(catalogRoute('/shop/joint')).toBe('/shop/[slug]');
expect(catalogRoute('/blog/article')).toBe('/blog/[slug]');
expect(catalogRoute('/dashboard/orders/secret')).toBe('/other');
});
it('respects browser privacy signals',()=>{expect(analyticsAllowed({doNotTrack:'1'},{})).toBe(false);expect(analyticsAllowed({}, {globalPrivacyControl:true})).toBe(false);});
it('records article to product intent without href, title, query or contact data',()=>{
const emit=vi.fn(); const a=document.createElement('a');a.href='/shop/joint?email=private#phone';a.textContent='private';
trackCatalogClick(new MouseEvent('click'),a,'/blog/article',emit);
expect(emit).toHaveBeenCalledExactlyOnceWith('internal_navigation',{source_route:'/blog/[slug]',target_route:'/shop/[slug]'});
});
it('records actual contact anchors but never contact value or external navigation',()=>{
const emit=vi.fn();const a=document.createElement('a');a.href='tel:private';trackCatalogClick(new MouseEvent('click'),a,'/shop/joint',emit);
expect(emit).toHaveBeenCalledExactlyOnceWith('contact_intent',{source_route:'/shop/[slug]',contact_type:'phone'});
emit.mockClear();a.href='https://external.test';trackCatalogClick(new MouseEvent('click'),a,'/blog/a',emit);expect(emit).not.toHaveBeenCalled();
});
it('ignores cancelled, modified, and non-primary clicks',()=>{const emit=vi.fn();const a=document.createElement('a');a.href='/shop/a';for(const event of [new MouseEvent('click',{ctrlKey:true}),new MouseEvent('click',{button:1})]) trackCatalogClick(event,a,'/blog/a',emit);const event=new MouseEvent('click',{cancelable:true});event.preventDefault();trackCatalogClick(event,a,'/blog/a',emit);expect(emit).not.toHaveBeenCalled();});
});