This commit is contained in:
Francis Cao committed 2026-07-24 15:47:05 -07:00
commit 44eb78f553
19 files changed
+3355

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
import { renderHook } from '@testing-library/react';
import { describe, expect, test, vi } from 'vitest';
// labels resolves any key to its own name; t is identity.
const labels = new Proxy({} as Record<string, string>, {
get: (_target, prop: string) => prop,
});
vi.mock('./useMessages', () => ({
useMessages: () => ({
t: (value: string) => value,
labels,
}),
}));
import { useFields } from './useFields';
function setup() {
return renderHook(() => useFields()).result.current;
}
describe('useFields', () => {
test('exposes a field for every filterable column', () => {
const { fields } = setup();
const names = fields.map(f => f.name);
expect(names).toEqual([
'path',
'query',
'title',
'referrer',
'country',
'region',
'city',
'browser',
'os',
'device',
'utmSource',
'utmMedium',
'utmCampaign',
'utmContent',
'utmTerm',
'hostname',
'distinctId',
'tag',
'event',
]);
});
test('assigns each field to the expected group', () => {
const { fields } = setup();
const byName = Object.fromEntries(fields.map(f => [f.name, f.group]));
expect(byName.path).toBe('url');
expect(byName.referrer).toBe('sources');
expect(byName.city).toBe('location');
expect(byName.browser).toBe('environment');
expect(byName.utmSource).toBe('utm');
expect(byName.tag).toBe('other');
});
test('uses distinct filterLabel and label where the source distinguishes them', () => {
const { fields } = setup();
const utmSource = fields.find(f => f.name === 'utmSource');
// filterLabel uses the generic "source" label, label uses "utmSource".
expect(utmSource?.filterLabel).toBe('source');
expect(utmSource?.label).toBe('utmSource');
});
test('returns group labels in display order', () => {
const { groupLabels } = setup();
expect(groupLabels.map(g => g.key)).toEqual([
'url',
'sources',
'location',
'environment',
'utm',
'other',
]);
});
});
@@ -0,0 +1,123 @@
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, test, vi } from 'vitest';
const nav: { pathname: string; query: Record<string, any> } = {
pathname: '/websites/1',
query: {},
};
let shareValue: any = null;
vi.mock('./useNavigation', () => ({
useNavigation: () => nav,
}));
vi.mock('./context/useShare', () => ({
useShare: () => shareValue,
}));
import { useFilterParameters } from './useFilterParameters';
function setup(props?: { includePagination?: boolean }) {
return renderHook(() => useFilterParameters(props)).result.current;
}
describe('useFilterParameters', () => {
beforeEach(() => {
nav.pathname = '/websites/1';
nav.query = {};
shareValue = null;
});
test('includes filter-column params and session property filters', () => {
nav.query = { path: 'x', spf1: 'y', notAField: 'z' };
const params = setup();
expect(params.path).toBe('x');
expect(params.spf1).toBe('y');
expect(params).not.toHaveProperty('notAField');
});
test('includes event property filters only on the events path', () => {
nav.pathname = '/websites/1/events';
nav.query = { epf1: 'e' };
const params = setup();
expect(params.epf1).toBe('e');
});
test('excludes event property filters off the events path', () => {
nav.pathname = '/websites/1';
nav.query = { epf1: 'e' };
const params = setup();
expect(params).not.toHaveProperty('epf1');
});
test('strips trailing index when matching filter columns', () => {
nav.query = { path12: 'x' };
const params = setup();
expect(params.path12).toBe('x');
});
test('passes through search, segment, cohort, excludeBounce and match when allowed', () => {
nav.query = {
search: 's',
segment: 'seg',
cohort: 'co',
excludeBounce: '1',
match: 'all',
};
const params = setup();
expect(params).toMatchObject({
search: 's',
segment: 'seg',
cohort: 'co',
excludeBounce: '1',
match: 'all',
});
});
test('includes pagination by default', () => {
nav.query = { page: '2', pageSize: '25' };
const params = setup();
expect(params.page).toBe('2');
expect(params.pageSize).toBe('25');
});
test('omits pagination when includePagination is false', () => {
nav.query = { page: '2', pageSize: '25' };
const params = setup({ includePagination: false });
expect(params).not.toHaveProperty('page');
expect(params).not.toHaveProperty('pageSize');
});
test('drops filter params and share-gated params when filtering is disallowed', () => {
shareValue = { parameters: { allowFilter: false } };
nav.query = {
path: 'x',
spf1: 'y',
search: 's',
segment: 'seg',
cohort: 'co',
excludeBounce: '1',
match: 'all',
};
const params = setup();
// filter columns and property filters are excluded entirely
expect(params).not.toHaveProperty('path');
expect(params).not.toHaveProperty('spf1');
// share-gated params become undefined
expect(params.segment).toBeUndefined();
expect(params.cohort).toBeUndefined();
expect(params.excludeBounce).toBeUndefined();
expect(params.match).toBeUndefined();
// search is always passed through
expect(params.search).toBe('s');
});
});
+171
View File
@@ -0,0 +1,171 @@
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, test, vi } from 'vitest';
const labels = new Proxy({} as Record<string, string>, {
get: (_target, prop: string) => prop,
});
const nav: { pathname: string; query: Record<string, any> } = {
pathname: '/websites/1',
query: {},
};
let shareValue: any = null;
const fields = [
{ name: 'path', label: 'Path' },
{ name: 'country', label: 'Country' },
];
vi.mock('./useMessages', () => ({
useMessages: () => ({ t: (value: string) => value, labels }),
}));
vi.mock('./useNavigation', () => ({
useNavigation: () => nav,
}));
vi.mock('./useFields', () => ({
useFields: () => ({ fields }),
}));
vi.mock('./useOperatorLabels', () => ({
useOperatorLabels: () => new Proxy({} as Record<string, string>, { get: (_t, p: string) => p }),
}));
vi.mock('./context/useShare', () => ({
useShare: () => shareValue,
}));
vi.mock('@/lib/params', () => ({
parseSessionPropertyFilters: vi.fn(() => ['session-filter']),
parseUniversalEventPropertyFilters: vi.fn(() => ['event-filter']),
}));
import { parseSessionPropertyFilters, parseUniversalEventPropertyFilters } from '@/lib/params';
import { useFilters } from './useFilters';
function setup() {
return renderHook(() => useFilters()).result.current;
}
describe('useFilters', () => {
beforeEach(() => {
nav.pathname = '/websites/1';
nav.query = {};
shareValue = null;
vi.clearAllMocks();
});
describe('filters', () => {
test('builds a filter for a filterable column with a default operator', () => {
nav.query = { country: 'US' };
const { filters } = setup();
expect(filters).toEqual([
{ name: 'country', type: 'country', operator: 'eq', value: 'US', label: 'Country' },
]);
});
test('parses an embedded operator prefix', () => {
nav.query = { path: 'c.blog' };
const { filters } = setup();
expect(filters).toEqual([
{ name: 'path', type: 'path', operator: 'c', value: 'blog', label: 'Path' },
]);
});
test('strips a trailing index to resolve the base column name', () => {
nav.query = { path1: 'eq.home' };
const { filters } = setup();
expect(filters).toEqual([
{ name: 'path1', type: 'path', operator: 'eq', value: 'home', label: 'Path' },
]);
});
test('ignores query keys that are not filter columns', () => {
nav.query = { page: '2', search: 'foo', path: 'x' };
const { filters } = setup();
expect(filters.map(f => f.name)).toEqual(['path']);
});
test('decodes URI-encoded values', () => {
nav.query = { path: 'c.a%20b' };
const { filters } = setup();
expect(filters[0].value).toBe('a b');
});
test('returns no filters when sharing disallows filtering', () => {
shareValue = { parameters: { allowFilter: false } };
nav.query = { path: 'c.blog' };
const { filters } = setup();
expect(filters).toEqual([]);
});
});
describe('property filters', () => {
test('includes event property filters only on the events path', () => {
nav.pathname = '/websites/1/events';
const { eventPropertyFilters } = setup();
expect(eventPropertyFilters).toEqual(['event-filter']);
expect(parseUniversalEventPropertyFilters).toHaveBeenCalled();
});
test('excludes event property filters off the events path', () => {
nav.pathname = '/websites/1';
const { eventPropertyFilters } = setup();
expect(eventPropertyFilters).toEqual([]);
expect(parseUniversalEventPropertyFilters).not.toHaveBeenCalled();
});
test('always includes session property filters when allowed', () => {
const { sessionPropertyFilters } = setup();
expect(sessionPropertyFilters).toEqual(['session-filter']);
expect(parseSessionPropertyFilters).toHaveBeenCalled();
});
test('excludes property filters when sharing disallows filtering', () => {
shareValue = { parameters: { allowFilter: false } };
nav.pathname = '/websites/1/events';
const { eventPropertyFilters, sessionPropertyFilters } = setup();
expect(eventPropertyFilters).toEqual([]);
expect(sessionPropertyFilters).toEqual([]);
});
});
describe('getFilters', () => {
test('maps operator keys to labelled options for a known type', () => {
const { getFilters } = setup();
expect(getFilters('string')).toEqual([
{ type: 'string', value: 'eq', label: 'eq' },
{ type: 'string', value: 'neq', label: 'neq' },
{ type: 'string', value: 'c', label: 'c' },
{ type: 'string', value: 'dnc', label: 'dnc' },
{ type: 'string', value: 're', label: 're' },
{ type: 'string', value: 'nre', label: 'nre' },
]);
});
test('returns an empty array for an unknown type', () => {
const { getFilters } = setup();
expect(getFilters('nope')).toEqual([]);
});
});
test('exposes the static operators list', () => {
const { operators } = setup();
expect(operators).toContainEqual({ name: 'gt', type: 'number', label: 'greaterThan' });
expect(operators.filter(o => o.type === 'boolean')).toHaveLength(2);
});
});
+170
View File
@@ -0,0 +1,170 @@
import { renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, test, vi } from 'vitest';
const countryNames: Record<string, string> = {
US: 'United States',
GB: 'United Kingdom',
};
const languageNames: Record<string, string> = {
en: 'English',
};
const labels: Record<string, string> = {
unknown: 'Unknown',
mobile: 'Mobile',
desktop: 'Desktop',
};
vi.mock('./useMessages', () => ({
useMessages: () => ({
t: (value: string) => value,
labels,
}),
}));
vi.mock('./useLocale', () => ({
useLocale: () => ({ locale: 'en-US' }),
}));
vi.mock('./useCountryNames', () => ({
useCountryNames: () => ({ countryNames }),
}));
vi.mock('./useLanguageNames', () => ({
useLanguageNames: () => ({ languageNames }),
}));
import { useFormat } from './useFormat';
function setup() {
return renderHook(() => useFormat()).result.current;
}
describe('useFormat', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('formatOS', () => {
test('maps known OS names', () => {
const { formatOS } = setup();
expect(formatOS('Mac OS')).toBe('macOS');
expect(formatOS('Windows 10')).toBe('Windows 10/11');
});
test('returns the original value when unknown', () => {
const { formatOS } = setup();
expect(formatOS('Haiku')).toBe('Haiku');
});
});
describe('formatBrowser', () => {
test('maps known browser keys', () => {
const { formatBrowser } = setup();
expect(formatBrowser('chrome')).toBe('Chrome');
expect(formatBrowser('edge-chromium')).toBe('Edge (Chromium)');
});
test('returns the original value when unknown', () => {
const { formatBrowser } = setup();
expect(formatBrowser('mystery')).toBe('mystery');
});
});
describe('formatDevice', () => {
test('translates a known device label', () => {
const { formatDevice } = setup();
expect(formatDevice('mobile')).toBe('Mobile');
});
test('falls back to the unknown label for missing devices', () => {
const { formatDevice } = setup();
expect(formatDevice('foo')).toBe('Unknown');
});
});
describe('formatCountry', () => {
test('maps a known country code', () => {
const { formatCountry } = setup();
expect(formatCountry('US')).toBe('United States');
});
test('returns the original value when unknown', () => {
const { formatCountry } = setup();
expect(formatCountry('ZZ')).toBe('ZZ');
});
});
describe('formatRegion', () => {
test('formats a known region as "Region, Country"', () => {
const { formatRegion } = setup();
expect(formatRegion('US-CA')).toBe('California, United States');
});
test('returns the original value for an unknown region', () => {
const { formatRegion } = setup();
expect(formatRegion('ZZ-99')).toBe('ZZ-99');
});
test('handles undefined without throwing', () => {
const { formatRegion } = setup();
expect(formatRegion(undefined)).toBeUndefined();
});
});
describe('formatCity', () => {
test('appends the country name when known', () => {
const { formatCity } = setup();
expect(formatCity('Portland', 'US')).toBe('Portland, United States');
});
test('returns just the city when the country is unknown', () => {
const { formatCity } = setup();
expect(formatCity('Portland', 'ZZ')).toBe('Portland');
});
test('returns just the city when no country is provided', () => {
const { formatCity } = setup();
expect(formatCity('Portland')).toBe('Portland');
});
});
describe('formatLanguage', () => {
test('maps the base language subtag', () => {
const { formatLanguage } = setup();
expect(formatLanguage('en-US')).toBe('English');
expect(formatLanguage('en')).toBe('English');
});
test('returns the original value when unknown', () => {
const { formatLanguage } = setup();
expect(formatLanguage('xx-YY')).toBe('xx-YY');
});
});
describe('formatValue', () => {
test('dispatches to the correct formatter by type', () => {
const { formatValue } = setup();
expect(formatValue('Mac OS', 'os')).toBe('macOS');
expect(formatValue('chrome', 'browser')).toBe('Chrome');
expect(formatValue('mobile', 'device')).toBe('Mobile');
expect(formatValue('US', 'country')).toBe('United States');
expect(formatValue('US-CA', 'region')).toBe('California, United States');
expect(formatValue('en-US', 'language')).toBe('English');
});
test('passes the country from data through for city', () => {
const { formatValue } = setup();
expect(formatValue('Portland', 'city', { country: 'US' })).toBe('Portland, United States');
});
test('returns the raw string for unknown types', () => {
const { formatValue } = setup();
expect(formatValue('anything', 'unknownType')).toBe('anything');
});
test('returns undefined for a non-string value with an unknown type', () => {
const { formatValue } = setup();
expect(formatValue(123 as unknown as string, 'unknownType')).toBeUndefined();
});
});
});