@@ -2,6 +2,7 @@ import * as coreUtil from '@amplitude/experiment-core';
22
33import {
44 concatenateQueryParamsOf ,
5+ getCookieDomain ,
56 getUrlParams ,
67 matchesUrl ,
78 removeQueryParams ,
@@ -355,6 +356,99 @@ describe('removeQueryParams', () => {
355356 } ) ;
356357} ) ;
357358
359+ describe ( 'getCookieDomain' , ( ) => {
360+ describe ( 'regular domains' , ( ) => {
361+ it ( 'should return root domain with leading dot for standard domain' , ( ) => {
362+ expect ( getCookieDomain ( 'https://example.com' ) ) . toBe ( '.example.com' ) ;
363+ } ) ;
364+
365+ it ( 'should return root domain with leading dot for subdomain' , ( ) => {
366+ expect ( getCookieDomain ( 'https://subdomain.example.com' ) ) . toBe (
367+ '.example.com' ,
368+ ) ;
369+ } ) ;
370+
371+ it ( 'should return root domain with leading dot for multiple subdomains' , ( ) => {
372+ expect ( getCookieDomain ( 'https://sub1.sub2.example.com' ) ) . toBe (
373+ '.example.com' ,
374+ ) ;
375+ } ) ;
376+
377+ it ( 'should handle domain with path and query parameters' , ( ) => {
378+ expect (
379+ getCookieDomain ( 'https://subdomain.example.com/path?param=value' ) ,
380+ ) . toBe ( '.example.com' ) ;
381+ } ) ;
382+
383+ it ( 'should handle domain with port' , ( ) => {
384+ expect ( getCookieDomain ( 'https://subdomain.example.com:3000' ) ) . toBe (
385+ '.example.com' ,
386+ ) ;
387+ } ) ;
388+ } ) ;
389+
390+ describe ( 'localhost' , ( ) => {
391+ it ( 'should return .localhost for localhost' , ( ) => {
392+ expect ( getCookieDomain ( 'http://localhost' ) ) . toBe ( '.localhost' ) ;
393+ expect ( getCookieDomain ( 'http://localhost:3000' ) ) . toBe ( '.localhost' ) ;
394+ } ) ;
395+
396+ it ( 'should return .localhost for subdomains of localhost' , ( ) => {
397+ expect ( getCookieDomain ( 'http://app.localhost' ) ) . toBe ( '.localhost' ) ;
398+ expect ( getCookieDomain ( 'http://app.localhost:3000' ) ) . toBe ( '.localhost' ) ;
399+ expect ( getCookieDomain ( 'http://sub1.sub2.localhost' ) ) . toBe ( '.localhost' ) ;
400+ } ) ;
401+ } ) ;
402+
403+ describe ( 'public suffix domains' , ( ) => {
404+ it ( 'should return full hostname with leading dot for vercel.app subdomain' , ( ) => {
405+ expect ( getCookieDomain ( 'https://myapp.vercel.app' ) ) . toBe (
406+ '.myapp.vercel.app' ,
407+ ) ;
408+ } ) ;
409+
410+ it ( 'should return full hostname with leading dot for netlify.app subdomain' , ( ) => {
411+ expect ( getCookieDomain ( 'https://myapp.netlify.app' ) ) . toBe (
412+ '.myapp.netlify.app' ,
413+ ) ;
414+ } ) ;
415+
416+ it ( 'should return full hostname with leading dot for pages.dev subdomain' , ( ) => {
417+ expect ( getCookieDomain ( 'https://myapp.pages.dev' ) ) . toBe (
418+ '.myapp.pages.dev' ,
419+ ) ;
420+ } ) ;
421+
422+ it ( 'should return full hostname with leading dot for nested public suffix subdomains' , ( ) => {
423+ expect ( getCookieDomain ( 'https://feature-branch.myapp.vercel.app' ) ) . toBe (
424+ '.feature-branch.myapp.vercel.app' ,
425+ ) ;
426+ expect ( getCookieDomain ( 'https://preview.myapp.netlify.app' ) ) . toBe (
427+ '.preview.myapp.netlify.app' ,
428+ ) ;
429+ expect ( getCookieDomain ( 'https://staging.myapp.pages.dev' ) ) . toBe (
430+ '.staging.myapp.pages.dev' ,
431+ ) ;
432+ } ) ;
433+ } ) ;
434+
435+ describe ( 'edge cases' , ( ) => {
436+ it ( 'should return undefined for invalid URL' , ( ) => {
437+ expect ( getCookieDomain ( 'not-a-valid-url' ) ) . toBeUndefined ( ) ;
438+ expect ( getCookieDomain ( '' ) ) . toBeUndefined ( ) ;
439+ } ) ;
440+
441+ it ( 'should handle two-part domain' , ( ) => {
442+ expect ( getCookieDomain ( 'https://example.co' ) ) . toBe ( '.example.co' ) ;
443+ } ) ;
444+
445+ it ( 'should handle three-part top-level domain' , ( ) => {
446+ expect ( getCookieDomain ( 'https://example.co.uk' ) ) . toBe ( '.co.uk' ) ;
447+ expect ( getCookieDomain ( 'https://subdomain.example.co.uk' ) ) . toBe ( '.co.uk' ) ;
448+ } ) ;
449+ } ) ;
450+ } ) ;
451+
358452afterAll ( ( ) => {
359453 // Restore the original getGlobalScope function after all tests
360454 spyGetGlobalScope . mockRestore ( ) ;
0 commit comments