Skip to content
Merged
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
13 changes: 11 additions & 2 deletions app/components/DateTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ const { locale } = useI18n()

const relativeDates = useRelativeDates()

const dateFormatter = new Intl.DateTimeFormat(locale.value, {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short',
})

// Compute the title - always show full date for accessibility
const titleValue = computed(() => {
if (props.title) return props.title
if (typeof props.datetime === 'string') return props.datetime
return props.datetime.toISOString()
const date = typeof props.datetime === 'string' ? new Date(props.datetime) : props.datetime
return dateFormatter.format(date)
})
</script>

Expand Down
13 changes: 7 additions & 6 deletions test/nuxt/components/DateTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vi.mock('~/composables/useSettings', () => ({
describe('DateTime', () => {
const testDate = '2024-01-15T12:00:00.000Z'
const testDateObject = new Date('2024-06-15T10:30:00.000Z')
const testYear = testDateObject.getUTCFullYear()

beforeEach(() => {
mockRelativeDates.value = false
Expand Down Expand Up @@ -61,12 +62,12 @@ describe('DateTime', () => {
})

describe('title attribute', () => {
it('uses datetime string as title by default', async () => {
it('has title with formatted date by default', async () => {
const component = await mountSuspended(DateTime, {
props: { datetime: testDate },
})
const timeEl = component.find('time')
expect(timeEl.attributes('title')).toBe(testDate)
expect(timeEl.attributes('title')).toContain(testYear)
})

it('uses custom title when provided', async () => {
Expand All @@ -81,12 +82,12 @@ describe('DateTime', () => {
expect(timeEl.attributes('title')).toBe(customTitle)
})

it('converts Date object to ISO string for title', async () => {
it('converts Date object to formatted date in title', async () => {
const component = await mountSuspended(DateTime, {
props: { datetime: testDateObject },
})
const timeEl = component.find('time')
expect(timeEl.attributes('title')).toBe(testDateObject.toISOString())
expect(timeEl.attributes('title')).toContain(testYear)
})
})

Expand Down Expand Up @@ -119,14 +120,14 @@ describe('DateTime', () => {
let component = await mountSuspended(DateTime, {
props: { datetime: testDate },
})
expect(component.find('time').attributes('title')).toBe(testDate)
expect(component.find('time').attributes('title')).toContain(testYear)

// Test with relative dates on
mockRelativeDates.value = true
component = await mountSuspended(DateTime, {
props: { datetime: testDate },
})
expect(component.find('time').attributes('title')).toBe(testDate)
expect(component.find('time').attributes('title')).toContain(testYear)
})
})

Expand Down
Loading