|
| 1 | +import { parseAttribution, attributionToProperties } from './attribution'; |
| 2 | + |
| 3 | +beforeEach(() => { |
| 4 | + sessionStorage.clear(); |
| 5 | +}); |
| 6 | + |
| 7 | +describe('parseAttribution', () => { |
| 8 | + it('parses UTM params from the URL', () => { |
| 9 | + Object.defineProperty(window, 'location', { |
| 10 | + value: { |
| 11 | + search: '?utm_source=youtube&utm_medium=influencer&utm_campaign=launch', |
| 12 | + href: 'https://studio.com/shop?utm_source=youtube&utm_medium=influencer&utm_campaign=launch', |
| 13 | + }, |
| 14 | + writable: true, |
| 15 | + }); |
| 16 | + |
| 17 | + const ctx = parseAttribution(); |
| 18 | + expect(ctx.utmSource).toBe('youtube'); |
| 19 | + expect(ctx.utmMedium).toBe('influencer'); |
| 20 | + expect(ctx.utmCampaign).toBe('launch'); |
| 21 | + expect(ctx.landingPage).toBe(window.location.href); |
| 22 | + }); |
| 23 | + |
| 24 | + it('parses click IDs from the URL', () => { |
| 25 | + Object.defineProperty(window, 'location', { |
| 26 | + value: { |
| 27 | + search: '?gclid=abc&fbclid=def&ttclid=ghi&msclkid=jkl&dclid=mno&li_fat_id=pqr', |
| 28 | + href: 'https://studio.com/?gclid=abc&fbclid=def&ttclid=ghi&msclkid=jkl&dclid=mno&li_fat_id=pqr', |
| 29 | + }, |
| 30 | + writable: true, |
| 31 | + }); |
| 32 | + |
| 33 | + const ctx = parseAttribution(); |
| 34 | + expect(ctx.gclid).toBe('abc'); |
| 35 | + expect(ctx.fbclid).toBe('def'); |
| 36 | + expect(ctx.ttclid).toBe('ghi'); |
| 37 | + expect(ctx.msclkid).toBe('jkl'); |
| 38 | + expect(ctx.dclid).toBe('mno'); |
| 39 | + expect(ctx.li_fat_id).toBe('pqr'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('parses ref param as referralCode', () => { |
| 43 | + Object.defineProperty(window, 'location', { |
| 44 | + value: { |
| 45 | + search: '?ref=creator_handle', |
| 46 | + href: 'https://studio.com/?ref=creator_handle', |
| 47 | + }, |
| 48 | + writable: true, |
| 49 | + }); |
| 50 | + |
| 51 | + const ctx = parseAttribution(); |
| 52 | + expect(ctx.referralCode).toBe('creator_handle'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns cached attribution on subsequent calls within session', () => { |
| 56 | + Object.defineProperty(window, 'location', { |
| 57 | + value: { |
| 58 | + search: '?utm_source=first', |
| 59 | + href: 'https://studio.com/?utm_source=first', |
| 60 | + }, |
| 61 | + writable: true, |
| 62 | + }); |
| 63 | + |
| 64 | + const first = parseAttribution(); |
| 65 | + expect(first.utmSource).toBe('first'); |
| 66 | + |
| 67 | + // Change URL (simulating SPA navigation) |
| 68 | + Object.defineProperty(window, 'location', { |
| 69 | + value: { |
| 70 | + search: '', |
| 71 | + href: 'https://studio.com/shop', |
| 72 | + }, |
| 73 | + writable: true, |
| 74 | + }); |
| 75 | + |
| 76 | + const second = parseAttribution(); |
| 77 | + expect(second.utmSource).toBe('first'); // Still the original |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns empty context with no params', () => { |
| 81 | + Object.defineProperty(window, 'location', { |
| 82 | + value: { |
| 83 | + search: '', |
| 84 | + href: 'https://studio.com/', |
| 85 | + }, |
| 86 | + writable: true, |
| 87 | + }); |
| 88 | + |
| 89 | + const ctx = parseAttribution(); |
| 90 | + expect(ctx.utmSource).toBeUndefined(); |
| 91 | + expect(ctx.gclid).toBeUndefined(); |
| 92 | + expect(ctx.landingPage).toBe('https://studio.com/'); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('attributionToProperties', () => { |
| 97 | + it('converts attribution context to flat properties', () => { |
| 98 | + const props = attributionToProperties({ |
| 99 | + utmSource: 'youtube', |
| 100 | + utmMedium: 'influencer', |
| 101 | + gclid: 'abc', |
| 102 | + dclid: 'mno', |
| 103 | + li_fat_id: 'pqr', |
| 104 | + referralCode: 'ref123', |
| 105 | + landingPage: 'https://studio.com/', |
| 106 | + }); |
| 107 | + |
| 108 | + expect(props).toEqual({ |
| 109 | + utm_source: 'youtube', |
| 110 | + utm_medium: 'influencer', |
| 111 | + gclid: 'abc', |
| 112 | + dclid: 'mno', |
| 113 | + li_fat_id: 'pqr', |
| 114 | + referral_code: 'ref123', |
| 115 | + landing_page: 'https://studio.com/', |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('omits undefined fields', () => { |
| 120 | + const props = attributionToProperties({ utmSource: 'youtube' }); |
| 121 | + expect(props).toEqual({ utm_source: 'youtube' }); |
| 122 | + expect(props).not.toHaveProperty('utm_medium'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments