156 lines
6.0 KiB
TypeScript
156 lines
6.0 KiB
TypeScript
import React from 'react';
|
|
import {render,fireEvent,act,cleanup} from '@testing-library/react';
|
|
import {vi,beforeEach,afterEach,it,expect} from 'vitest';
|
|
import AnalyticsDeferred from '../AnalyticsDeferred';
|
|
|
|
let mockPathname = '/';
|
|
vi.mock('next/navigation', () => ({
|
|
usePathname: () => mockPathname,
|
|
}));
|
|
|
|
beforeEach(()=>{
|
|
vi.useFakeTimers();
|
|
mockPathname = '/';
|
|
document.head.innerHTML='';
|
|
delete (window as any).dataLayer;
|
|
delete (window as any).clarity;
|
|
delete (window as any).gtag;
|
|
});
|
|
afterEach(()=>{
|
|
cleanup();
|
|
vi.useRealTimers();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it('pushes genuine native arguments object for gtag commands so gtag.js processes them',()=>{
|
|
render(<AnalyticsDeferred gaId="G-test"/>);
|
|
fireEvent.scroll(window);
|
|
act(()=>vi.advanceTimersByTime(3500));
|
|
expect(window.dataLayer).toBeDefined();
|
|
const configEntry = window.dataLayer?.find(entry => {
|
|
return Object.prototype.toString.call(entry) === '[object Arguments]' && entry[0] === 'config';
|
|
});
|
|
expect(configEntry).toBeDefined();
|
|
expect(configEntry[1]).toBe('G-test');
|
|
});
|
|
|
|
it('defers tags until interaction and loads only once',()=>{
|
|
render(<AnalyticsDeferred gtmId="GTM-test" gaId="G-test" clarityId="test"/>);
|
|
expect(document.scripts).toHaveLength(0);
|
|
fireEvent.scroll(window);
|
|
fireEvent.keyDown(window);
|
|
act(()=>vi.advanceTimersByTime(3500));
|
|
expect(document.scripts).toHaveLength(3);
|
|
const hasConfig = window.dataLayer?.some(entry => entry[0] === 'config' && entry[1] === 'G-test');
|
|
expect(hasConfig).toBe(true);
|
|
});
|
|
|
|
it('tracks client-side route changes when pathname changes',()=>{
|
|
const {rerender} = render(<AnalyticsDeferred gaId="G-test" gtmId="GTM-test"/>);
|
|
fireEvent.scroll(window);
|
|
act(()=>vi.advanceTimersByTime(3500));
|
|
|
|
// Simulate navigation to blog
|
|
mockPathname = '/blog/test-article';
|
|
rerender(<AnalyticsDeferred gaId="G-test" gtmId="GTM-test"/>);
|
|
|
|
const gtmPageView = window.dataLayer?.find(entry => entry.event === 'page_view');
|
|
expect(gtmPageView).toBeUndefined(); // direct GA is the single first-party owner
|
|
mockPathname = '/';
|
|
rerender(<AnalyticsDeferred gaId="G-test" gtmId="GTM-test"/>);
|
|
const views = window.dataLayer?.filter(entry => entry[0] === 'event' && entry[1] === 'page_view');
|
|
expect(views?.map(entry => entry[2].page_path)).toEqual(['/','/blog/[slug]','/']);
|
|
expect(views?.every(entry => !('page_title' in entry[2]) && !entry[2].page_location.includes('?'))).toBe(true);
|
|
|
|
const gaPageView = window.dataLayer?.find(entry => {
|
|
return Object.prototype.toString.call(entry) === '[object Arguments]' && entry[0] === 'event' && entry[1] === 'page_view';
|
|
});
|
|
expect(gaPageView).toBeDefined();
|
|
expect(gaPageView[2].page_path).toBe('/');
|
|
});
|
|
|
|
it.each(['scroll','touchstart','mousemove','keydown'])('loads on %s',event=>{
|
|
render(<AnalyticsDeferred gtmId="GTM-test"/>);
|
|
fireEvent(window,new Event(event));
|
|
expect(document.scripts).toHaveLength(1);
|
|
});
|
|
|
|
it('loads clarity when no script exists',()=>{
|
|
render(<AnalyticsDeferred clarityId="solo"/>);
|
|
fireEvent.scroll(window);
|
|
expect(document.querySelector('script')?.src).toBe('https://www.clarity.ms/tag/solo');
|
|
});
|
|
|
|
it('preserves the existing clarity implementation',()=>{
|
|
const clarity=vi.fn();
|
|
(window as any).clarity=clarity;
|
|
render(<AnalyticsDeferred clarityId="existing"/>);
|
|
fireEvent.scroll(window);
|
|
expect((window as any).clarity).toBe(clarity);
|
|
expect(document.querySelector('script')?.src).toBe('https://www.clarity.ms/tag/existing');
|
|
});
|
|
|
|
it('queues clarity calls before its script initializes',()=>{
|
|
render(<AnalyticsDeferred clarityId="queued"/>);
|
|
fireEvent.scroll(window);
|
|
(window as any).clarity('event','checkout');
|
|
expect((window as any).clarity.q).toEqual([['event','checkout']]);
|
|
});
|
|
|
|
it('falls back after 3500ms and reuses existing GA script',()=>{
|
|
const s=document.createElement('script');
|
|
s.src='https://www.googletagmanager.com/gtag/js?id=G-test';
|
|
document.head.append(s);
|
|
render(<AnalyticsDeferred gaId="G-test"/>);
|
|
act(()=>vi.advanceTimersByTime(3499));
|
|
expect(window.dataLayer).toBeUndefined();
|
|
act(()=>vi.advanceTimersByTime(1));
|
|
expect(document.scripts).toHaveLength(1);
|
|
});
|
|
|
|
it('cancels idle work on unmount',()=>{
|
|
let cb:()=>void=()=>{};
|
|
vi.stubGlobal('requestIdleCallback',vi.fn(fn=>{cb=fn;return 9;}));
|
|
vi.stubGlobal('cancelIdleCallback',vi.fn());
|
|
const v=render(<AnalyticsDeferred gtmId="GTM-test"/>);
|
|
act(()=>vi.advanceTimersByTime(3500));
|
|
v.unmount();
|
|
act(()=>cb());
|
|
expect(document.scripts).toHaveLength(0);
|
|
expect(cancelIdleCallback).toHaveBeenCalledWith(9);
|
|
});
|
|
|
|
it('honors opt-out before loading and after initialization',()=>{
|
|
window.globalPrivacyControl=true;
|
|
const v=render(<AnalyticsDeferred gaId="G-test"/>);
|
|
fireEvent.scroll(window);
|
|
expect(document.scripts).toHaveLength(0);
|
|
delete window.globalPrivacyControl;
|
|
fireEvent.keyDown(window);
|
|
const count=window.dataLayer?.length;
|
|
window.globalPrivacyControl=true;
|
|
mockPathname='/shop/joint';v.rerender(<AnalyticsDeferred gaId="G-test"/>);
|
|
expect(window.dataLayer).toHaveLength(count!);
|
|
delete window.globalPrivacyControl;
|
|
});
|
|
|
|
it('tracks catalog views and actual enabled contact anchors without commerce events',()=>{
|
|
mockPathname='/shop/joint';
|
|
const v=render(<AnalyticsDeferred gaId="G-test"/>);
|
|
fireEvent.scroll(window);
|
|
const a=document.createElement('a');a.href='tel:private';document.body.append(a);
|
|
fireEvent.click(a);a.remove();
|
|
expect(window.dataLayer?.filter(e=>e[0]==='event').map(e=>e[1])).toEqual(['page_view','catalog_product_view','contact_intent']);
|
|
expect(window.dataLayer?.some(e=>e[0]==='config' && e[2].send_page_view===false)).toBe(true);
|
|
mockPathname='/shop/joint';v.rerender(<AnalyticsDeferred gaId="G-test"/>);
|
|
expect(window.dataLayer?.filter(e=>e[1]==='catalog_product_view')).toHaveLength(1);
|
|
});
|
|
|
|
it('unmount removes event and timeout callbacks',()=>{
|
|
const v=render(<AnalyticsDeferred gtmId="GTM-test"/>);
|
|
v.unmount();
|
|
fireEvent.scroll(window);
|
|
act(()=>vi.advanceTimersByTime(4000));
|
|
expect(document.scripts).toHaveLength(0);
|
|
});
|