Files
bibdle/tests/stats-analytics.test.ts

31 lines
1.1 KiB
TypeScript

import { describe, test, expect } from 'bun:test';
import { parseAcquisition } from '$lib/utils/acquisition';
describe('parseAcquisition', () => {
test('extracts utm parameters and ref', () => {
const a = parseAcquisition('?utm_source=bluesky&utm_medium=social&utm_campaign=launch', '');
expect(a.utm_source).toBe('bluesky');
expect(a.utm_medium).toBe('social');
expect(a.utm_campaign).toBe('launch');
});
test('falls back to ref for the source', () => {
expect(parseAcquisition('?ref=hn', '').utm_source).toBe('hn');
});
test('keeps an external referrer', () => {
const a = parseAcquisition('', 'https://news.ycombinator.com/item?id=1');
expect(a.referrer).toBe('https://news.ycombinator.com/item?id=1');
});
test('blank and whitespace values become null', () => {
const a = parseAcquisition('?utm_source=%20%20', '');
expect(a.utm_source).toBeNull();
expect(a.referrer).toBeNull();
});
test('caps absurdly long values', () => {
const a = parseAcquisition(`?utm_source=${'x'.repeat(500)}`, '');
expect(a.utm_source!.length).toBe(256);
});
});