Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions __tests__/breadcrumbUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import {describe, it, expect} from 'vitest'
import {
toTitleCase,
slugToTitle,
generateBreadcrumbs,
} from '../lib/breadcrumbUtils'

describe('breadcrumbUtils', () => {
describe('toTitleCase', () => {
it('capitalizes single word', () => {
expect(toTitleCase('hello')).toBe('Hello')
})

it('capitalizes each word', () => {
expect(toTitleCase('hello world')).toBe('Hello World')
})

it('handles already capitalized words', () => {
expect(toTitleCase('Hello World')).toBe('Hello World')
})

it('handles empty string', () => {
expect(toTitleCase('')).toBe('')
})

it('handles single character words', () => {
expect(toTitleCase('a b c')).toBe('A B C')
})
})

describe('slugToTitle', () => {
it('replaces hyphens with spaces', () => {
expect(slugToTitle('hello-world')).toBe('Hello World')
})

it('handles single word slug', () => {
expect(slugToTitle('archive')).toBe('Archive')
})

it('handles multiple hyphens', () => {
expect(slugToTitle('this-is-a-test')).toBe('This Is A Test')
})

it('handles empty string', () => {
expect(slugToTitle('')).toBe('')
})
})

describe('generateBreadcrumbs', () => {
it('returns only base breadcrumb for base path', () => {
const result = generateBreadcrumbs('/blog', {
baseSegment: 'blog',
baseHref: '/blog',
baseText: 'WXYC PRESS',
})
expect(result).toEqual([{href: '/blog', text: 'WXYC PRESS'}])
})

it('generates breadcrumbs for nested path', () => {
const result = generateBreadcrumbs('/blog/show-reviews', {
baseSegment: 'blog',
baseHref: '/blog',
baseText: 'WXYC PRESS',
})
expect(result).toEqual([
{href: '/blog', text: 'WXYC PRESS'},
{href: '/blog/show-reviews', text: 'Show Reviews'},
])
})

it('generates breadcrumbs for deeply nested path', () => {
const result = generateBreadcrumbs('/blog/show-reviews/my-review', {
baseSegment: 'blog',
baseHref: '/blog',
baseText: 'WXYC PRESS',
})
expect(result).toEqual([
{href: '/blog', text: 'WXYC PRESS'},
{href: '/blog/show-reviews', text: 'Show Reviews'},
{href: '/blog/show-reviews/my-review', text: 'My Review'},
])
})

it('applies pluralization at specified index', () => {
const result = generateBreadcrumbs('/blog/show-review/my-review', {
baseSegment: 'blog',
baseHref: '/blog',
baseText: 'WXYC PRESS',
pluralizeAtIndex: 1,
})
expect(result[2].text).toBe('My Reviews')
})

it('does not pluralize when index is null', () => {
const result = generateBreadcrumbs('/blog/show-review', {
baseSegment: 'blog',
baseHref: '/blog',
baseText: 'WXYC PRESS',
pluralizeAtIndex: null,
})
expect(result[1].text).toBe('Show Review')
})

it('builds cumulative hrefs correctly', () => {
const result = generateBreadcrumbs('/archive/concerts/2024', {
baseSegment: 'archive',
baseHref: '/archive',
baseText: 'Archive',
})
expect(result[1].href).toBe('/archive/concerts')
expect(result[2].href).toBe('/archive/concerts/2024')
})

it('filters empty segments', () => {
const result = generateBreadcrumbs('/blog//show-reviews/', {
baseSegment: 'blog',
baseHref: '/blog',
baseText: 'WXYC PRESS',
})
expect(result).toHaveLength(2)
expect(result[1].text).toBe('Show Reviews')
})

it('works without baseSegment stripping', () => {
const result = generateBreadcrumbs('/about/team', {
baseHref: '',
baseText: 'Home',
})
expect(result).toEqual([
{href: '', text: 'Home'},
{href: '/about', text: 'About'},
{href: '/about/team', text: 'Team'},
])
})

it('handles archive-style paths', () => {
const result = generateBreadcrumbs('/archive/concerts', {
baseSegment: 'archive',
baseHref: '/archive',
baseText: 'Archive',
})
expect(result).toEqual([
{href: '/archive', text: 'Archive'},
{href: '/archive/concerts', text: 'Concerts'},
])
})
})
})
153 changes: 153 additions & 0 deletions __tests__/organizingArchive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import {describe, it, expect} from 'vitest'
import {
groupEventsByWeek,
generateStructuredData,
} from '../components/OrganizingArchive'

describe('OrganizingArchive', () => {
describe('groupEventsByWeek', () => {
it('returns empty object for empty array', () => {
const result = groupEventsByWeek([])
expect(Object.keys(result)).toHaveLength(0)
})

it('groups events from the same week together', () => {
// All days in the same week (Mon-Wed of the same week)
const events = [
{node: {published: '2024-01-15', title: 'Monday Event'}},
{node: {published: '2024-01-16', title: 'Tuesday Event'}},
{node: {published: '2024-01-17', title: 'Wednesday Event'}},
]
const result = groupEventsByWeek(events)
const keys = Object.keys(result)
expect(keys).toHaveLength(1)
expect(result[keys[0]]).toHaveLength(3)
})

it('separates events from different weeks', () => {
const events = [
{node: {published: '2024-01-15', title: 'Week 1'}}, // Mon Jan 15
{node: {published: '2024-01-22', title: 'Week 2'}}, // Mon Jan 22
]
const result = groupEventsByWeek(events)
expect(Object.keys(result)).toHaveLength(2)
})

it('uses Sunday as week start', () => {
// Events on Mon Jan 15 and Sun Jan 21 should be in different weeks
// Jan 15 (Mon) -> week starts Jan 14 (Sun)
// Jan 21 (Sun) -> week starts Jan 21 (Sun)
const events = [
{node: {published: '2024-01-15T12:00:00', title: 'Monday Jan 15'}},
{node: {published: '2024-01-21T12:00:00', title: 'Sunday Jan 21'}},
]
const result = groupEventsByWeek(events)
const keys = Object.keys(result)
expect(keys).toHaveLength(2)
expect(keys).toContain('2024-01-14') // Week starting Sun Jan 14
expect(keys).toContain('2024-01-21') // Week starting Sun Jan 21
})

it('uses ISO date format for keys', () => {
const events = [{node: {published: '2024-01-15', title: 'Event'}}]
const result = groupEventsByWeek(events)
const key = Object.keys(result)[0]
expect(key).toMatch(/^\d{4}-\d{2}-\d{2}$/)
})

it('calculates correct week start date', () => {
// Wed Jan 17 should have week start of Sun Jan 14
const events = [{node: {published: '2024-01-17', title: 'Wednesday'}}]
const result = groupEventsByWeek(events)
expect(Object.keys(result)).toContain('2024-01-14')
})

it('handles week boundary correctly', () => {
// Sun Jan 14 is start of week, Sat Jan 20 is end of same week
const events = [
{node: {published: '2024-01-14T12:00:00', title: 'Sunday'}},
{node: {published: '2024-01-20T12:00:00', title: 'Saturday'}},
]
const result = groupEventsByWeek(events)
expect(Object.keys(result)).toHaveLength(1)
expect(Object.keys(result)).toContain('2024-01-14')
})

it('extracts node data from events', () => {
const events = [
{
node: {
published: '2024-01-15',
title: 'Test Event',
description: 'A description',
},
},
]
const result = groupEventsByWeek(events)
const weekKey = Object.keys(result)[0]
expect(result[weekKey][0]).toEqual({
published: '2024-01-15',
title: 'Test Event',
description: 'A description',
})
})
})

describe('generateStructuredData', () => {
it('returns empty array for empty input', () => {
const result = generateStructuredData({})
expect(result).toHaveLength(0)
})

it('generates heading and events for each week', () => {
const groupedEvents = {
'2024-01-14': [{title: 'Event 1'}, {title: 'Event 2'}],
}
const result = generateStructuredData(groupedEvents)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({type: 'heading', weekStartDate: '2024-01-14'})
expect(result[1]).toEqual({
type: 'events',
weekEvents: [
{type: 'event', event: {title: 'Event 1'}},
{type: 'event', event: {title: 'Event 2'}},
],
})
})

it('handles multiple weeks', () => {
const groupedEvents = {
'2024-01-14': [{title: 'Week 1 Event'}],
'2024-01-21': [{title: 'Week 2 Event'}],
}
const result = generateStructuredData(groupedEvents)
// Should have 2 headings + 2 event blocks = 4 items
expect(result).toHaveLength(4)
expect(result.filter((item) => item.type === 'heading')).toHaveLength(2)
expect(result.filter((item) => item.type === 'events')).toHaveLength(2)
})

it('preserves week order from input object', () => {
const groupedEvents = {
'2024-01-14': [{title: 'First'}],
'2024-01-21': [{title: 'Second'}],
}
const result = generateStructuredData(groupedEvents)
const headings = result.filter((item) => item.type === 'heading')
expect(headings[0].weekStartDate).toBe('2024-01-14')
expect(headings[1].weekStartDate).toBe('2024-01-21')
})

it('wraps each event with type: event', () => {
const groupedEvents = {
'2024-01-14': [{title: 'Event', id: 1}],
}
const result = generateStructuredData(groupedEvents)
const eventsBlock = result.find((item) => item.type === 'events')
expect(eventsBlock.weekEvents[0]).toEqual({
type: 'event',
event: {title: 'Event', id: 1},
})
})
})
})
41 changes: 41 additions & 0 deletions __tests__/posthog.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,47 @@ describe('PostHog tracking', () => {
})
)
})

it('calls debug() in development mode via loaded callback', async () => {
const originalEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'development'

const {initPostHog} = await import('../lib/usePostHog')
initPostHog('test-key')

// Get the loaded callback that was passed to init
const initCall = mockInit.mock.calls[0]
const options = initCall[1]
expect(options.loaded).toBeDefined()

// Call the loaded callback with a mock posthog instance
const mockPosthogInstance = {debug: mockDebug}
options.loaded(mockPosthogInstance)

expect(mockDebug).toHaveBeenCalled()

process.env.NODE_ENV = originalEnv
})

it('does not call debug() in production mode', async () => {
const originalEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'production'

const {initPostHog} = await import('../lib/usePostHog')
initPostHog('test-key')

// Get the loaded callback
const initCall = mockInit.mock.calls[0]
const options = initCall[1]

// Call the loaded callback
const mockPosthogInstance = {debug: mockDebug}
options.loaded(mockPosthogInstance)

expect(mockDebug).not.toHaveBeenCalled()

process.env.NODE_ENV = originalEnv
})
})

describe('usePostHogPageview', () => {
Expand Down
Loading