-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathDateTime.spec.ts
More file actions
162 lines (144 loc) · 5.12 KB
/
DateTime.spec.ts
File metadata and controls
162 lines (144 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import DateTime from '~/components/DateTime.vue'
// Mock the useRelativeDates composable
const mockRelativeDates = shallowRef(false)
vi.mock('~/composables/useSettings', () => ({
useRelativeDates: () => ({
relativeDates: mockRelativeDates,
}),
useSettings: () => ({
settings: ref({ relativeDates: mockRelativeDates.value }),
}),
useAccentColor: () => ({}),
}))
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
})
describe('props handling', () => {
it('accepts datetime as ISO string', async () => {
const component = await mountSuspended(DateTime, {
props: { datetime: testDate },
})
expect(component.html()).toContain('time')
})
it('accepts datetime as Date object', async () => {
const component = await mountSuspended(DateTime, {
props: { datetime: testDateObject },
})
expect(component.html()).toContain('time')
})
it('passes date-style prop to NuxtTime', async () => {
const component = await mountSuspended(DateTime, {
props: {
datetime: testDate,
dateStyle: 'medium',
},
})
// The component should render with the specified dateStyle
expect(component.html()).toBeTruthy()
})
it('passes individual date parts to NuxtTime', async () => {
const component = await mountSuspended(DateTime, {
props: {
datetime: testDate,
year: 'numeric',
month: 'short',
day: 'numeric',
},
})
expect(component.html()).toBeTruthy()
})
})
describe('title attribute', () => {
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')).toContain(testYear)
})
it('uses custom title when provided', async () => {
const customTitle = 'Custom date title'
const component = await mountSuspended(DateTime, {
props: {
datetime: testDate,
title: customTitle,
},
})
const timeEl = component.find('time')
expect(timeEl.attributes('title')).toBe(customTitle)
})
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')).toContain(testYear)
})
})
describe('relative dates setting', () => {
it('renders absolute date when relativeDates is false', async () => {
mockRelativeDates.value = false
const component = await mountSuspended(DateTime, {
props: {
datetime: testDate,
dateStyle: 'medium',
},
})
// Should not have the "relative" attribute behavior
// The NuxtTime component will render with date formatting
expect(component.html()).toContain('time')
})
it('renders with relative prop when relativeDates is true', async () => {
mockRelativeDates.value = true
const component = await mountSuspended(DateTime, {
props: { datetime: testDate },
})
// Component should still render a time element
expect(component.html()).toContain('time')
})
it('always preserves title attribute for accessibility regardless of mode', async () => {
// Test with relative dates off
mockRelativeDates.value = false
let component = await mountSuspended(DateTime, {
props: { datetime: 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')).toContain(testYear)
})
it('clicking button to toggle the relative dates settings', async () => {
const component = await mountSuspended(DateTime, {
props: { datetime: testDate },
})
const button = component.find('button')
await button.trigger('click')
expect(button.attributes('aria-pressed')).toBe('true')
await button.trigger('click')
expect(button.attributes('aria-pressed')).toBe('false')
})
})
describe('SSR fallback', () => {
it('renders time element in fallback (SSR) mode', async () => {
// The ClientOnly component has a fallback slot for SSR
// This test ensures the fallback renders correctly
const component = await mountSuspended(DateTime, {
props: {
datetime: testDate,
dateStyle: 'medium',
},
})
// Should have a time element rendered
expect(component.find('time').exists()).toBe(true)
})
})
})