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
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { EventCard, EventModal, EventsSection } from '../../src/components/Events.js'
describe('Events', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('EventCard', () => {
it('should render event card with basic information', () => {
const event = {
id: 1,
host_id: 2,
title: 'Test Event',
date: '2024-12-25',
host: { name: 'John Doe' },
description: 'A test event',
rsvps: [],
image_url: 'https://example.com/image.jpg'
}
const result = EventCard(event)
expect(result).toContain('Test Event')
expect(result).toContain('John Doe')
expect(result).toContain('A test event')
expect(result).toContain('https://example.com/image.jpg')
expect(result).toContain('0 went')
})
it('should handle missing optional fields', () => {
const event = {
id: 1,
title: 'Test Event',
date: '2024-12-25T10:00:00Z',
host_id: 123,
rsvps: [],
image_url: 'http://image.web'
}
const result = EventCard(event)
expect(result).toContain('Test Event')
expect(result).toContain('User 123')
expect(result).not.toContain('undefined')
})
it('should show past events correctly', () => {
const event = {
id: 1,
host_id: 4,
title: 'Past Event',
date: '1995-01-01',
host: { name: 'John Doe' },
image_url: 'http://image.web',
rsvps: [{}, {}] // 2 RSVPs
}
const result = EventCard(event);
expect(result).toContain('2 went');
expect(result).not.toContain('<button role="button" data-target="modal-event-1" ');
})
it('should show upcoming events with RSVP button', () => {
const futureDate = new Date()
futureDate.setDate(futureDate.getDate() + 1) // Tomorrow
const event = {
id: 1,
host_id: 6,
title: 'Future Event',
date: futureDate.toISOString(),
host: { name: 'John Doe' },
image_url: 'http://image.web',
rsvps: []
}
const result = EventCard(event)
expect(result).toContain('0 going')
expect(result).toContain('RSVP')
})
})
describe('EventModal', () => {
it('should render modal with correct form structure', () => {
const event = {
id: 1,
title: 'Test Event',
date: '2029-12-31',
host_id: 1,
image_url: 'http://image.web'
}
const result = EventModal(event)
expect(result).toContain('modal-event-1')
expect(result).toContain('rsvp-form-1')
expect(result).toContain('RSVP to Test Event')
expect(result).toContain('input type="text"')
expect(result).toContain('input type="email"')
})
})
describe('EventsSection', () => {
it('should render section with events', () => {
const events = [
{
id: 1,
title: 'Event 1',
date: '2024-12-25T10:00:00Z',
host_id: 1,
host: { name: 'Host 1' },
image_url: 'http://image.web',
rsvps: []
},
{
id: 2,
host_id: 5,
title: 'Event 2',
date: '2024-12-26T10:00:00Z',
host: { name: 'Host 2' },
image_url: 'http://image.web',
rsvps: []
}
]
const result = EventsSection('Upcoming', events)
expect(result).toContain('Upcoming events')
expect(result).toContain('Event 1')
expect(result).toContain('Event 2')
})
it('should handle empty events array', () => {
const result = EventsSection('Past', [])
expect(result).toContain('Past events')
expect(result).toContain('No events')
})
})
})
|